[Design/Perf] C++ fast path for Array.prototype.flat (2-pass, V8)

12 views
Skip to first unread message

Amemiya Riya

unread,
Jan 28, 2026, 4:33:32 AM (yesterday) Jan 28
to v8-dev
Hi V8 team,

I’m working on a patch to add a C++ fast path for Array.prototype.flat
(2-pass: precompute output length, preallocate, then write directly).
This is modeled after JSC’s C++ implementation but adapted for V8’s
elements/protector model.

Summary of changes:
- Add runtime fast path for Array.prototype.flat
- Preconditions: FastJSArray + NoElements/ArraySpecies protectors intact
- Proxy/accessor/custom elements bail out to existing slow path
- Recursive flatten only when elements are JSArray; otherwise bail
- If length exceeds 2^53-1, throw TypeError (same as spec)

Why:
- Current implementation writes element-by-element via property creation.
- For large arrays / deep nesting, we can significantly reduce overhead.

Correctness:
- holes are skipped (no property), consistent with spec
- fallback is always used when protos/Proxy/accessors exist

Performance (single-run huge array):
- input: outer=20000, chunk=1024 (output length 20,480,000)
- d8 fast (with patch): 58ms
- d8 slow (NoElements invalidated): 924ms
- speedup: ~16x

Tests run:
- mjsunit/harmony/array-flat
- mjsunit/harmony/array-flatMap
- mjsunit/harmony/array-flat-species
- mjsunit/harmony/array-flatMap-species
- (additional large test run completed)

I can share the patch and more detailed numbers if helpful.
Is this direction acceptable, or would you recommend any constraints or
additional checks?

Thanks!

Leszek Swirski

unread,
Jan 28, 2026, 6:59:46 AM (yesterday) Jan 28
to v8-...@googlegroups.com
Hi Amemiya,

This sounds like a reasonable approach, fast-paths for a valid NoElements protector is a pattern we use elsewhere so it makes sense to use it for Array.prototype.flat too. Please share your patch on Gerrit and we can discuss details there, you're welcome to put me as a reviewer.

Cheers,
Leszek

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/v8-dev/fa39a8c3-7aca-4c29-ac4b-bd34e70d44b6n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages