Skip to content

perf: reuse V8 template for wall profile context objects - #408

Merged
IlyasShabi merged 1 commit into
mainfrom
ishabi/dictionary-template-optimization
Sep 11, 2026
Merged

perf: reuse V8 template for wall profile context objects#408
IlyasShabi merged 1 commit into
mainfrom
ishabi/dictionary-template-optimization

Conversation

@IlyasShabi

Copy link
Copy Markdown

Summary

V8’s DictionaryTemplate optimizes repeated construction of JS objects with a fixed set of properties. It creates the template once and lets V8 reuse the same object layout for subsequent objects.

During wall-profile collection, we materialize one JS context object for every matched sample. Previously, each object was created empty and populated using up to 4 individual property writes. This adds overhead on both the C++ and V8 sides through repeated property operations and object-shape transitions.

This PR caches a DictionaryTemplate per V8 isolate and uses it to construct these context objects directly with the expected layout.

Benchmark

  • Runs wall profiling with contexts, CPU time, and async IDs enabled.
  • Measures the native TimeProfiler.stop() phase with process.hrtime.bigint().
  • Divides the elapsed time by the number of context objects created.

Time per object on main: 379 ns
Time per object on this PR: 185 ns

This reduces construction cost by more than 50%. The generated objects also go from three V8 object shapes to one consistent shape.

DictionaryTemplate is available starting with Node.js 22

There are other fixed-shape objects in the time and heap profilers that may benefit from the same optimization. I can address those in follow-up PRs if this approach looks good.

@IlyasShabi IlyasShabi added the semver-patch Bug or security fixes, mainly label Sep 10, 2026
@github-actions

Copy link
Copy Markdown

Overall package size

Self size: 2.62 MB
Deduped: 3.32 MB
No deduping: 3.32 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | pprof-format | 2.3.1 | 504.33 kB | 504.33 kB | | source-map | 0.8.0 | 185.66 kB | 185.66 kB | | node-gyp-build | 4.8.4 | 13.86 kB | 13.86 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Sep 10, 2026

Copy link
Copy Markdown

Pipelines

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 176e4e5 | Docs | View more details | Give us feedback!

@IlyasShabi
IlyasShabi force-pushed the ishabi/dictionary-template-optimization branch from 425ea51 to 176e4e5 Compare September 10, 2026 09:10
@IlyasShabi
IlyasShabi marked this pull request as ready for review September 10, 2026 12:49

@szegedi szegedi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

I guess the maybe obvious next place to subject to this treatment is the tree builder as the nodes are all also identical dictionaries

@IlyasShabi
IlyasShabi merged commit 38c5d3a into main Sep 11, 2026
179 of 186 checks passed
@IlyasShabi
IlyasShabi deleted the ishabi/dictionary-template-optimization branch September 11, 2026 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver-patch Bug or security fixes, mainly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants