Skip to content

Proposal: faster heap and time profile serialization (cache source-mapped locations, build stacks lazily) #438

Closed

Description

@nuzayets

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():

  1. Calls getLocation() for every node, and getLocation() runs sourceMapper.mappingInfo() before it looks up locationIdMap (L205–L236). A frame that appears at many tree nodes is source-mapped once per node.
  2. 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.
  3. In buildLabels, iterates Object.entries(labelSet) (L597), which allocates a [key, value] array per label for every sample.

Proposed changes

  1. Cache locations by the node's unmapped position (scriptId:line:column:name, plus the script name where scriptId is 0), so mappingInfo() runs once per distinct frame. The existing locationIdMap key on the mapped location stays as it is, so ids and output are unchanged.
  2. 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 locationId order and contents are unchanged.
  3. Use Object.keys in buildLabels. 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

Activity

  1. BridgeAR commented on Nov 13, 2026

    @BridgeAR
    Member

    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.

  2. nuzayets commented on Nov 14, 2026

    @nuzayets
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions