Skip to content

Commit 4851d06

Browse files
committed
limit parsing and detected cache
1 parent 9a01a2c commit 4851d06

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
watchForComposerChanges,
2727
} from "./support/fileWatcher";
2828
import { info } from "./support/logger";
29-
import { setParserBinaryPath } from "./support/parser";
29+
import { clearParserCaches, setParserBinaryPath } from "./support/parser";
3030
import { clearDefaultPhpCommand, initVendorWatchers } from "./support/php";
3131
import { hasWorkspace, projectPathExists } from "./support/project";
3232
import { cleanUpTemp } from "./support/util";
@@ -185,6 +185,7 @@ export function deactivate() {
185185
}
186186

187187
disposeWatchers();
188+
clearParserCaches();
188189

189190
if (client) {
190191
client.stop();

src/support/cache.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export class Cache<K, V> {
2+
private maxSize: number;
3+
private cache: Map<K, V>;
4+
5+
constructor(maxSize: number) {
6+
this.maxSize = maxSize;
7+
this.cache = new Map();
8+
}
9+
10+
get(key: K): V | undefined {
11+
const value = this.cache.get(key);
12+
13+
if (value !== undefined) {
14+
this.cache.delete(key);
15+
this.cache.set(key, value);
16+
}
17+
18+
return value;
19+
}
20+
21+
set(key: K, value: V): void {
22+
if (this.cache.has(key)) {
23+
this.cache.delete(key);
24+
} else if (this.cache.size >= this.maxSize) {
25+
const firstKey = this.cache.keys().next().value;
26+
27+
if (firstKey !== undefined) {
28+
this.cache.delete(firstKey);
29+
}
30+
}
31+
32+
this.cache.set(key, value);
33+
}
34+
35+
has(key: K): boolean {
36+
return this.cache.has(key);
37+
}
38+
39+
clear(): void {
40+
this.cache.clear();
41+
}
42+
43+
size(): number {
44+
return this.cache.size;
45+
}
46+
}

src/support/parser.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ import * as fs from "fs";
99
import * as os from "os";
1010
import * as vscode from "vscode";
1111
import { FeatureTag, ValidDetectParamTypes } from "..";
12+
import { Cache } from "./cache";
1213
import { showErrorPopup } from "./popup";
1314
import { md5, tempPath, toArray } from "./util";
1415

15-
const currentlyParsing = new Map<string, Promise<AutocompleteResult>>();
16-
const detected = new Map<
16+
const currentlyParsing = new Cache<string, Promise<AutocompleteResult>>(100);
17+
const detected = new Cache<
1718
string,
1819
Promise<AutocompleteParsingResult.ContextValue[]>
19-
>();
20+
>(50);
21+
22+
export const clearParserCaches = (): void => {
23+
currentlyParsing.clear();
24+
detected.clear();
25+
};
2026

2127
type TokenFormatted = [string, string, number];
2228
type Token = string | TokenFormatted;

0 commit comments

Comments
 (0)