5 ms·
By 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
by londons_explore 6y ago
By 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.
- zerovox 6y ago> We also require every access to any stack variable to do math to figure out where on the stack it's arguments are. In the article, it appears to be the opposite. Previously this math was required (`[ai] = 2 + parameter_count - i - 1`), but by reversing the arguments in the stack it's now always a constant offset, and they prevent indexing out of the passed arguments in the frame by ensuring there's at least as many arguments as formal parameters by stuffing the call with extra `undefined`s: > But what happens if we reverse the arguments? Now the offset can be simply calculated as [ai] = 2 + i. We don’t need to know how many arguments are in the stack, but if we can guarantee that we'll always have at least the parameter count of arguments in the stack, then we can always use this scheme to calculate the offset.
- IainIreland 6y agoOne of the things I've realized after working on SpiderMonkey for a few years is that simple solutions that accept a little bit of overhead are often faster than more complicated approaches that try to eke out every last cycle. Math is fast. Stack space is rarely a key constraint. On the other hand, tracking typical argument counts for a function also requires overhead, and compiling multiple copies of a function is also not free. If a particular call site is hot enough that call overhead matters, it's probably hot enough that inlining is the correct answer. As one additional data point: V8's new approach appears to be very similar to how SpiderMonkey's been implemented all along. Convergent evolution is generally a good sign.
- hinkley 6y agoYouTube fed me a video I’d watched a couple years ago about a log searching tool. Compressed data, decompressed into cpu cache, and then scanned. Almost no indexing at all, just cpu cache for speed. We spend a lot of time arguing with the cpu. If we give it something it’s actually happy doing, it almost doesn’t matter how stupid that thing is, because it’s stupid fast.
- owaislone 6y agoInteresting. Would really appreciate if you could share the video with us. Thanks
- posnet 6y agoThis is one of the main selling points of Blosc https://blosc.org/pages/blosc-in-depth/ https://blosc.org/pages/blosc-in-depth/
- e12e 6y agoI couldn't find the talk hinted at, but this blog post seems to touch on some of the same ideas: https://www.humio.com/whats-new/blog/how-fast-can-you-grep/ https://www.humio.com/whats-new/blog/how-fast-can-you-grep/
- devbug 6y ago
- simias 6y ago>We also require every access to any stack variable to do math to figure out where on the stack it's arguments are. I suspect that on modern architectures these trivial additions are effectively free most of the time. In my experience the hard part of optimizing for modern architectures is compensating for memory latency (cache optimization, prefetching) and pipeline flushes (branch prediction). An inline, unconditional add is as cheap as it gets these days. A few additional bytes of stack that's (almost) always going to be super hot in cache is not really significant. I suppose it could pose an issue for super deep function call tree where it would cause the stack to grow more than necessary, but given the usual memory overhead of JS I doubt that it's going to be very significant even then.
- CyberRabbi 6y agoStack space is virtually free and it’s just a few instructions to put the value on the stack. Regardless, this increased overhead is very small compared to the total overhead of making a function call in the first place. The place to eke out every last cycle is the tight loop, which should not include function calls anyway.
- hinkley 6y ago> Stack space is virtually free On machines with 64 bit addressing. There are lots of things that don’t work so well on 32 bit or 16 bit OSes that we have to unlearn. There have been a couple times where I thought about trying to catalog all of the things I think I know about a piece of hardware, software or even a single library, so that I can challenge my own assumptions when a major release comes out. Never have gotten around to it.
- CyberRabbi 6y ago> On machines with 64 bit addressing. There are lots of things that don’t work so well on 32 bit or 16 bit OSes that we have to unlearn. Stack space is virtually free on 32 bit systems as well. V8 is not designed for or portable to 16-bit or 8-bit systems, so while your point is interesting, it’s irrelevant here.
- hinkley 6y ago> Stack space is virtually free on 32 bit systems as well. How do you figure that, given we’ve had quite a bit of time wrestling with 2G memory limitations and how those interact badly with multithreading?
- CyberRabbi 6y agoFor general applications and as a matter of good practice, stack depth practically never gets deep enough to make a dent in 2GB. If your stack is reaching a significant proportion of 2GB you are seriously doing something wrong and it’s likely other parts of your application will start breaking as well.