Commit 683c2b7
committed
perf(prefix-source): implement buffer() that beats the Source default by ~17%
Walks the underlying source's buffer directly instead of going through
this.source() + Buffer.from. Skips both the regex replace and the
intermediate prefixed-string allocation.
Key trick that beats the V8 default: skip the count pass. Allocate
worst-case `prefixLen + contentLen + prefixLen * contentLen` with
allocUnsafe (cheap, no init), fill in one pass, return result.subarray.
For typical short prefixes ("\t", " ") the worst case is bounded by
~2x and the subarray retains the over-allocation cheaply. For long
prefixes the >50% waste threshold triggers a copy into a tight Buffer
so the extra capacity gets GC'd.
No caching — _source can be mutable (ReplaceSource etc.) and the
result must reflect changes on each call. The existing
"reflect-mutations-to-underlying-source" regression test still passes.
A/B/A median over 5 alternating runs (es6-promise.js fixture, 10
calls per task body):
baseline: 1130 ops/s
this: 1322 ops/s (+17%)
Earlier attempts (committed and reverted on this branch) — for the
record so the next person doesn't redo them:
- Buffer.concat(this.buffers()): array-iteration overhead -> slower
- count-pass + exact alloc + copy: 2x indexOf walks -> slower
- cache the result: faster but unsafe with mutable child
https://claude.ai/code/session_01EHhGq9PRFRGefVtwwasCqZ1 parent 9a37395 commit 683c2b7
1 file changed
Lines changed: 50 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
72 | 114 | | |
73 | 115 | | |
74 | 116 | | |
75 | 117 | | |
76 | 118 | | |
77 | | - | |
| 119 | + | |
78 | 120 | | |
79 | 121 | | |
80 | 122 | | |
| |||
0 commit comments