News for hackers. Stuff that matters.
Re:Faster JavaScript Calls by londons_explore on Monday February 15, 2021 @12:19PM (#26144731)
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.

Re:Faster JavaScript Calls by IainIreland on Monday February 15, 2021 @01:09PM (#26145305)
One 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.

Re:Faster JavaScript Calls by hinkley on Monday February 15, 2021 @01:56PM (#26145855)
YouTube 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.

Re:Faster JavaScript Calls by owaislone on Monday February 15, 2021 @02:11PM (#26146016)
Interesting. Would really appreciate if you could share the video with us. Thanks
Re:Faster JavaScript Calls by posnet on Monday February 15, 2021 @03:12PM (#26146732)
This is one of the main selling points of Blosc https://blosc.org/pages/blosc-in-depth/
Re:Faster JavaScript Calls by e12e on Monday February 15, 2021 @03:35PM (#26146982)
I 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/
Re:Faster JavaScript Calls by devbug on Monday February 15, 2021 @08:51PM (#26150035)
Brute force search is how Scaylr does it. Here’s an article they wrote about their implementation.

https://www.scalyr.com/blog/searching-1tb-sec-systems-engine...

Re:Faster JavaScript Calls by mhh__ on Monday February 15, 2021 @06:41PM (#26149192)
> Compressed data, decompressed into cpu cache, and then scanned. Almost no indexing at all, just cpu cache for speed.

Where are these programs not using the CPU cache?

Re:Faster JavaScript Calls by hinkley on Tuesday February 16, 2021 @03:25PM (#26158350)
Indexes and tree structures involve pointer walking. Nothing is even guaranteed to be in main memory, nevermind L2 cache. These guys apparently went straight from disk (linear reads) to L1/L2.
Re:Faster JavaScript Calls by chii on Monday February 15, 2021 @09:43PM (#26150337)
assume they meant decompress small enough a block to fit the CPU cache, and do in-memory scans.
Re:Faster JavaScript Calls by zerovox on Monday February 15, 2021 @12:46PM (#26145043)
> 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.

Re:Faster JavaScript Calls by simias on Monday February 15, 2021 @01:10PM (#26145316)
>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.

Re:Faster JavaScript Calls by thechao on Monday February 15, 2021 @12:41PM (#26144981)
Is 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?

Re:Faster JavaScript Calls by josefx on Monday February 15, 2021 @04:40PM (#26147818)
> Clearly, their numbers show it’s better.

Their numbers seem cherry picked or is Turbo Fan actually unable to inline code?

Re:Faster JavaScript Calls by ash_gti on Monday February 15, 2021 @06:22PM (#26149002)
TF 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.
Re:Faster JavaScript Calls by josefx on Wednesday February 17, 2021 @04:19AM (#26164397)
Inlining 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.
Re:Faster JavaScript Calls by CyberRabbi on Monday February 15, 2021 @01:43PM (#26145720)
Stack 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.
Re:Faster JavaScript Calls by hinkley on Monday February 15, 2021 @02:03PM (#26145941)
> 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.

Re:Faster JavaScript Calls by CyberRabbi on Monday February 15, 2021 @02:26PM (#26146174)
> 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.

Re:Faster JavaScript Calls by hinkley on Monday February 15, 2021 @03:44PM (#26147094)
> 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?

Re:Faster JavaScript Calls by topspin on Tuesday February 16, 2021 @12:33AM (#26151255)
> How do you figure that

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.

Re:Faster JavaScript Calls by hinkley on Tuesday February 16, 2021 @12:20PM (#26156227)
I'm not talking about word size on the stack. I'm talking about 2GB available for heap, plus reserved space for all stacks.

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'.

Re:Faster JavaScript Calls by topspin on Tuesday February 16, 2021 @02:26PM (#26157721)
> and you pre-allocate that address space

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.

Re:Faster JavaScript Calls by CyberRabbi on Monday February 15, 2021 @04:23PM (#26147630)
For 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.

Re:Faster JavaScript Calls by hinkley on Tuesday February 16, 2021 @12:23PM (#26156264)
I think it's pretty clear that I'm saying "this has not been my experience."

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.