@@ -52,7 +52,6 @@ export interface TinybenchBench {
5252 teardown : TinybenchHook ;
5353}
5454
55-
5655/**
5756 * The tinybench statistics shape (latency/throughput) shared across the v2 and
5857 * v6 lines. Only the fields the conversion needs are modeled.
@@ -84,78 +83,13 @@ interface InstrumentWindow {
8483 runStart : bigint | null ;
8584}
8685
87- let isBenchAddPatched = false ;
88-
8986/**
9087 * The window bracketing the currently running task's measured loop, driven by
9188 * the setup/teardown hooks below. Tasks run strictly sequentially within a
9289 * worker, so one shared value suffices.
9390 */
9491const instrumentWindow : InstrumentWindow = { runStart : null } ;
9592
96- // tinybench keeps a task's fn and options as `#private` fields (v6+), so we
97- // capture them ourselves when `Bench.add` runs, keyed by bench then task name.
98- // The analysis seam needs the raw fn to run it under its own tight window
99- // instead of tinybench's timing loop.
100- const capturedTasks = new WeakMap < object , Map < string , CapturedTask > > ( ) ;
101-
102- /** The minimal tinybench Bench prototype we patch to capture registrations. */
103- interface TinybenchBenchClass {
104- prototype : {
105- // eslint-disable-next-line @typescript-eslint/no-explicit-any
106- add : ( ...args : any [ ] ) => unknown ;
107- } ;
108- }
109-
110- /**
111- * Patch `Bench.prototype.add` to record each task's fn and options, keyed by
112- * bench then task name. Idempotent, and applied to the prototype so it captures
113- * registrations on every Bench the host constructs.
114- *
115- * `BenchClass` must be the exact class the host instantiates. In tinybench v6 a
116- * task's fn is a true `#private` field — it cannot be read or replaced on the
117- * task afterwards — so capturing (and, for walltime, root-frame-wrapping) has to
118- * happen here, as the fn is registered.
119- *
120- * `registerFn` transforms the fn actually handed to tinybench: identity for
121- * analysis (which runs the captured fn itself), or a root-frame wrap for
122- * walltime (where tinybench drives the fn and the frame must already be baked
123- * in).
124- */
125- export function captureBenchAddOnce (
126- BenchClass : TinybenchBenchClass ,
127- registerFn : ( fn : CapturedTask [ "fn" ] ) => CapturedTask [ "fn" ] ,
128- ) : void {
129- if ( isBenchAddPatched ) {
130- return ;
131- }
132- isBenchAddPatched = true ;
133-
134- const originalAdd = BenchClass . prototype . add ;
135- BenchClass . prototype . add = function (
136- this : object ,
137- name : string ,
138- fn : CapturedTask [ "fn" ] ,
139- fnOpts ?: TinybenchFnOptions ,
140- ) {
141- let byName = capturedTasks . get ( this ) ;
142- if ( ! byName ) {
143- byName = new Map < string , CapturedTask > ( ) ;
144- capturedTasks . set ( this , byName ) ;
145- }
146- byName . set ( name , { fn, fnOpts } ) ;
147- return originalAdd . call ( this , name , registerFn ( fn ) , fnOpts ) ;
148- } ;
149- }
150-
151- /** Retrieve the fn/options captured for a task on a given bench, if any. */
152- export function getCapturedTask (
153- bench : object ,
154- taskName : string ,
155- ) : CapturedTask | undefined {
156- return capturedTasks . get ( bench ) ?. get ( taskName ) ;
157- }
158-
15993/** The tinybench Task prototype whose `run` the legacy seam wraps. */
16094interface TinybenchTaskClass {
16195 // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -193,21 +127,16 @@ export function patchTaskRunOnce(TaskClass: TinybenchTaskClass): void {
193127}
194128
195129/**
196- * The root-frame wrap to hand tinybench at registration time (walltime, v5).
197- * Post-hoc assignment to a task's `fn` is a no-op on tinybench v6 (private
198- * field), so the frame must be baked into the registered fn instead.
130+ * The root-frame wrap to hand tinybench at registration time. Post-hoc
131+ * assignment to a task's `fn` is a no-op on tinybench v6 (private field), so the
132+ * frame must be baked into the registered fn instead.
199133 */
200134export function rootFrameRegisterFn (
201135 fn : CapturedTask [ "fn" ] ,
202136) : CapturedTask [ "fn" ] {
203137 return wrapWithRootFrame ( ( ) => fn ( ) ) ;
204138}
205139
206- /** Identity registration: analysis runs the captured fn itself, unwrapped. */
207- export function identityRegisterFn ( fn : CapturedTask [ "fn" ] ) : CapturedTask [ "fn" ] {
208- return fn ;
209- }
210-
211140/**
212141 * Run one benchmark under instrumentation, matching the analysis window the
213142 * Vitest 3/4 runner uses exactly: warm the JIT with `optimizeFunction` outside
0 commit comments