Skip to content

Commit a96f483

Browse files
authored
cherry-pick(#33245): fix(trace viewer): make LRUCache per-trace (#33260)
1 parent 8e96d94 commit a96f483

File tree

3 files changed

+61
-36
lines changed

3 files changed

+61
-36
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export class LRUCache<K, V> {
18+
private _maxSize: number;
19+
private _map: Map<K, { value: V, size: number }>;
20+
private _size: number;
21+
22+
constructor(maxSize: number) {
23+
this._maxSize = maxSize;
24+
this._map = new Map();
25+
this._size = 0;
26+
}
27+
28+
getOrCompute(key: K, compute: () => { value: V, size: number }): V {
29+
if (this._map.has(key)) {
30+
const result = this._map.get(key)!;
31+
// reinserting makes this the least recently used entry
32+
this._map.delete(key);
33+
this._map.set(key, result);
34+
return result.value;
35+
}
36+
37+
const result = compute();
38+
39+
while (this._map.size && this._size + result.size > this._maxSize) {
40+
const [firstKey, firstValue] = this._map.entries().next().value;
41+
this._size -= firstValue.size;
42+
this._map.delete(firstKey);
43+
}
44+
45+
this._map.set(key, result);
46+
this._size += result.size;
47+
return result.value;
48+
}
49+
}

packages/trace-viewer/src/sw/snapshotRenderer.ts

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import { escapeHTMLAttribute, escapeHTML } from '@isomorphic/stringUtils';
1818
import type { FrameSnapshot, NodeNameAttributesChildNodesSnapshot, NodeSnapshot, RenderedFrameSnapshot, ResourceSnapshot, SubtreeReferenceSnapshot } from '@trace/snapshot';
19+
import type { LRUCache } from './lruCache';
1920

2021
function isNodeNameAttributesChildNodesSnapshot(n: NodeSnapshot): n is NodeNameAttributesChildNodesSnapshot {
2122
return Array.isArray(n) && typeof n[0] === 'string';
@@ -25,43 +26,17 @@ function isSubtreeReferenceSnapshot(n: NodeSnapshot): n is SubtreeReferenceSnaps
2526
return Array.isArray(n) && Array.isArray(n[0]);
2627
}
2728

28-
let cacheSize = 0;
29-
const cache = new Map<SnapshotRenderer, string>();
30-
const CACHE_SIZE = 300_000_000; // 300mb
31-
32-
function lruCache(key: SnapshotRenderer, compute: () => string): string {
33-
if (cache.has(key)) {
34-
const value = cache.get(key)!;
35-
// reinserting makes this the least recently used entry
36-
cache.delete(key);
37-
cache.set(key, value);
38-
return value;
39-
}
40-
41-
42-
const result = compute();
43-
44-
while (cache.size && cacheSize + result.length > CACHE_SIZE) {
45-
const [firstKey, firstValue] = cache.entries().next().value;
46-
cacheSize -= firstValue.length;
47-
cache.delete(firstKey);
48-
}
49-
50-
cache.set(key, result);
51-
cacheSize += result.length;
52-
53-
return result;
54-
}
55-
5629
export class SnapshotRenderer {
30+
private _htmlCache: LRUCache<SnapshotRenderer, string>;
5731
private _snapshots: FrameSnapshot[];
5832
private _index: number;
5933
readonly snapshotName: string | undefined;
6034
private _resources: ResourceSnapshot[];
6135
private _snapshot: FrameSnapshot;
6236
private _callId: string;
6337

64-
constructor(resources: ResourceSnapshot[], snapshots: FrameSnapshot[], index: number) {
38+
constructor(htmlCache: LRUCache<SnapshotRenderer, string>, resources: ResourceSnapshot[], snapshots: FrameSnapshot[], index: number) {
39+
this._htmlCache = htmlCache;
6540
this._resources = resources;
6641
this._snapshots = snapshots;
6742
this._index = index;
@@ -151,16 +126,15 @@ export class SnapshotRenderer {
151126
};
152127

153128
const snapshot = this._snapshot;
154-
const html = lruCache(this, () => {
129+
const html = this._htmlCache.getOrCompute(this, () => {
155130
visit(snapshot.html, this._index, undefined, undefined);
156-
157-
const html = result.join('');
158-
// Hide the document in order to prevent flickering. We will unhide once script has processed shadow.
159131
const prefix = snapshot.doctype ? `<!DOCTYPE ${snapshot.doctype}>` : '';
160-
return prefix + [
132+
const html = prefix + [
133+
// Hide the document in order to prevent flickering. We will unhide once script has processed shadow.
161134
'<style>*,*::before,*::after { visibility: hidden }</style>',
162135
`<script>${snapshotScript(this._callId, this.snapshotName)}</script>`
163-
].join('') + html;
136+
].join('') + result.join('');
137+
return { value: html, size: html.length };
164138
});
165139

166140
return { html, pageId: snapshot.pageId, frameId: snapshot.frameId, index: this._index };

packages/trace-viewer/src/sw/snapshotStorage.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
import type { FrameSnapshot, ResourceSnapshot } from '@trace/snapshot';
1818
import { rewriteURLForCustomProtocol, SnapshotRenderer } from './snapshotRenderer';
19+
import { LRUCache } from './lruCache';
1920

2021
export class SnapshotStorage {
2122
private _resources: ResourceSnapshot[] = [];
2223
private _frameSnapshots = new Map<string, {
2324
raw: FrameSnapshot[],
2425
renderers: SnapshotRenderer[]
2526
}>();
27+
private _cache = new LRUCache<SnapshotRenderer, string>(100_000_000); // 100MB per each trace
2628

2729
addResource(resource: ResourceSnapshot): void {
2830
resource.request.url = rewriteURLForCustomProtocol(resource.request.url);
@@ -43,7 +45,7 @@ export class SnapshotStorage {
4345
this._frameSnapshots.set(snapshot.pageId, frameSnapshots);
4446
}
4547
frameSnapshots.raw.push(snapshot);
46-
const renderer = new SnapshotRenderer(this._resources, frameSnapshots.raw, frameSnapshots.raw.length - 1);
48+
const renderer = new SnapshotRenderer(this._cache, this._resources, frameSnapshots.raw, frameSnapshots.raw.length - 1);
4749
frameSnapshots.renderers.push(renderer);
4850
return renderer;
4951
}

0 commit comments

Comments
 (0)