perf(array): add dense fast path to Array.prototype.push#5300
perf(array): add dense fast path to Array.prototype.push#5300tkshsbcue wants to merge 1 commit intoboa-dev:mainfrom
Conversation
Test262 conformance changes
Broken tests (3):Tested main commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5300 +/- ##
===========================================
+ Coverage 47.24% 59.64% +12.40%
===========================================
Files 476 589 +113
Lines 46892 63588 +16696
===========================================
+ Hits 22154 37930 +15776
- Misses 24738 25658 +920 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
447ebbe to
39e6041
Compare
Array.prototype.push was going through the generic [[Set]] machinery for every element, which involves property descriptor validation and prototype chain walks. This is unnecessary for dense arrays where we can append directly to the indexed storage. Add a fast path that mirrors the PushValueToArray opcode: verify the array is extensible and its shape matches the default array template (guaranteeing length is writable), then push directly via push_dense() and update the length in-place. Falls through to the existing slow path for sparse arrays, non-extensible arrays, arrays with modified property descriptors, and array-like objects. ~49% improvement on a push-dominated microbenchmark, ~20% on a realistic mixed workload (push objects + filter).
39e6041 to
372c580
Compare
|
the failing tests hint on skipped checks (similar to #5076) |
|
You're right @zhuzhu81998 same class of bug as #5076. The shape check only verifies the array's own shape matches the default template, but doesn't guard against indexed property setters on the prototype chain. So when someone does I think the most practical fix is to extend the guard to also check that let array_proto_shape = context
.intrinsics()
.constructors()
.array()
.prototype();
// bail if Array.prototype or Object.prototype have been modified
// with indexed propertiesThe more robust long-term approach would be a realm-level "no indexed accessors on prototypes" flag (like V8's protector cells), but checking prototype shapes should be sufficient here and keeps the PR scoped. I'll update the PR with the fix. |
perf(array): add dense fast path to Array.prototype.push
Description
Array.prototype.pushgoes through the generic[[Set]]path for every element — converting the index to aPropertyKey, cloning the receiver, calling__get_own_property__, then__define_own_property__with full descriptor validation. For dense arrays, none of this is necessary.The
PushValueToArrayopcode (used for array literals like[1, 2, 3]) already has a fast path that appends directly to dense indexed storage viapush_dense(). This PR applies the same pattern to theArray.prototype.pushbuiltin.How it works
Before entering the generic
[[Set]]loop, check if the object is an extensible array with dense storage. If so, push each element directly viapush_dense()and update the length in-place.push_dense()handles type transitions (DenseI32 → DenseF64 → DenseElement) internally and only returnsfalsefor already-sparse storage, so the first push serves as a probe — if it succeeds, all subsequent pushes are guaranteed to succeed.The fast path is skipped for:
Object.preventExtensions,Object.freeze,Object.seal)The slow path is completely untouched.
Benchmark results
Criterion microbenchmark (200 iterations × 5000 integer pushes):
This is a push-dominated benchmark. On a more realistic workload (pushing objects + filtering), the improvement is smaller since push is no longer the bottleneck:
Improvement ranges from ~20% (mixed workloads) to ~49% (push-dominated) depending on how much time is spent in push.