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.
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.
https://www.scalyr.com/blog/searching-1tb-sec-systems-engine...
Where are these programs not using the CPU cache?
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.
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.
Maybe I’m wrong — could you elaborate how you’d do it?
Their numbers seem cherry picked or is Turbo Fan actually unable to inline code?
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.
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.
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?
Given 32 bit words you'll see 4 bytes per call for parameter counts. A call stack 100 calls deep will incur 400 extra bytes of stack space overhead; less than half a KiB. Along the way all extra 'arguments adaptor frames' are eliminated, so the net is even smaller.
That doesn't look like a problem. If you're 100 calls deep and you blow out the stack because it's 400 bytes larger you have other issues.
Most programming languages need the stack to be a linear set of addresses. So you figure out what the max reasonable stack size is for a given thread, and you pre-allocate that address space. Which means even if the stack stays shallow, you can't use those addresses for heap. If you increase the average case for your call stack size, then the available heap shrinks.
That's why I'm saying we are doing this now, even though in theory this knowledge has been available to us for a very long time. Nearly everything is 64 bit now, and even in the JVM where they carve 2 bits off for parallel mark and sweep, there are still oceans of extra address space for us to 'waste'.
The increase in stack usage is highly unlikely to require more space than the default 'reasonable' stack size already being allocated for threads in 32b environments where one is likely to be operating v8; typically the defaults are already pretty generous. Unless the default thread stack size allocation actually has to be increased to accommodate the modest growth caused by the change then there is zero impact on available heap.
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.
It's not 'my stack'. It's all stacks. Each thread has a stack. They are all in your 1.2-2GB address space. If you're not using a Global Interpreter Lock'ed language, you could easily end up with hundreds of threads for production code. And the more you try to dedupe work, the more threads you are likely to end up with.
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.