Skip to content

Commit 4b671d5

Browse files
authored
perf(fmt): compact cache options in place (#347)
1 parent 7c836cc commit 4b671d5

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

packages/rstack/src/fmt/cacheStore.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,33 +202,34 @@ class FmtCacheStoreImpl implements FmtCacheStore {
202202
return index;
203203
}
204204

205+
/** Removes unreferenced option hashes and remaps file entries to the compacted indexes. */
205206
#compactUnusedOptions(): void {
206207
if (!this.#optionsUseCounts.includes(0)) {
207208
return;
208209
}
209210

210211
const { files, options } = this.#cache;
211-
const nextOptions: string[] = [];
212-
const nextUseCounts: number[] = [];
213-
const remappedIndexes = new Int32Array(options.length).fill(-1);
212+
const counts = this.#optionsUseCounts;
213+
const remap = new Int32Array(options.length).fill(-1);
214+
let nextIndex = 0;
215+
this.#optionsIndexes.clear();
214216
for (let index = 0; index < options.length; index++) {
215-
const useCount = this.#optionsUseCounts[index];
216-
if (useCount > 0) {
217-
remappedIndexes[index] = nextOptions.length;
218-
nextOptions.push(options[index]);
219-
nextUseCounts.push(useCount);
217+
const count = counts[index];
218+
if (count > 0) {
219+
const option = options[index];
220+
remap[index] = nextIndex;
221+
options[nextIndex] = option;
222+
counts[nextIndex] = count;
223+
this.#optionsIndexes.set(option, nextIndex);
224+
nextIndex++;
220225
}
221226
}
222-
for (let offset = 0; offset < files.length; offset += fileEntryWidth) {
223-
const currentIndex = files[offset + optionsIndexOffset] as number;
224-
files[offset + optionsIndexOffset] = remappedIndexes[currentIndex];
225-
}
227+
options.length = nextIndex;
228+
counts.length = nextIndex;
226229

227-
options.splice(0, options.length, ...nextOptions);
228-
this.#optionsUseCounts.splice(0, this.#optionsUseCounts.length, ...nextUseCounts);
229-
this.#optionsIndexes.clear();
230-
for (let index = 0; index < options.length; index++) {
231-
this.#optionsIndexes.set(options[index], index);
230+
for (let offset = 0; offset < files.length; offset += fileEntryWidth) {
231+
const index = files[offset + optionsIndexOffset] as number;
232+
files[offset + optionsIndexOffset] = remap[index];
232233
}
233234
}
234235

0 commit comments

Comments
 (0)