Skip to content

Commit 830626a

Browse files
committed
perf(bitap): cap the shared bit-array buffers
The shared rows grew to fit the longest text ever searched and never shrank, so one 5M-character document pinned 67 MB for the life of the process. Reuse them only up to 64k entries; longer texts get one-off arrays that are collected after the call. Max retained is now 512 KB, with no change to the speedup on typical texts.
1 parent 0d691dc commit 830626a

12 files changed

Lines changed: 90 additions & 59 deletions

‎dist/fuse.basic.cjs‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,18 @@ function convertMaskToIndices(matchmask = [], minMatchCharLength = Config.minMat
407407

408408
//#endregion
409409
//#region src/search/bitap/search.ts
410+
const MAX_SHARED_ROW_LENGTH = 65536;
410411
let bitArr = /* @__PURE__ */ new Int32Array(256);
411412
let lastBitArr = /* @__PURE__ */ new Int32Array(256);
412-
function ensureBitArrCapacity(size) {
413-
if (bitArr.length >= size) return;
414-
let capacity = bitArr.length;
415-
while (capacity < size) capacity *= 2;
416-
bitArr = new Int32Array(capacity);
417-
lastBitArr = new Int32Array(capacity);
413+
function getBitArrs(size) {
414+
if (size > MAX_SHARED_ROW_LENGTH) return [new Int32Array(size), new Int32Array(size)];
415+
if (bitArr.length < size) {
416+
let capacity = bitArr.length;
417+
while (capacity < size) capacity *= 2;
418+
bitArr = new Int32Array(capacity);
419+
lastBitArr = new Int32Array(capacity);
420+
}
421+
return [bitArr, lastBitArr];
418422
}
419423
function search(text, pattern, patternAlphabet, { location = Config.location, distance = Config.distance, threshold = Config.threshold, findAllMatches = Config.findAllMatches, minMatchCharLength = Config.minMatchCharLength, includeMatches = Config.includeMatches, ignoreLocation = Config.ignoreLocation } = {}) {
420424
if (pattern.length > 32) throw new Error(PATTERN_LENGTH_TOO_LARGE(32));
@@ -446,9 +450,7 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
446450
}
447451
}
448452
bestLocation = -1;
449-
ensureBitArrCapacity(textLen + patternLen + 2);
450-
let row = bitArr;
451-
let lastRow = lastBitArr;
453+
let [row, lastRow] = getBitArrs(textLen + patternLen + 2);
452454
let finalScore = 1;
453455
let bestErrors = 0;
454456
let binMax = patternLen + textLen;

‎dist/fuse.basic.min.cjs‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎dist/fuse.basic.min.mjs‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎dist/fuse.basic.mjs‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,18 @@ function convertMaskToIndices(matchmask = [], minMatchCharLength = Config.minMat
406406

407407
//#endregion
408408
//#region src/search/bitap/search.ts
409+
const MAX_SHARED_ROW_LENGTH = 65536;
409410
let bitArr = /* @__PURE__ */ new Int32Array(256);
410411
let lastBitArr = /* @__PURE__ */ new Int32Array(256);
411-
function ensureBitArrCapacity(size) {
412-
if (bitArr.length >= size) return;
413-
let capacity = bitArr.length;
414-
while (capacity < size) capacity *= 2;
415-
bitArr = new Int32Array(capacity);
416-
lastBitArr = new Int32Array(capacity);
412+
function getBitArrs(size) {
413+
if (size > MAX_SHARED_ROW_LENGTH) return [new Int32Array(size), new Int32Array(size)];
414+
if (bitArr.length < size) {
415+
let capacity = bitArr.length;
416+
while (capacity < size) capacity *= 2;
417+
bitArr = new Int32Array(capacity);
418+
lastBitArr = new Int32Array(capacity);
419+
}
420+
return [bitArr, lastBitArr];
417421
}
418422
function search(text, pattern, patternAlphabet, { location = Config.location, distance = Config.distance, threshold = Config.threshold, findAllMatches = Config.findAllMatches, minMatchCharLength = Config.minMatchCharLength, includeMatches = Config.includeMatches, ignoreLocation = Config.ignoreLocation } = {}) {
419423
if (pattern.length > 32) throw new Error(PATTERN_LENGTH_TOO_LARGE(32));
@@ -445,9 +449,7 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
445449
}
446450
}
447451
bestLocation = -1;
448-
ensureBitArrCapacity(textLen + patternLen + 2);
449-
let row = bitArr;
450-
let lastRow = lastBitArr;
452+
let [row, lastRow] = getBitArrs(textLen + patternLen + 2);
451453
let finalScore = 1;
452454
let bestErrors = 0;
453455
let binMax = patternLen + textLen;

‎dist/fuse.cjs‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,18 @@ function convertMaskToIndices(matchmask = [], minMatchCharLength = Config.minMat
407407

408408
//#endregion
409409
//#region src/search/bitap/search.ts
410+
const MAX_SHARED_ROW_LENGTH = 65536;
410411
let bitArr = /* @__PURE__ */ new Int32Array(256);
411412
let lastBitArr = /* @__PURE__ */ new Int32Array(256);
412-
function ensureBitArrCapacity(size) {
413-
if (bitArr.length >= size) return;
414-
let capacity = bitArr.length;
415-
while (capacity < size) capacity *= 2;
416-
bitArr = new Int32Array(capacity);
417-
lastBitArr = new Int32Array(capacity);
413+
function getBitArrs(size) {
414+
if (size > MAX_SHARED_ROW_LENGTH) return [new Int32Array(size), new Int32Array(size)];
415+
if (bitArr.length < size) {
416+
let capacity = bitArr.length;
417+
while (capacity < size) capacity *= 2;
418+
bitArr = new Int32Array(capacity);
419+
lastBitArr = new Int32Array(capacity);
420+
}
421+
return [bitArr, lastBitArr];
418422
}
419423
function search(text, pattern, patternAlphabet, { location = Config.location, distance = Config.distance, threshold = Config.threshold, findAllMatches = Config.findAllMatches, minMatchCharLength = Config.minMatchCharLength, includeMatches = Config.includeMatches, ignoreLocation = Config.ignoreLocation } = {}) {
420424
if (pattern.length > 32) throw new Error(PATTERN_LENGTH_TOO_LARGE(32));
@@ -446,9 +450,7 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
446450
}
447451
}
448452
bestLocation = -1;
449-
ensureBitArrCapacity(textLen + patternLen + 2);
450-
let row = bitArr;
451-
let lastRow = lastBitArr;
453+
let [row, lastRow] = getBitArrs(textLen + patternLen + 2);
452454
let finalScore = 1;
453455
let bestErrors = 0;
454456
let binMax = patternLen + textLen;

‎dist/fuse.min.cjs‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎dist/fuse.min.mjs‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎dist/fuse.mjs‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,18 @@ function convertMaskToIndices(matchmask = [], minMatchCharLength = Config.minMat
406406

407407
//#endregion
408408
//#region src/search/bitap/search.ts
409+
const MAX_SHARED_ROW_LENGTH = 65536;
409410
let bitArr = /* @__PURE__ */ new Int32Array(256);
410411
let lastBitArr = /* @__PURE__ */ new Int32Array(256);
411-
function ensureBitArrCapacity(size) {
412-
if (bitArr.length >= size) return;
413-
let capacity = bitArr.length;
414-
while (capacity < size) capacity *= 2;
415-
bitArr = new Int32Array(capacity);
416-
lastBitArr = new Int32Array(capacity);
412+
function getBitArrs(size) {
413+
if (size > MAX_SHARED_ROW_LENGTH) return [new Int32Array(size), new Int32Array(size)];
414+
if (bitArr.length < size) {
415+
let capacity = bitArr.length;
416+
while (capacity < size) capacity *= 2;
417+
bitArr = new Int32Array(capacity);
418+
lastBitArr = new Int32Array(capacity);
419+
}
420+
return [bitArr, lastBitArr];
417421
}
418422
function search(text, pattern, patternAlphabet, { location = Config.location, distance = Config.distance, threshold = Config.threshold, findAllMatches = Config.findAllMatches, minMatchCharLength = Config.minMatchCharLength, includeMatches = Config.includeMatches, ignoreLocation = Config.ignoreLocation } = {}) {
419423
if (pattern.length > 32) throw new Error(PATTERN_LENGTH_TOO_LARGE(32));
@@ -445,9 +449,7 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
445449
}
446450
}
447451
bestLocation = -1;
448-
ensureBitArrCapacity(textLen + patternLen + 2);
449-
let row = bitArr;
450-
let lastRow = lastBitArr;
452+
let [row, lastRow] = getBitArrs(textLen + patternLen + 2);
451453
let finalScore = 1;
452454
let bestErrors = 0;
453455
let binMax = patternLen + textLen;

‎dist/fuse.worker.min.mjs‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎dist/fuse.worker.mjs‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,18 @@ function convertMaskToIndices(matchmask = [], minMatchCharLength = Config.minMat
395395

396396
//#endregion
397397
//#region src/search/bitap/search.ts
398+
const MAX_SHARED_ROW_LENGTH = 65536;
398399
let bitArr = /* @__PURE__ */ new Int32Array(256);
399400
let lastBitArr = /* @__PURE__ */ new Int32Array(256);
400-
function ensureBitArrCapacity(size) {
401-
if (bitArr.length >= size) return;
402-
let capacity = bitArr.length;
403-
while (capacity < size) capacity *= 2;
404-
bitArr = new Int32Array(capacity);
405-
lastBitArr = new Int32Array(capacity);
401+
function getBitArrs(size) {
402+
if (size > MAX_SHARED_ROW_LENGTH) return [new Int32Array(size), new Int32Array(size)];
403+
if (bitArr.length < size) {
404+
let capacity = bitArr.length;
405+
while (capacity < size) capacity *= 2;
406+
bitArr = new Int32Array(capacity);
407+
lastBitArr = new Int32Array(capacity);
408+
}
409+
return [bitArr, lastBitArr];
406410
}
407411
function search(text, pattern, patternAlphabet, { location = Config.location, distance = Config.distance, threshold = Config.threshold, findAllMatches = Config.findAllMatches, minMatchCharLength = Config.minMatchCharLength, includeMatches = Config.includeMatches, ignoreLocation = Config.ignoreLocation } = {}) {
408412
if (pattern.length > 32) throw new Error(PATTERN_LENGTH_TOO_LARGE(32));
@@ -434,9 +438,7 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
434438
}
435439
}
436440
bestLocation = -1;
437-
ensureBitArrCapacity(textLen + patternLen + 2);
438-
let row = bitArr;
439-
let lastRow = lastBitArr;
441+
let [row, lastRow] = getBitArrs(textLen + patternLen + 2);
440442
let finalScore = 1;
441443
let bestErrors = 0;
442444
let binMax = patternLen + textLen;

0 commit comments

Comments
 (0)