10 ms·
Faster JavaScript Calls
- mromanuk 6y ago> “turns out that, with a clever trick, we can remove this extra frame, simplify the V8 codebase and get rid of almost the entire overhead.” A lost opportunity to mention “with this one weird trick”
- The_rationalist 6y agoI wonder if Openjdk or other VMs could benefit from a similar optimization.
- Gaelan 6y agoI'd imagine not. Assuming the JVM's semantics aren't too different from those of the Java language, you always know at compile time how many arguments a function takes[1], so this optimization wouldn't be relevant. [1]: Java does have variadic functions, but we also know at compile time which functions have such a signature, so it probably just desugars to a normal array as the final argument.
- avereveard 6y agowell, there's also invokedynamic, but call site selection is in user space, so one can implement this optimization for it's own dynamic language without violating the JVM own static invocation rules.
- PaulBGD_ 6y agoJava knows at compile time exactly how many arguments are passed to a method, so presumably there's no adapter layer.
- The_rationalist 6y agoWrong, Java Varargs functions can take spreaded arrays as Var parameters hence the number for this not uncommon case (e.g jdbc apis, format(), etc) is only known at runtime
- colejohnson66 6y agoBut in Java (and similar), you have to be explicit about varargs. In JavaScript, every function supports varargs. This will run just fine: function print(arg) { console.log(arg); } print("a", "b", "c"); It’ll only output "a", obviously. In fact, this will run just fine too: print(); You’ll get "undefined" in the console, but it’ll work. JavaScript’s nature is that arguments are in the "arguments" pseudo-array, and the parameters are just fancy names for different values in "arguments". See: function count() { console.log(arguments.length); } count(); count("a"); count("a", "b"); In order, you’ll get: 0, 1, and 2. Despite the fact that "count" has no parameters in the definition. In the first function ("print"), "arg" is just syntax sugar for "arguments[0]". What I’m getting at is: in C, Java, etc., the compiler knows what is a varargs function and what isn’t. In JavaScript, the interpreter/compiler doesn’t and has to assume everything is.
- desert_boi 6y agohttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters#the_difference_between_rest_parameters_and_the_arguments_object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- colejohnson66 6y agoHence, “pseudo-array”
- The_rationalist 6y agoGreat explanation and I think you are right for Java, Kotlin, etc. But what about dynamic languages on the JVM such as GraalJS, groovy, etc?
- masklinn 6y ago> In the first function ("print"), "arg" is just syntax sugar for "arguments[0]". So much so that they reflect one another: you can set `arguments[0]` and retrieve that value from `arg`, and the other way around: function foo(arg0, arg1) { arg0 = "foo"; arguments[1] = "bar"; console.log(Array.from(arguments), arg0, arg1); } foo(1, 2) will log [ "foo", "bar" ] foo bar Although in reality optimising engines treat `arguments` as a keyword of sorts: they will not reify the object if they don't have to. However this meant they had to deoptimise a fair bit before the "spread" arguments arrived as that was how you'd call your "super".
- kevingadd 6y agoThe whole problem this solves doesn't exist in the JVM.
- The_rationalist 6y agoThe JVM isn't limited to Java, for example GraalJs is also an ecmascript compliant JS engine
- pjmlp 6y agoGraal uses a complete different implementation model.
- The_rationalist 6y agoNever heard about nashorn, jruby, groovy then ? It all use the same C2 JIT as Java
- pjmlp 6y agoNone of them relate to Graal, they just pretend to be Java to the JVM.
- The_rationalist 6y agoThe whole problem this solves doesn't exist in the JVM. The JVM isn't limited to Java, for example: nashorn, jruby, groovy Hence the optimization would make sense for the JVM for those client languages: nashorn, jruby, groovy, etc Quite trivial to understand, isn't it?
- pjmlp 6y agoExcept Graal isn't the JVM, quite trivial to understand by anyone that knows a little bit about compilers.
- londons_explore 6y agoBy putting the number of arguments onto the stack, we end up making the stack larger for every single function call. We also require every access to any stack variable to do math to figure out where on the stack it's arguments are. That seems an insane overhead for the common case... If this really does make benchmarks faster overall, it suggests an even better solution might exist out there that does not add overhead for the common case... Perhaps it might be worth keeping track of how many arguments a function is typically called with, and recompiling code for each case? Then all the checks for argument counts can be entirely removed in the optimized code.
- thechao 6y agoIs it better, or worse, than dumping the adapter object into the stack? Clearly, their numbers show it’s better. Also, runtime variable calling conventions are a nightmare. Telling the callee the calling convention (in some sort of mixed mode) still requires a flag ... like an integer? So, it seems they have to pay the cost of the integer no matter what. Maybe I’m wrong — could you elaborate how you’d do it?
- josefx 6y ago> Clearly, their numbers show it’s better. Their numbers seem cherry picked or is Turbo Fan actually unable to inline code?
- ash_gti 6y agoTF can inline, but if it runs that optimization then the benchmark would be turned into a constant, so it wouldn't be measuring the call overhead of the way arguments are passed.
- josefx 6y agoInlining is something I would think also eliminates stack frames. So a comparison between an optimization that gets rid of a complicated stack frame and a compiler that has inlining turned off isn't something I would consider ideal.
- bachmeier 6y ago"JavaScript allows calling a function with a different number of arguments than the expected number of parameters, i.e., one can pass fewer or more arguments than the declared formal parameters." I'm relatively new to Javascript. I've been bitten by both of these recently. I wasted an hour where I added a third argument to a function but missed a place elsewhere that was sending only two parameters. That makes it harder to change your code. Now I read that it's not only a good way to introduce ugly bugs, but that this wonderful feature also makes your code run slower. Genius.
- jchw 6y agoIt has its issues, but the best use case is as a mechanism for optional parameters, especially when adding new parameters to existing functions while maintaining API compatibility, which is especially important for standard functions. If you want to reduce the amount of mistakes made, in TypeScript accidental under-application becomes a type error. I feel the experience of writing TypeScript is a lot smoother than JS alone.
- harikb 6y agoOther languages have solved it using better methods. 1. Declare another function with same name and make the older function a wrapper with a default for new argument 2. Support for an explicit default argument Either of these would have prevented GP from having their phone bug/problem
- jakelazaroff 6y agoPresumably OP didn’t want to create a new function, or they would have. JavaScript supports default parameters, but again, presumably OP wanted the caller to supply all the arguments. If you’re trying to change the signature of an existing function, I don’t think we’ve yet figured out a better safeguard than static typing.
- colejohnson66 6y agoIt’s possible to use "arguments.length" to “change” the function signature and execute different code for different parameter counts. It’s a hack, but it works.
- khalilravanna 6y agoThis sort of low level stuff is so interesting to me but I feel out of my depth having not looked at this sort of stuff since undergrad. In a hypothetical world where someone wanted to contribute to v8, what are some good resources to get up to speed?
- hashseed 6y agoThere is a lot of content on V8's dev blog, with different depth, all pretty well written: https://v8.dev/blog https://v8.dev/blog
- khalilravanna 6y agoFor sure the blog is great! But I’m thinking something more along the lines of “here’s how you’d build something like this” or “here’s the stuff to read to get started on a project like this”.
- saagarjha 6y agoJIT engineers are mostly oddly specialized compiler engineers, so you're really looking at learning how optimizing compilers work as a prerequisite.
- pas 6y agoIf you mean JIT, then I'd recommend looking at PyPy, also other JS engines and Java HotSpot, to see what other techniques are out there, when they work, when don't and why. Ultimately V8 is almost an ecosystem in itself, it's embedded into quite a few other software, the ECMA262 standard is continuously changing, the Web is changing, Chrome changes on top of all this, plus there is a lot of backward compatibility stuff. You can look at the V8 bugtracker pick a task and try your luck.
- brundolf 6y agoI've brought this up before, but I wonder if a "JS constraint spec" could be added to the JS standard that guarantees against this sort of (already uncommon) dynamic behavior in certain cases, so that code can be more aggressively optimized when possible If you're already using TypeScript to constrain your code, why couldn't it generate interpreter/compiler hints? These could take the form of a language-agnostic metadata file, similar to source maps, that optionally ships alongside the JS bundle Edit: Several people have misunderstood, so I must not have articulated this well. What I meant was not a new/stricter subset of JS that forbids certain dynamic behaviors across the board. What I'm talking about is having TypeScript (or otherwise), which knows things at compile-time about how specific pieces of code are and are not called, manifest this information in a way that V8 can digest and act on directly, for that specific codebase. V8 already tries to guess this information and uses it to decide which things to optimize and how, but it's treating the JS bundle as a black box even though in many cases, an earlier stage of the pipeline already had this information on-hand and threw it away. In addition to being more granular, this (like TypeScript itself) would allow parts of a codebase to continue being fully dynamic while other parts are well-constrained.
- matsemann 6y agoThere's already "use strict" that does some of this to enable optimizations and other stuff. Maybe it should be even stricter or something similar could be used. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- brundolf 6y agoRight but I'm talking about very specific hints for one codebase "Function X will only ever take two arguments, they will be of types Y and Z" "This object will only ever have these properties; it will never have properties created or deleted" "This prototype will never be modified at runtime"
- hajile 6y agoIt still has to verify that those assertions are true in practice. Any random person at the console can call a function and pass whatever they like. The only real way to make that work would be a new `<script type="typed-module" />` variant that implemented a subset of JS in a typed environment. My guess is that this could happen in the future, but everyone is watching Typescript as a testing ground to find exactly what works, what doesn't, and what works, but isn't desirable.
- inglor 6y agoVery exciting! This is super useful for Node.js - I've had PRs with features I didn't land because of this and I intend to investigate again e.g. https://github.com/nodejs/node/pull/35877 https://github.com/nodejs/node/pull/35877
- adzm 6y agoReminds me of cdecl vs stdcall with the argument ordering. Really interesting low level details here for sure.
- BenoitEssiambre 6y agoI once proposed to also speed up and simplify asynchronous async/await function calls by removing the extra caching layer and state machine underlying all such calls that come from using promises: https://es.discourse.group/t/callback-based-simplified-async-await/343 https://es.discourse.group/t/callback-based-simplified-async...
- cxcxcxcx 6y agoHave you considered that generators are basically state machines?
- BenoitEssiambre 6y agoYes, but I meant not an additional state machine and caching layer. Promise based async/await also have the generators.
- k__ 6y agoI read, TypeScript code is sometimes faster, because it tends to favor monomorphic functions. Can I assume that V8 moved that gain to JS?
- exyi 6y agoEven in TypeScript optional parameters are very common, so I don't really think it's a JS specific optimization.
- toastal 6y agoIf you want to maintain compatibility with existing JavaScript, you kinda have to support the complexity of variadic functions and multiple return types; otherwise FFI would be required to use JavaScript. It seems this a convenience comprise TypeScript makes.
- Amin3456 6y agoHow to Take Care of Betta Fish https://bit.ly/2Zqahul https://bit.ly/2Zqahul
- pierrebai 6y agoI find it amusing that they basically "rediscovered" the C printf() trick, AKA variadic function trick.
- ratboy666 6y agoBeing old... I read the article quickly, and had the same thought. Yes, C pushes arguments in reverse order... and we never cared if arguments were "under specified". Or over. Just needed to know where the first argument was, and how to get to the second, and how to return to the caller. The caller, of course, is then responsible for argument clean-up. Made sense 50 years ago... makes sense now. FredW
- abdo2023 6y agoThe 12 Best Jobs for Dog Lovers https://bit.ly/MONY23GIFT https://bit.ly/MONY23GIFT
- gameswithgo 6y agoIt is amazing the amount of brain power put into making this language fast-ish. Too that brain power wasn't used to do other things.
- GiftCard22 6y agoThe 12 Best Jobs for Dog Lovers (https://bit.ly/MONY23GIFT https://bit.ly/MONY23GIFT)
- jcims 6y agoLex Fridman just did a great podcast episode with Brendan Eich, credited with creating JavaScript. Lots of great little tidbits of history in the conversation - https://www.youtube.com/watch?v=krB0enBeSiE https://www.youtube.com/watch?v=krB0enBeSiE
- nojvek 6y agoI’ve been coding JS for a decade. Nowadays I wish JS was simpler. I’m fine with ES6 syntactic sugar. I just want the language to do be stricter, safer and faster. I don’t want to transpile TS into JS. Just make JS strict with monomorphic types. One thing that really impressed me is how crazy fast golang is. Not just at runtime but compile time too. Esbuild is like 20x faster than webpack. That’s nuts. I see a webserver running on 512MB ram and single core, serving 10s of thousands of requests a second without batting an eye. Just rock solid reliability and performance. I want to build web apps with low memory, cpu, and bandwidth profile. Give us a simpler language that doesn’t need crazy complicated hotspot profiling JITs. Just make the default, simple thing insanely fast. Give us better fundamental primitives that don’t need a gajillion build tools. The JS ecosystem is so complicated nowadays. It just seems we’re patching shit on top of each other without reasoning from first principles. Disclaimer: I am not saying this specific improvement isn't worth it. I love v8, but I feel we can make magnitudes of higher performance gains by removing things and simplifying such that you don't even need top level complex optimizations.
- marton78 6y agoI think what you want it wasm. You just need to wait for a simple language which compiles to it - or learn rust.
- pwdisswordfish6 6y ago> I want to build web apps with low memory, cpu, and bandwidth profile. Give us a simpler language that doesn’t need crazy complicated hotspot profiling JITs. Just make the default, simple thing insanely fast. What makes you think it isn't? What if I told you that people were creating Electron-style apps over a decade before Electron ever existed? Go use Firefox 0.8 or Netscape 6. Pretty much every part of the UI on your screen that you'll interact with is backed by JavaScript. Keep in mind that this was 15–20 years ago, pre-Windows Vista, where main memory wasn't measured in gigabytes. And that was all pre-JIT! Here's a screenshot of the file listing for the version of AOL Instant Messenger that was built into Netscape Navigator: https://imgur.com/a/ztaMVon https://imgur.com/a/ztaMVon And a shaky cellphone video of someone using it over 10 years later, before AOL shut down the network: https://www.youtube.com/watch?v=apyR0bPnFAo https://www.youtube.com/watch?v=apyR0bPnFAo > Esbuild is like 20x faster than webpack. The problem is webpack and the development style favored by the NodeJS/NPM community. It's filled with bad practices, and they all do it because either no one knows better, or they don't care. JS is not inherently slow. It's exceptionally fast, even. Contrary to popular belief, though, how you write code still matters. The myth of the sufficiently smart compiler (including JITs) is that: a myth.