Re:Slimmer and faster JavaScript strings in Firefox
by kevingadd on Monday July 21, 2014 @09:29PM
(#8067033)
SpiderMonkey uses one of the rather elaborate, carefully-tuned modern allocators (tcmalloc or jemalloc, I forget which) that's designed around clustering allocations into 'size categories', and carving allocations out of those categories. As a result, a given allocation will end up with overhead based on the size bucket it's put into, and certain buckets may perform better.
In this environment it's very useful for each type to be fixed-size (within reason) and be consistently sized. You'll end up with all your JSStrings allocated out of the same size bucket and other things get simpler and faster.
For example, with fixed-size JSStrings allocating out of a fixed-size allocation bucket, perhaps thread-local, you can allocate a string by atomically incrementing a pointer. Speedy!
Re:Slimmer and faster JavaScript strings in Firefox
by pcwalton on Monday July 21, 2014 @09:47PM
(#8067075)
In general Firefox uses jemalloc, so shipping builds of SpiderMonkey do as well. However, individual JavaScript objects are allocated using a custom SpiderMonkey-specific allocator that uses fixed-size bins, knows about garbage collection information, and (as of this year) supports multiple generations. No production-quality JavaScript engine uses the C malloc interface for individual JS objects; you'd get killed in the benchmarks if you tried.
Re:Slimmer and faster JavaScript strings in Firefox
by quotemstr on Monday July 21, 2014 @09:19PM
(#8067007)
Remember that malloc is a least-common-denominator interface. It needs to work everywhere. In a lot of places, it could be hard to justify the complexity.
Anyway, on glibc systems, you can use malloc_usable_size. On Windows systems (Windows gets the heap very right), you can use the HeapSize function.
I wonder what the reason is for this roundabout way of doing it - couldn't the whole string be stored as a variable-length block (header with length, and then the content bytes), all on one heap? Incidentally this is also one of the things I think is broken about the malloc() interface; there is no portable way to get the size of an allocated block with only a pointer to it, despite that information being available somewhere - free() has to know, after all. Thus to do it the "correct, portable" way you have to end up essentially duplicating that length somewhere else. The fact that people are getting told that it's not something they need to know (e.g. http://stackoverflow.com/questions/5451104/how-to-get-memory... ) doesn't help either.
I've written a "nonportable" (in reality, all that would be needed is to change the function that gets the length from the block header) string implementation that uses this technique, and it definitely works well.
Some operations like eval currently inflate Latin1 to a temporary TwoByte buffer, because the parser still works on TwoByte strings and making it work on Latin1 would be a pretty big change.
I haven't looked at their code but if the parser expects the whole string to be available and accesses it randomly it would certainly be a big rewrite; otherwise, if it's more like a getchar(), it wouldn't be so hard to have a function expand each character from the source string as the parser consumes it.
The main goal was saving memory, but Latin1 strings also improved performance on several benchmarks.
With modern processors having multilevel cache hierarchies and increasing memory latencies, smaller almost always is faster - it's well worth spending a few extra cycles in the core to avoid the few hundred cycles (or more) of a cache miss.