Repository navigation
Implement performance.now() and timeOrigin in TypeScript - #7969
Conversation
ddce4ce to
32ba030
Compare
|
This pull request's reviews are posted as pull request reviews: see the review of |
32ba030 to
cfd9172
Compare
There was a problem hiding this comment.
Superseded by the review of
464b886e.
This review, of cfd91727
💬 Approved with comments — Nothing blocks and there are no warnings. Two INFO notes from the last review still apply: the native wiring runs even with the autogate off, and the gated swap can be observed, which the repository's compatibility rules ask to be recorded as an accepted risk.
Findings: 2 info
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 2 info
Nothing new in the changed code; two INFO notes from the last review still apply.
Recommendation: 💬 Optional Nits
- [INFO] createUtilsObject() always resolves Performance's template and tag range and builds both signature-bound natives, even with the typescript-performance-now autogate off. It is safe today, but an isolate type without Performance would fail at bootstrap even with the gate off. (
src/workerd/api/per-isolate-bootstrap.c++:339) - [INFO] Under the gate, toString() shows TS source and
newon the timeOrigin getter throws a different TypeError message. Both are documented in AGENTS.md. Under the repository's compatibility rules, the decision to accept them should be recorded in the commit message. (src/per_isolate/performance/now.ts:72)
View full review
The new TODO(cleanup) on install() in now.ts matches what the inline discussion asked for and adds no defect.
Two INFO notes still apply because their code has not changed:
- The native wiring runs whatever the gate says (
src/workerd/api/per-isolate-bootstrap.c++:339).createUtilsObject()always resolves Performance's template and tag range and builds the two signature-bound natives. Only the TS install inmain.tschecks thetypescript-performance-nowautogate. For an unregistered type,getDynamicTypeInfofails withKJ_FAIL_REQUIRE. That is safe today, because every bootstrapped isolate registersPerformance, but an isolate type without it would fail at bootstrap even with the gate off. Build these natives only when the gate is on, or add a comment that records the dependency. - The swap can be observed (
src/per_isolate/performance/now.ts:72). Under the gate,Function.prototype.toString()shows TS source instead of[native code], andnewon thetimeOrigingetter throws a different TypeError message. Both are documented inperformance/AGENTS.md. The repository's backward-compatibility rules say autogates do not preserve existing Workers' API surface, and that the decision to accept a change like this belongs in the commit message.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security issues found. A v8::Signature for Performance's template checks the receiver before the native runs. The receiver is then unwrapped against Performance's tag range with unwrapFromShimInRangeOrAbort().
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: cfd91727
cfd9172 to
464b886
Compare
There was a problem hiding this comment.
Superseded by the review of
4862acb4.
This review, of 464b886e
✅ Approved — No findings. The natives are now built only when the autogate is on, and the observable differences are documented and recorded as an accepted risk.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The natives are built with a v8::Signature for Performance's template, so V8 rejects a wrong receiver before the native runs. unwrapFromShimInRangeOrAbort() then checks the tag range. The clock stays native, so the Spectre-safe time and its clamping are unchanged.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
createUtilsObject() (src/workerd/api/per-isolate-bootstrap.c++) now builds the Performance natives only under util::Autogate::isEnabled(util::AutogateKey::TYPESCRIPT_PERFORMANCE_NOW). That matches the gate check in src/per_isolate/main.ts. The observable differences under the gate are documented in performance/AGENTS.md. The new jsg::Lock virtuals, the TimerTest autogate plumbing and the benchmark addition look correct. The same test file runs against both implementations, and a canary checks that the TypeScript version is installed.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 464b886e
There was a problem hiding this comment.
Superseded by the review of
7198b146.
This review, of 4862acb4
✅ Approved — No findings. The changes since the last review (the merge of main and runtime-read compat flags in the test) introduce no issues, and the requested TODO is in place.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
Both natives carry a v8::Signature for Performance's template, so V8 rejects a wrong receiver before the native runs, and the receiver is then unwrapped against Performance's tag range. The time still comes from native Performance::now(), so the Spectre-safe time and its clamping are unchanged. The natives are built only when the TYPESCRIPT_PERFORMANCE_NOW autogate is on.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
No code quality issues found.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 4862acb4
There was a problem hiding this comment.
Superseded by the review of
66a40060.
This review, of 7198b146
✅ Approved — No findings. The changes since the last review (the merge of main and runtime-read compat flags in the test) introduce no issues, and the requested TODO is in place.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
Both natives carry a v8::Signature for Performance's template, so V8 rejects a wrong receiver before the native runs, and the receiver is then unwrapped against Performance's tag range. The time still comes from native Performance::now(), so the Spectre-safe time and its clamping are unchanged. The natives are built only when the TYPESCRIPT_PERFORMANCE_NOW autogate is on.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
No code quality issues found.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 7198b146 — same changes as 4862acb4, so this is that commit's review (no new review was run; line numbers are as of 4862acb4)
There was a problem hiding this comment.
Superseded by the review of
409824e2.
This review, of 66a40060
✅ Approved — No findings. Since the last review, the only changes are the merge of main into four files, and they introduce no issues. The TODO(cleanup) on install() that was asked for is in place.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Receiver checks: V8 still rejects a wrong receiver through the
v8::Signaturebefore the native runs. The receiver is then unwrapped against Performance's tag range. - Clock: The time still comes from the native
Performance::now(), so the Spectre-safe clamping is unchanged.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
Since the last review, only src/per_isolate/AGENTS.md, src/per_isolate/main.ts, src/workerd/api/per-isolate-bootstrap.c++ and src/workerd/util/autogate.h changed, all from merging main. The merged code introduces no regressions.
- The Performance natives are still built only when the
TYPESCRIPT_PERFORMANCE_NOWautogate is on. - Both natives carry a
v8::Signaturefor Performance's template. getTemplateForandgetWrappableTagRangeForuse the samegetDynamicTypeInfopath asgetPrototypeFor.- The
TODO(cleanup)oninstall()that @jasnell requested is innow.ts.
⚡ Performance: ✅ LGTM
- Hot path:
now()makes one fast-API call. The tag range is passed as the function's data, so there is no type lookup on each call. - Setup cost: The natives are built once per isolate, and only when the autogate is on.
- Warm-up: The description reports slower reads before TurboFan optimizes
now(). That is a one-time interpreter cost.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 66a40060
There was a problem hiding this comment.
Superseded by the review of
f056e254.
This review, of 409824e2
✅ Approved — No findings. Since the last review, the only changes are the merge of main into four files, and they introduce no issues. The TODO(cleanup) on install() that was asked for is in place.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Receiver checks: V8 still rejects a wrong receiver through the
v8::Signaturebefore the native runs. The receiver is then unwrapped against Performance's tag range. - Clock: The time still comes from the native
Performance::now(), so the Spectre-safe clamping is unchanged.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
Since the last review, only src/per_isolate/AGENTS.md, src/per_isolate/main.ts, src/workerd/api/per-isolate-bootstrap.c++ and src/workerd/util/autogate.h changed, all from merging main. The merged code introduces no regressions.
- The Performance natives are still built only when the
TYPESCRIPT_PERFORMANCE_NOWautogate is on. - Both natives carry a
v8::Signaturefor Performance's template. getTemplateForandgetWrappableTagRangeForuse the samegetDynamicTypeInfopath asgetPrototypeFor.- The
TODO(cleanup)oninstall()that @jasnell requested is innow.ts.
⚡ Performance: ✅ LGTM
- Hot path:
now()makes one fast-API call. The tag range is passed as the function's data, so there is no type lookup on each call. - Setup cost: The natives are built once per isolate, and only when the autogate is on.
- Warm-up: The description reports slower reads before TurboFan optimizes
now(). That is a one-time interpreter cost.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 409824e2 — same changes as 66a40060, so this is that commit's review (no new review was run; line numbers are as of 66a40060)
There was a problem hiding this comment.
Superseded by the review of
aedbaa17.
This review, of f056e254
✅ Approved — No findings. The changes since the last review are the consistent autogate rename and merges from main.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Receiver checks: The
v8::Signaturereceiver check is unchanged. - Clock: Time still comes from the Spectre-safe native clock.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
- Autogate rename:
TS_PERFORMANCE_NOWis used consistently in C++, TypeScript, the test config and the benchmark. No references to the old name remain. - New
jsg::Lockmethods:getTemplateForandgetWrappableTagRangeForhave their single implementation insetup.h. - Autogate off: The Performance natives are still not built when the autogate is off.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: f056e254
f056e25 to
aedbaa1
Compare
There was a problem hiding this comment.
Superseded by the review of
94cf1184.
This review, of aedbaa17
✅ Approved — No findings. The receiver checks, the native clock and the autogate gating are unchanged, and the requested cleanup TODO is in place.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Receiver check: the
v8::Signaturefor the Performance template rejects foreign receivers, includingObject.create(performance), on both the slow and the fast-API paths. - Clock:
now()still goes through the Spectre-safePerformance::now(). - Tag range: it is fixed at bootstrap, and user code cannot change it.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
- Autogate name:
TS_PERFORMANCE_NOWis used consistently across the C++, the TypeScript, the wd-test config and the benchmark. - Natives behind the autogate: they are built only when the autogate is on.
- New
jsg::Lockmethods:getTemplateForandgetWrappableTagRangeForboth get their result fromgetDynamicTypeInfo.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: aedbaa17
There was a problem hiding this comment.
Superseded by the review of
3057ccf6.
This review, of 94cf1184
✅ Approved — No findings. The receiver checks, the native clock and the autogate gating are unchanged, and the requested cleanup TODO is in place.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Receiver check: the
v8::Signaturefor the Performance template rejects foreign receivers, includingObject.create(performance), on both the slow and the fast-API paths. - Clock:
now()still goes through the Spectre-safePerformance::now(). - Tag range: it is fixed at bootstrap, and user code cannot change it.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
- Autogate name:
TS_PERFORMANCE_NOWis used consistently across the C++, the TypeScript, the wd-test config and the benchmark. - Natives behind the autogate: they are built only when the autogate is on.
- New
jsg::Lockmethods:getTemplateForandgetWrappableTagRangeForboth get their result fromgetDynamicTypeInfo.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 94cf1184 — same changes as aedbaa17, so this is that commit's review (no new review was run; line numbers are as of aedbaa17)
Return the per-isolate FunctionTemplate and the CppHeapPointerTag range of a JSG_RESOURCE_TYPE, alongside the existing getPrototypeFor<T>(). Native functions created outside the resource type system can use the template to build a v8::Signature, so that V8 applies the same exact receiver check that JSG applies to the type's own methods, and the tag range to unwrap the receiver with Wrappable::unwrapFromShimInRangeOrAbort(), as JSG's own dispatch does. Both depend on the isolate's TypeWrapper, which such code cannot see.
Under the new ts-performance-now autogate, the per-isolate bootstrap
replaces the JSG now() and timeOrigin getter on Performance.prototype with
TypeScript. Replacing them on the prototype reaches globalThis.performance,
node:perf_hooks and the Performance class alike.
The clock stays native. utils.performanceNow() calls Performance::now(), so
the value is still the IoContext's Spectre-safe time (event-loop time,
clamped inside input-gate critical sections, monotonic, 0 outside a
request, and precise_timers' 3 ms rounding), and the
markPerfEvent("performance_now") accounting is kept. timeOrigin stays the
constant 0, behind utils.requirePerformance(), which exists only for its
receiver check.
Each member crosses into C++ exactly once, as a V8 fast-API call. Both
natives take the Performance as `this` and carry a v8::Signature for its
template, so V8 applies JSG's exact receiver check (Object.create(performance)
is rejected) with JSG's "Illegal invocation" TypeError. TurboFan does the
check statically when it knows the receiver's map, as it does for
`performance.now()`. performanceNow() then unwraps the receiver with
unwrapFromShimInRangeOrAbort() against Performance's tag range, like JSG's
dispatch; the range is looked up once per context and passed as the
function's data. utils.getPerformancePrototype() reaches the prototype
without allocating the lazy `performance` singleton, and without
globalThis.Performance, which exists only under the perf compat flags.
The install is gated by an autogate rather than a compat flag, because the
swap is meant to be unobservable. It copies every descriptor attribute and
each function's name and length from the JSG originals, both of which vary
with spec_compliant_property_attributes. The natives behind utils are built
only when the autogate is on, so with it off the bootstrap neither depends
on Performance nor pays for the template and tag-range lookups.
Compatibility decision: no compat flag; the risk of two observable
divergences is accepted. Under the autogate, Function.prototype.toString()
on now and the timeOrigin getter shows the TypeScript source rather than
"[native code]", and `new` on the timeOrigin getter throws "is not a
constructor" rather than "Illegal invocation" (both TypeErrors). Code that
depends on either is not expected to exist; both are documented in
src/per_isolate/performance/AGENTS.md. The API surface is otherwise
unchanged.
performance-now-test.js runs unmodified against both implementations,
covering descriptor shape, receiver checks, values, node:perf_hooks, and
optimized callers that reach the fast path. A canary checks that the
TypeScript version is installed.
bench-timer-clock gains clockReadsWithBlockedTimersTypeScript, which runs the same clock-read body with the ts-performance-now autogate on, and so measures the TypeScript performance.now() against the JSG one. TimerTest takes an optional list of autogates to support it. On an M-series Mac under load, the TypeScript version reads at 26.7M/s against JSG's 32.8M/s with the benchmark's 10k reads per fresh isolate, which includes the time before TurboFan optimizes the TypeScript member. With 1M reads per isolate it reads at 76.4M/s against 67.4M/s: once optimized, the member is inlined and the signature check is done statically.
performance-now-test.js took spec_compliant_property_attributes and enable_nodejs_perf_hooks_module from per-service JSON bindings, which describe the flags a service lists rather than the flags it runs with. Under @all-compat-flags they disagree: nodejs_compat implies the perf-hooks flag, so two of the three services skipped the node:perf_hooks route check, and once spec_compliant_property_attributes gets an enable date the getter name check would fail in the services that don't list it. Read both from Cloudflare.compatibilityFlags instead, and drop the bindings.
94cf118 to
3057ccf
Compare
There was a problem hiding this comment.
✅ Approved — No findings. The receiver checks, the native clock and the autogate gating are unchanged, and the requested cleanup TODO is in place.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
- Receiver check: the
v8::Signaturefor the Performance template rejects foreign receivers, includingObject.create(performance), on both the slow and the fast-API paths. - Clock:
now()still goes through the Spectre-safePerformance::now(). - Tag range: it is fixed at bootstrap, and user code cannot change it.
📖 AGENTS.md: ✅ LGTM
No AGENTS.md issues found.
📋 Code Quality: ✅ LGTM
- Autogate name:
TS_PERFORMANCE_NOWis used consistently across the C++, the TypeScript, the wd-test config and the benchmark. - Natives behind the autogate: they are built only when the autogate is on.
- New
jsg::Lockmethods:getTemplateForandgetWrappableTagRangeForboth get their result fromgetDynamicTypeInfo.
⚡ Performance: ✅ LGTM
No performance issues found.
📝 Documentation: ✅ LGTM
No documentation issues found.
Reviewed commit: 3057ccf6 — same changes as aedbaa17, so this is that commit's review (no new review was run; line numbers are as of aedbaa17)
This moves
performance.now()and thetimeOrigingetter from C++ JSGto TypeScript in the per-isolate bootstrap, next to
URL,URLPatternand
structuredClone(), so that the JSG members can be deleted once theTS_PERFORMANCE_NOWautogate is removed. Only the two members onPerformance.prototypeare replaced, which coversglobalThis.performance,node:perf_hooksand thePerformanceclassalike. The clock stays native:
now()makes one fast-API call intoPerformance::now(), so it returns the same Spectre-safe time and stillrecords the
performance_nowperf event, andtimeOriginstays 0. Bothnatives carry a
v8::Signaturefor thePerformancetemplate, so V8applies JSG's exact receiver check and its "Illegal invocation" error;
two new
jsg::Lockhelpers give the bootstrap that template and thetype's tag range.
The swap copies every descriptor attribute and each function's name and
length from the JSG originals, which is why it sits behind an autogate
rather than a compatibility flag. Two differences remain, and I've
accepted the risk rather than gating them:
Function.prototype.toString()shows the TypeScript source instead of[native code], andnewon thetimeOrigingetter throws "is not aconstructor" instead of "Illegal invocation". A Worker can observe both,
but both are TypeErrors on paths no code is expected to rely on. In
bench-timer-clock, the TypeScriptnow()is about 12% slower than JSGwith 10k reads per fresh isolate, before TurboFan optimizes it, and
about 6% faster with 1M reads.
performance-now-test.jsruns unchangedagainst both implementations, covering descriptor shape, receiver
checks, values,
node:perf_hooksand optimized callers on the fastpath, and a canary test checks that the autogate installs the TypeScript
version.