Repository navigation
Conversation
…ch string StringTable encoded every new string into its own Uint8Array as it was added. A profile with thousands of distinct file paths and function names made thousands of small allocations, and every array over V8's 64-byte on-heap typed array limit got its own off-heap backing store, which adds to V8's external memory. Track each string's UTF-8 length instead and write the bytes straight into the output buffer when the table is encoded. Strings read by decode keep their original bytes so re-encoding stays byte-for-byte identical.
|
Friendly ping on this one. The related serializer changes proposed in DataDog/pprof-nodejs#438 have since landed via DataDog/pprof-nodejs#430, so the string table is the remaining piece. On production string tables it cuts encode time from 21.6 ms to 14.3 ms, with byte-identical output. Happy to adjust anything. |
| this.#encodings.push(encoded) | ||
| this.#encodedLength += encoded.length | ||
| const position = this.#add(decodeString(buffer), buffer.length) | ||
| this.#decoded[position] = buffer.slice() |
There was a problem hiding this comment.
If I am not mistaken, this could cause mistakes because slice just creates a reference.
The following would fix that by creating an actual copy and using a fast path for small entries:
| this.#decoded[position] = buffer.slice() | |
| this.#decoded[position] = buffer.length <= 64 || typeof Buffer === 'undefined' | |
| ? new Uint8Array(buffer) | |
| : Buffer.from(buffer) |
| this.#encodings.push(encoded) | ||
| this.#encodedLength += encoded.length | ||
| const position = this.#add(decodeString(buffer), buffer.length) | ||
| this.#decoded[position] = buffer.slice() |
| if (decoded !== undefined) { | ||
| buffer.set(decoded, offset) | ||
| } else if (byteLength > 0) { | ||
| writeUtf8(this.strings[i], offset) |
There was a problem hiding this comment.
Unlike the previous buffer.set(), both Buffer.write() and TextEncoder.encodeInto() can silently write only what fits. Since their return values are ignored, table.encode(new Uint8Array(table.encodedLength - 1)) can now return truncated protobuf output rather than throwing. Could we validate capacity in StringTable.encode(), using the existing prepareBuffer helper, restore the previous throwing behavior, and add a regression test?
|
Superseded by porting this including the fixes for the above. Thank you for the contribution @nuzayets! |
StringTable.dedupencodes every new string into its ownUint8Arrayas it is added. A profile from a large Node service has thousands of distinct strings, mostly long file paths, so building its string table makes thousands of small allocations. Every array over V8's 64-byte on-heap typed array limit also gets its own off-heap backing store, which counts toward V8's external memory and can start a full GC soon after the profile is encoded.This keeps each string's UTF-8 byte length instead and writes the bytes straight into the output buffer when the table is encoded (
Buffer#writein Node,TextEncoder#encodeIntoelsewhere). Strings read bydecodekeep a copy of their original bytes, as they do today, so re-encoding a decoded profile stays byte-for-byte identical, including bytes that aren't valid UTF-8. The public API is unchanged;_encodeStringand_encodeStringFromUtf8are kept.Results
Node 24.22.1. Each run builds and encodes 40 string tables; times are the mean of 30 runs after 5 warm-up runs.
node_modulespaths (script below)--scavenger-max-new-space-capacity-mb=8(the nodejs/node#61967 workaround)In the service those tables came from,
_encodeStringFromUtf8was the largest single frame in the profiler's once-a-minute serialization. For 60 of those profiles,Profile.decode(...).encode()and freshly built tables produce identical bytes before and after, with and withoutBufferavailable.Tests
TextEncoderdoes, andencodedLengthmatches the encoded size.Benchmark script