- V8's efforts to improve JavaScript performance: Constantly striving to enhance JavaScript performance. Recently revisited the JetStream2 benchmark suite to eliminate performance cliffs. A specific optimization in the
async-fs
benchmark yielded a significant2.5x
improvement and contributed to an overall score boost. - The target
async-fs
andMath.random
:async-fs
is a JavaScript file system implementation focusing on asynchronous operations with a performance bottleneck in the customMath.random
implementation. Theseed
variable is updated on each call to generate the pseudo-random sequence and is stored in aScriptContext
.ScriptContext
differentiates betweenSMI
andHeapNumber
storage based on tagging. - The bottleneck: Profiling
Math.random
revealed two major issues -HeapNumber
allocation due to pointing to a standard immutableHeapNumber
and using slower floating-point instructions despite integer operations becauseseed
is stored as a genericHeapNumber
. - The solution: Implemented a two-part optimization - slot type tracking with mutable heap number slots and mutable heap
Int32
. The former eliminates the need for newHeapNumber
allocation on updates, and the latter allows the compiler to generate optimized integer instructions when it knows the slot contains anInt32
. These optimizations introduce a code dependency on the slot type. - The results: The changes significantly speed up the
Math.random
function with no allocation and fast in-place updates, and the compiler can generate optimized integer instructions. This led to a~2.5x
speedup in theasync-fs
benchmark and a~1.6%
improvement in the overall JetStream2 score, showing that small targeted optimizations can have a big impact.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。