Skip to content

Commit e66ab83

Browse files
committed
Update on "compiler: Log metrics on pruned memo blocks/values"
Adds additional information to the CompileSuccess LoggerEvent: * `prunedMemoBlocks` is the number of reactive scopes that were pruned for some reason. * `prunedMemoValues` is the number of unique _values_ produced by those scopes. Both numbers exclude blocks that are just a hook call - ie although we create and prune a scope for eg `useState()`, that's just an artifact of the sequencing of our pipeline. So what this metric is counting is cases of _other_ values that go unmemoized. See the new fixture, which takes advantage of improvements in the snap runner to optionally emit the logger events in the .expect.md file if you include the "logger" pragma in a fixture. [ghstack-poisoned]
1 parent d63a63a commit e66ab83

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ export function compileProgram(
329329
fnName: compiledFn.id?.name ?? null,
330330
memoSlots: compiledFn.memoSlotsUsed,
331331
memoBlocks: compiledFn.memoBlocks,
332+
memoValues: compiledFn.memoValues,
332333
prunedMemoBlocks: compiledFn.prunedMemoBlocks,
333334
prunedMemoValues: compiledFn.prunedMemoValues,
334335
});

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export type CodegenFunction = {
6969
*/
7070
memoBlocks: number;
7171

72+
/**
73+
* Number of memoized values across all reactive scopes
74+
*/
75+
memoValues: number;
76+
7277
/**
7378
* The number of reactive scopes that were created but had to be discarded
7479
* because they contained hook calls.
@@ -297,15 +302,17 @@ function codegenReactiveFunction(
297302
generator: fn.generator,
298303
async: fn.async,
299304
memoSlotsUsed: cx.nextCacheIndex,
300-
memoBlocks: countMemoBlockVisitor.count,
305+
memoBlocks: countMemoBlockVisitor.memoBlocks,
306+
memoValues: countMemoBlockVisitor.memoValues,
301307
prunedMemoBlocks: countMemoBlockVisitor.prunedMemoBlocks,
302308
prunedMemoValues: countMemoBlockVisitor.prunedMemoValues,
303309
});
304310
}
305311

306312
class CountMemoBlockVisitor extends ReactiveFunctionVisitor<void> {
307313
env: Environment;
308-
count: number = 0;
314+
memoBlocks: number = 0;
315+
memoValues: number = 0;
309316
prunedMemoBlocks: number = 0;
310317
prunedMemoValues: number = 0;
311318

@@ -314,9 +321,10 @@ class CountMemoBlockVisitor extends ReactiveFunctionVisitor<void> {
314321
this.env = env;
315322
}
316323

317-
override visitScope(scope: ReactiveScopeBlock, state: void): void {
318-
this.count += 1;
319-
this.traverseScope(scope, state);
324+
override visitScope(scopeBlock: ReactiveScopeBlock, state: void): void {
325+
this.memoBlocks += 1;
326+
this.memoValues += scopeBlock.scope.declarations.size;
327+
this.traverseScope(scopeBlock, state);
320328
}
321329

322330
override visitPrunedScope(

0 commit comments

Comments
 (0)