Skip to content

Performance: keep the time origin per environment - #240

Open
matthargett wants to merge 2 commits into
BabylonJS:mainfrom
rebeckerspecialties:performance-per-env-time-origin
Open

matthargett wants to merge 2 commits into
BabylonJS:mainfrom
rebeckerspecialties:performance-per-env-time-origin

Conversation

@matthargett

@matthargett matthargett commented Sep 14, 2026

Copy link
Copy Markdown

performance.now() measured from a single process-wide start time that every Performance::Initialize rewrote. Any host that runs more than one AppRuntime (a second runtime, or a worker) therefore moves the first realm's clock backwards on each initialization, and with concurrent runtimes the write is a data race (ThreadSanitizer reports it once workers exist).

Each realm has its own time origin on the web, so capture it per environment in the now() closure, and measure with steady_clock, which cannot step.

Verified on macOS (JavaScriptCore): full UnitTests green. Fork twin: rebeckerspecialties#25.

performance.now() measured from one process-wide start time that every
Initialize() rewrote. With Workers each runtime initializes the polyfill
on its own thread, so ThreadSanitizer reports the write race, and every
new worker also moved the main realm's clock backwards. Capture the
origin per environment in the now() closure, as each realm has its own
time origin on the web, and use steady_clock, which cannot step.

(cherry picked from commit 2391b0d)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation resolves the timing isolation issue; the remaining regression-test request is a minor, non-blocking enhancement.

Pull request overview

Updates performance.now() to maintain an independent, monotonic time origin for each runtime environment.

Changes:

  • Replaces process-wide timing state with per-environment closure state.
  • Uses std::chrono::steady_clock for elapsed-time measurement.
File summaries
File Summary
Polyfills/Performance/Source/Performance.cpp Implements per-environment monotonic timing.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread Polyfills/Performance/Source/Performance.cpp
Two runtimes: after the second initializes the polyfill, the first
one's performance.now() must not go backwards. Fails against the
process-wide origin (the second Initialize reset it to zero).
@matthargett

Copy link
Copy Markdown
Author

Regression test added in 5c6cd00 (fails against the previous implementation: after 0.02 ms vs before 55 ms). Fork twin rebeckerspecialties#25 at that head: 24/24 green — https://github.com/rebeckerspecialties/JsRuntimeHost/actions/runs/34841426895

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants