Repository navigation
Description
I'd like to propose three small changes to ts/src/profile-serializer.ts that cut the main-thread time of serializing heap and wall profiles. Following CONTRIBUTING, I'm opening an issue before sending PRs. I'd especially like to know how to sequence this with #430, which moves the same code into profile-normalizer.ts.
Where the time goes
In a large Node service running dd-trace's profiler with source maps (wall with CPU time, heap, timeline, code hotspots and endpoint collection), the once-a-minute collection stops the event loop for roughly 250 ms. Serializing the heap profile is the biggest part. A typical heap profile tree there has about 20k nodes but only about 2.8k distinct frames. On current main (3c04f56), serialize():
- Calls
getLocation()for every node, andgetLocation()runssourceMapper.mappingInfo()before it looks uplocationIdMap(L205–L236). A frame that appears at many tree nodes is source-mapped once per node. - Copies the whole stack array for every child,
entries.push({node: child, stack: stack.slice()})(L192), even for nodes that have no samples and only need the stack to pass on. - In
buildLabels, iteratesObject.entries(labelSet)(L597), which allocates a[key, value]array per label for every sample.
Proposed changes
- Cache locations by the node's unmapped position (
scriptId:line:column:name, plus the script name wherescriptIdis 0), somappingInfo()runs once per distinct frame. The existinglocationIdMapkey on the mapped location stays as it is, so ids and output are unchanged. - Build a node's stack only when it's needed. Keep a parent link per entry and materialize the location-id array only for nodes that produce samples. The sample's
locationIdorder and contents are unchanged. - Use
Object.keysinbuildLabels. That's one array per sample instead of one per label.
Output stays byte-for-byte identical: same locations, functions, ids, string table and sample order.
Measurements
We have these as local patches against @datadog/pprof 5.15.1 (together with DataDog/pprof-format#121 for the string table).
- Standalone serializer benchmark on heap and time profile trees rebuilt from production profiles, with byte-identical output: heap-profile serialization −30%, time-profile serialization −50%.
- A/B in one warmed-up process. The real service ran under a fixed request mix for 55 minutes, with production profiler settings, alternating unpatched and patched serializers on each collection, warm-up excluded. Per collection:
| Main-thread time per collection (ms) | Before p50 / p90 / max | After p50 / p90 / max |
|---|---|---|
| Heap profile | 167 / 224 / 653 | 126 / 139 / 143 |
| Wall stop | 125 / 136 / 152 | 101 / 110 / 110 |
| Heap profile + wall stop (one uninterrupted pause) | 296 / 376 / 785 | 227 / 242 / 246 |
These figures include the pprof-format string-table change. Most of the heap-profile tail (p90 and max) comes from the source-map caching.
Questions
- Would you take these as one PR or three?
- Should they go in before refactor: extract a format-neutral profile normalizer from the pprof serializer #430, or be rebased onto the normalizer once it lands? I'm happy to do either.
- Is the CLA in CONTRIBUTING.md (Google's) still current for this repo, or is there a Datadog process?
Activity
BridgeAR commentedon Nov 13, 2026
Hey @nuzayets, thank you for the suggestions. Performance improvements are generally good, so we will likely accept any patch for these (without any guarantee). We might adjust the patch a bit before landing, while just reading about the suggestions sound good to me.
nuzayets commentedon Nov 14, 2026
Thanks! #430 landed all three of these in
profile-normalizer.ts: position-keyed frame caching ahead of source mapping, stacks materialized only on first sample (and deduplicated by prefix, which goes further than what I proposed), and allocation-free label iteration. Closing this. The string-table part is still open as DataDog/pprof-format#121.