用可变堆数字对 V8 进行涡轮增压 · V8

  • 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 significant 2.5x improvement and contributed to an overall score boost.
  • The target async-fs and Math.random: async-fs is a JavaScript file system implementation focusing on asynchronous operations with a performance bottleneck in the custom Math.random implementation. The seed variable is updated on each call to generate the pseudo-random sequence and is stored in a ScriptContext. ScriptContext differentiates between SMI and HeapNumber storage based on tagging.
  • The bottleneck: Profiling Math.random revealed two major issues - HeapNumber allocation due to pointing to a standard immutable HeapNumber and using slower floating-point instructions despite integer operations because seed is stored as a generic HeapNumber.
  • 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 new HeapNumber allocation on updates, and the latter allows the compiler to generate optimized integer instructions when it knows the slot contains an Int32. 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 the async-fs benchmark and a ~1.6% improvement in the overall JetStream2 score, showing that small targeted optimizations can have a big impact.
阅读 9
0 条评论