Skip to content

Commit a404696

Browse files
committed
fix(components): keep defer and change detection counts exact
A repeated defer block at the cap no longer marks the list as cut; only a new block past the cap does. An instance checked while the host cap was full keeps its checks, and they carry over when it gets a slot. Refs #366
1 parent d6d9076 commit a404696

4 files changed

Lines changed: 87 additions & 12 deletions

File tree

‎packages/devtools/src/__tests__/cd-recorder.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,49 @@ describe('change detection recorder', () => {
107107
expect(empty.droppedHosts).toBeUndefined();
108108
});
109109

110+
it("carries an untracked instance's checks over when a slot frees up", () => {
111+
// A WeakRef the test can clear, standing in for garbage collection.
112+
const collected = new WeakSet<object>();
113+
class TestRef<T extends object> {
114+
constructor(private target: T) {}
115+
deref() {
116+
return collected.has(this.target) ? undefined : this.target;
117+
}
118+
}
119+
vi.stubGlobal('WeakRef', TestRef);
120+
try {
121+
const recorder = createCdRecorder({ maxCycles: 10 });
122+
recorder.start();
123+
const check = (instance: object) => {
124+
recorder.onEvent(E.ComponentStart);
125+
recorder.onEvent(E.ComponentEnd, instance);
126+
};
127+
const kept = Array.from({ length: 2000 }, () => new Cart());
128+
const late = new Price();
129+
const other = new Price();
130+
recorder.onEvent(E.ChangeDetectionStart);
131+
for (const instance of kept) check(instance);
132+
check(late);
133+
check(other);
134+
recorder.onEvent(E.ChangeDetectionEnd);
135+
recorder.onEvent(E.ChangeDetectionStart);
136+
check(late);
137+
recorder.onEvent(E.ChangeDetectionEnd);
138+
expect(recorder.snapshot(() => null).droppedHosts).toBe(2);
139+
140+
collected.add(kept[0]!);
141+
recorder.onEvent(E.ChangeDetectionStart);
142+
check(late);
143+
recorder.onEvent(E.ChangeDetectionEnd);
144+
const snap = recorder.snapshot((instance) => (instance === late ? 'late' : null));
145+
// Two checks while untracked, one after it got a slot.
146+
expect(snap.hosts).toEqual({ late: 3 });
147+
expect(snap.droppedHosts).toBe(1);
148+
} finally {
149+
vi.unstubAllGlobals();
150+
}
151+
});
152+
110153
it('reports no dropped classes or hosts below its caps', () => {
111154
const recorder = createCdRecorder({ maxCycles: 10 });
112155
recorder.start();

‎packages/devtools/src/__tests__/defer-blocks.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,32 @@ describe('createDeferTracker', () => {
7575
expect(tracker.truncated).toBe(false);
7676
});
7777

78+
it('does not mark a list cut when the block past the cap repeats a host', () => {
79+
const { root, instances } = page();
80+
const anchors = Array.from({ length: MAX_DEFER_BLOCKS }, () => {
81+
const anchor = document.createComment('container');
82+
root.appendChild(anchor);
83+
return anchor;
84+
});
85+
let extra: Comment | null = null;
86+
const ng: DeferDebugNg = {
87+
getComponent: (el) => instances.get(el) ?? null,
88+
ɵgetControlFlowBlocks: (node) =>
89+
node === root
90+
? [...anchors, ...(extra ? [extra] : []), anchors[0]].map((anchor) =>
91+
block({ hostNode: anchor }),
92+
)
93+
: [],
94+
};
95+
const tracker = createDeferTracker(() => 1000);
96+
expect(tracker.collect(ng)).toHaveLength(MAX_DEFER_BLOCKS);
97+
expect(tracker.truncated).toBe(false);
98+
extra = document.createComment('container');
99+
root.appendChild(extra);
100+
expect(tracker.collect(ng)).toHaveLength(MAX_DEFER_BLOCKS);
101+
expect(tracker.truncated).toBe(true);
102+
});
103+
78104
it('reads defer blocks with their owner, state, triggers and root elements', () => {
79105
const { root, reviews, anchor, lazy, instances } = page();
80106
const section = reviews.querySelector('section')!;

‎packages/devtools/src/cd-recorder.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ export function createCdRecorder(options: {
9292
let perInstance = new WeakMap<object, number>();
9393
let instances: WeakRef<object>[] = [];
9494
let prunedCycle = 0;
95-
// Classes and instances checked past the caps, each counted once.
95+
// Classes and instances checked past the caps, each counted once. An
96+
// untracked instance keeps its checks so they carry over if a slot frees up.
9697
let untrackedClasses = new WeakSet<Ctor>();
9798
let untrackedClassCount = 0;
98-
let untrackedHosts = new WeakSet<object>();
99+
let untrackedHosts = new WeakMap<object, number>();
99100
let untrackedHostCount = 0;
100101

101102
const onEvent = (event: number, instance?: unknown, hook?: unknown) => {
@@ -180,13 +181,17 @@ export function createCdRecorder(options: {
180181
prunedCycle = current.id;
181182
instances = instances.filter((ref) => ref.deref());
182183
}
184+
const earlier = untrackedHosts.get(instance);
183185
if (instances.length < MAX_HOSTS) {
184186
instances.push(new WeakRef(instance));
185-
perInstance.set(instance, 1);
186-
if (untrackedHosts.delete(instance)) untrackedHostCount--;
187-
} else if (!untrackedHosts.has(instance)) {
188-
untrackedHosts.add(instance);
189-
untrackedHostCount++;
187+
perInstance.set(instance, (earlier ?? 0) + 1);
188+
if (earlier !== undefined) {
189+
untrackedHosts.delete(instance);
190+
untrackedHostCount--;
191+
}
192+
} else {
193+
untrackedHosts.set(instance, (earlier ?? 0) + 1);
194+
if (earlier === undefined) untrackedHostCount++;
190195
}
191196
return;
192197
}
@@ -220,7 +225,7 @@ export function createCdRecorder(options: {
220225
prunedCycle = 0;
221226
untrackedClasses = new WeakSet();
222227
untrackedClassCount = 0;
223-
untrackedHosts = new WeakSet();
228+
untrackedHosts = new WeakMap();
224229
untrackedHostCount = 0;
225230
};
226231

‎packages/devtools/src/defer-blocks.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ export function createDeferTracker(now: () => number = Date.now) {
133133
const order: object[] = [];
134134
let position = 0;
135135
for (const [index, raw] of valid.entries()) {
136-
if (out.length >= MAX_DEFER_BLOCKS) {
137-
truncated = true;
138-
return out;
139-
}
140136
const roots: unknown[] = Array.isArray(raw.rootNodes) ? raw.rootNodes : [];
141137
const hostNode = raw.hostNode instanceof Node ? raw.hostNode : anchorOf(roots);
142138
if (hostNode && !(raw.hostNode instanceof Node)) {
@@ -145,6 +141,11 @@ export function createDeferTracker(now: () => number = Date.now) {
145141
const host = hostNode ?? positionKey(root, position++);
146142
order.push(host);
147143
if (hosts.has(host)) continue;
144+
// Only a new block past the cap means some were left out.
145+
if (out.length >= MAX_DEFER_BLOCKS) {
146+
truncated = true;
147+
return out;
148+
}
148149
hosts.add(host);
149150
const hydration = HYDRATION.has(raw.incrementalHydrationState as string)
150151
? (raw.incrementalHydrationState as DeferBlockInfo['hydration'])

0 commit comments

Comments
 (0)