Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions packages/rstack/src/fmt/cacheStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,33 +202,34 @@ class FmtCacheStoreImpl implements FmtCacheStore {
return index;
}

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

const { files, options } = this.#cache;
const nextOptions: string[] = [];
const nextUseCounts: number[] = [];
const remappedIndexes = new Int32Array(options.length).fill(-1);
const counts = this.#optionsUseCounts;
const remap = new Int32Array(options.length).fill(-1);
let nextIndex = 0;
this.#optionsIndexes.clear();
for (let index = 0; index < options.length; index++) {
const useCount = this.#optionsUseCounts[index];
if (useCount > 0) {
remappedIndexes[index] = nextOptions.length;
nextOptions.push(options[index]);
nextUseCounts.push(useCount);
const count = counts[index];
if (count > 0) {
const option = options[index];
remap[index] = nextIndex;
options[nextIndex] = option;
counts[nextIndex] = count;
this.#optionsIndexes.set(option, nextIndex);
nextIndex++;
}
}
for (let offset = 0; offset < files.length; offset += fileEntryWidth) {
const currentIndex = files[offset + optionsIndexOffset] as number;
files[offset + optionsIndexOffset] = remappedIndexes[currentIndex];
}
options.length = nextIndex;
counts.length = nextIndex;

options.splice(0, options.length, ...nextOptions);
this.#optionsUseCounts.splice(0, this.#optionsUseCounts.length, ...nextUseCounts);
this.#optionsIndexes.clear();
for (let index = 0; index < options.length; index++) {
this.#optionsIndexes.set(options[index], index);
for (let offset = 0; offset < files.length; offset += fileEntryWidth) {
const index = files[offset + optionsIndexOffset] as number;
files[offset + optionsIndexOffset] = remap[index];
}
}

Expand Down