Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit f984a56

Browse files
author
Brian Vaughn
committed
Replaced simple cache with lru cache for chart data
1 parent 09edccb commit f984a56

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"jest": "22.1.4",
2525
"json-loader": "0.5.4",
2626
"log-update": "^2.0.0",
27+
"lru-cache": "^4.1.3",
2728
"memoize-one": "^3.1.1",
2829
"node-libs-browser": "0.5.3",
2930
"nullthrows": "^1.0.0",

plugins/Profiler/ProfilerStore.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {ChartType, Interaction, RootProfilerData, Snapshot} from './Profile
1616
const {List} = require('immutable');
1717
const {EventEmitter} = require('events');
1818
const {get, set} = require('../../utils/storage');
19+
const LRU = require('lru-cache');
1920

2021
const LOCAL_STORAGE_CHART_TYPE_KEY = 'profiler:selectedChartType';
2122
const LOCAL_STORAGE_COMMIT_THRESHOLD = 'profiler:commitThreshold';
@@ -26,7 +27,7 @@ class ProfilerStore extends EventEmitter {
2627
_bridge: Bridge;
2728
_mainStore: Object;
2829

29-
cachedData = {};
30+
cachedData = LRU(50); // Evict items from the cache after this number
3031
commitThreshold: number = ((get(LOCAL_STORAGE_COMMIT_THRESHOLD, 0): any): number);
3132
hideCommitsBelowThreshold: boolean = ((get(LOCAL_STORAGE_HIDE_COMMITS_BELOW_THRESHOLD, false): any): boolean);
3233
isRecording: boolean = false;
@@ -54,26 +55,26 @@ class ProfilerStore extends EventEmitter {
5455
}
5556

5657
cacheDataForSnapshot(snapshotIndex: number, snapshotRootID: string, key: string, data: any): void {
57-
this.cachedData[`${snapshotIndex}-${snapshotRootID}-${key}`] = data;
58+
this.cachedData.set(`${snapshotIndex}-${snapshotRootID}-${key}`, data);
5859
}
5960

6061
cacheInteractionData(rootID: string, data: any): void {
61-
this.cachedData[`${rootID}-interactions`] = data;
62+
this.cachedData.set(`${rootID}-interactions`, data);
6263
}
6364

6465
clearSnapshots = () => {
65-
this.cachedData = {};
66+
this.cachedData.reset();
6667
this.processedInteractions = {};
6768
this.rootsToProfilerData = new Map();
6869
this.emit('profilerData', this.rootsToProfilerData);
6970
};
7071

7172
getCachedDataForSnapshot(snapshotIndex: number, snapshotRootID: string, key: string): any {
72-
return this.cachedData[`${snapshotIndex}-${snapshotRootID}-${key}`] || null;
73+
return this.cachedData.get(`${snapshotIndex}-${snapshotRootID}-${key}`) || null;
7374
}
7475

7576
getCachedInteractionData(rootID: string): any {
76-
return this.cachedData[`${rootID}-interactions`] || null;
77+
return this.cachedData.get(`${rootID}-interactions`) || null;
7778
}
7879

7980
processInteraction(interaction: Interaction): Interaction {

yarn.lock

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4769,6 +4769,13 @@ lru-cache@^4.0.0, lru-cache@^4.0.1:
47694769
pseudomap "^1.0.1"
47704770
yallist "^2.0.0"
47714771

4772+
lru-cache@^4.1.3:
4773+
version "4.1.3"
4774+
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.3.tgz#a1175cf3496dfc8436c156c334b4955992bce69c"
4775+
dependencies:
4776+
pseudomap "^1.0.2"
4777+
yallist "^2.1.2"
4778+
47724779
make-dir@^1.0.0:
47734780
version "1.0.0"
47744781
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-1.0.0.tgz#97a011751e91dd87cfadef58832ebb04936de978"
@@ -5580,7 +5587,7 @@ prr@~0.0.0:
55805587
version "0.0.0"
55815588
resolved "https://registry.yarnpkg.com/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"
55825589

5583-
pseudomap@^1.0.1:
5590+
pseudomap@^1.0.1, pseudomap@^1.0.2:
55845591
version "1.0.2"
55855592
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
55865593

@@ -7334,6 +7341,10 @@ yallist@^2.0.0:
73347341
version "2.0.0"
73357342
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.0.0.tgz#306c543835f09ee1a4cb23b7bce9ab341c91cdd4"
73367343

7344+
yallist@^2.1.2:
7345+
version "2.1.2"
7346+
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"
7347+
73377348
yargs-parser@^4.2.0:
73387349
version "4.2.1"
73397350
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-4.2.1.tgz#29cceac0dc4f03c6c87b4a9f217dd18c9f74871c"

0 commit comments

Comments
 (0)