Skip to content

Reduce time profiler memory usage with lazy profile tree#287

Open
IlyasShabi wants to merge 8 commits intomainfrom
ishabi/lazy-time-profiler
Open

Reduce time profiler memory usage with lazy profile tree#287
IlyasShabi wants to merge 8 commits intomainfrom
ishabi/lazy-time-profiler

Conversation

@IlyasShabi
Copy link

@IlyasShabi IlyasShabi commented Mar 2, 2026

What does this PR do?:
Introduces a new stopAndCollect function for the time profiler that reduces memory usage during profile serialization. Instead of materializing the entire V8 CPU profile tree as JS objects upfront, the new API resolves children on demand using getters.

This also moves totalHitCount() computation to C++ instead of TS to avoid getting children twice from C++

Motivation:
The current stop() API recursively translates the entire profile tree into JS objects, creating a full copy in heap memory which can be significant for large applications.
The new stopAndCollect() API uses a callback to keep the V8 profile alive while the TS serializer traverses the tree lazily.
Scalar properties are set at creation time, but children are resolved on first access, reducing peak memory usage.

Additional Notes:

  • Similar to heap profiling
  • Benchmark on CI shows:
* ~99% on initial load which normal since we are not materializing the profiler to JS objects.
* ~52% after traversing the profiler tree to explicitly call getters.
* ~58% after calling afterHitCount() which is moved to c++.

How to test the change?:
Unit tests

@IlyasShabi IlyasShabi added the semver-patch Bug or security fixes, mainly label Mar 2, 2026
@github-actions
Copy link

github-actions bot commented Mar 2, 2026

Overall package size

Self size: 1.99 MB
Deduped: 2.34 MB
No deduping: 2.34 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | source-map | 0.7.6 | 185.63 kB | 185.63 kB | | pprof-format | 2.2.1 | 163.06 kB | 163.06 kB | | node-gyp-build | 3.9.0 | 8.81 kB | 8.81 kB |

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

@IlyasShabi IlyasShabi force-pushed the ishabi/lazy-time-profiler branch from 216d01f to 0d480d5 Compare March 3, 2026 14:45
@pr-commenter
Copy link

pr-commenter bot commented Mar 3, 2026

Benchmarks

Benchmark execution time: 2026-03-05 10:33:13

Comparing candidate commit 3ed4376 in PR branch ishabi/lazy-time-profiler with baseline commit 2db9741 in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 91 metrics, 29 unstable metrics.

@IlyasShabi IlyasShabi force-pushed the ishabi/lazy-time-profiler branch from 9881a82 to 0e5f1dc Compare March 3, 2026 19:10
@IlyasShabi IlyasShabi force-pushed the ishabi/lazy-time-profiler branch 6 times, most recently from 7ee93d7 to b617efb Compare March 4, 2026 15:02
@IlyasShabi IlyasShabi force-pushed the ishabi/lazy-time-profiler branch from b617efb to c4d4158 Compare March 4, 2026 19:58
@IlyasShabi IlyasShabi marked this pull request as ready for review March 4, 2026 21:05
@IlyasShabi
Copy link
Author

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3a567e03d1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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.

1 participant