Skip to content

Commit 0d691dc

Browse files
tunardevkrisk
authored andcommitted
perf(bitap): reuse bit arrays and look up the alphabet by char code
Closes #865
1 parent 41c4978 commit 0d691dc

14 files changed

Lines changed: 200 additions & 79 deletions

‎dist/fuse.basic.cjs‎

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

408408
//#endregion
409409
//#region src/search/bitap/search.ts
410+
let bitArr = /* @__PURE__ */ new Int32Array(256);
411+
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);
418+
}
410419
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 } = {}) {
411420
if (pattern.length > 32) throw new Error(PATTERN_LENGTH_TOO_LARGE(32));
412421
const patternLen = pattern.length;
@@ -437,7 +446,9 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
437446
}
438447
}
439448
bestLocation = -1;
440-
let lastBitArr = [];
449+
ensureBitArrCapacity(textLen + patternLen + 2);
450+
let row = bitArr;
451+
let lastRow = lastBitArr;
441452
let finalScore = 1;
442453
let bestErrors = 0;
443454
let binMax = patternLen + textLen;
@@ -453,14 +464,15 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
453464
binMax = binMid;
454465
let start = Math.max(1, expectedLocation - binMid + 1);
455466
const finish = findAllMatches ? textLen : Math.min(expectedLocation + binMid, textLen) + patternLen;
456-
const bitArr = Array(finish + 2);
457-
bitArr[finish + 1] = (1 << i) - 1;
467+
row.fill(0, 0, finish + 2);
468+
row[finish + 1] = (1 << i) - 1;
458469
for (let j = finish; j >= start; j -= 1) {
459470
const currentLocation = j - 1;
460-
const charMatch = patternAlphabet[text[currentLocation]];
461-
bitArr[j] = (bitArr[j + 1] << 1 | 1) & charMatch;
462-
if (i) bitArr[j] |= (lastBitArr[j + 1] | lastBitArr[j]) << 1 | 1 | lastBitArr[j + 1];
463-
if (bitArr[j] & mask) {
471+
const charMatch = currentLocation < textLen ? patternAlphabet[text.charCodeAt(currentLocation)] : 0;
472+
let bits = (row[j + 1] << 1 | 1) & charMatch;
473+
if (i) bits |= (lastRow[j + 1] | lastRow[j]) << 1 | 1 | lastRow[j + 1];
474+
row[j] = bits;
475+
if (bits & mask) {
464476
finalScore = calcScore(i, currentLocation);
465477
if (finalScore <= currentThreshold) {
466478
currentThreshold = finalScore;
@@ -472,11 +484,13 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
472484
}
473485
}
474486
if (calcScore(i + 1, expectedLocation) > currentThreshold) break;
475-
lastBitArr = bitArr;
487+
const swap = lastRow;
488+
lastRow = row;
489+
row = swap;
476490
}
477491
if (computeMatches && bestLocation >= 0) {
478492
const matchEnd = Math.min(textLen - 1, bestLocation + patternLen - 1 + bestErrors);
479-
for (let k = bestLocation; k <= matchEnd; k += 1) if (patternAlphabet[text[k]]) matchMask[k] = 1;
493+
for (let k = bestLocation; k <= matchEnd; k += 1) if (patternAlphabet[text.charCodeAt(k)]) matchMask[k] = 1;
480494
}
481495
const result = {
482496
isMatch: bestLocation >= 0,
@@ -495,8 +509,8 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
495509
function createPatternAlphabet(pattern) {
496510
const mask = {};
497511
for (let i = 0, len = pattern.length; i < len; i += 1) {
498-
const char = pattern.charAt(i);
499-
mask[char] = (mask[char] || 0) | 1 << len - i - 1;
512+
const code = pattern.charCodeAt(i);
513+
mask[code] = (mask[code] || 0) | 1 << len - i - 1;
500514
}
501515
return mask;
502516
}

‎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: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,15 @@ function convertMaskToIndices(matchmask = [], minMatchCharLength = Config.minMat
406406

407407
//#endregion
408408
//#region src/search/bitap/search.ts
409+
let bitArr = /* @__PURE__ */ new Int32Array(256);
410+
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);
417+
}
409418
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 } = {}) {
410419
if (pattern.length > 32) throw new Error(PATTERN_LENGTH_TOO_LARGE(32));
411420
const patternLen = pattern.length;
@@ -436,7 +445,9 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
436445
}
437446
}
438447
bestLocation = -1;
439-
let lastBitArr = [];
448+
ensureBitArrCapacity(textLen + patternLen + 2);
449+
let row = bitArr;
450+
let lastRow = lastBitArr;
440451
let finalScore = 1;
441452
let bestErrors = 0;
442453
let binMax = patternLen + textLen;
@@ -452,14 +463,15 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
452463
binMax = binMid;
453464
let start = Math.max(1, expectedLocation - binMid + 1);
454465
const finish = findAllMatches ? textLen : Math.min(expectedLocation + binMid, textLen) + patternLen;
455-
const bitArr = Array(finish + 2);
456-
bitArr[finish + 1] = (1 << i) - 1;
466+
row.fill(0, 0, finish + 2);
467+
row[finish + 1] = (1 << i) - 1;
457468
for (let j = finish; j >= start; j -= 1) {
458469
const currentLocation = j - 1;
459-
const charMatch = patternAlphabet[text[currentLocation]];
460-
bitArr[j] = (bitArr[j + 1] << 1 | 1) & charMatch;
461-
if (i) bitArr[j] |= (lastBitArr[j + 1] | lastBitArr[j]) << 1 | 1 | lastBitArr[j + 1];
462-
if (bitArr[j] & mask) {
470+
const charMatch = currentLocation < textLen ? patternAlphabet[text.charCodeAt(currentLocation)] : 0;
471+
let bits = (row[j + 1] << 1 | 1) & charMatch;
472+
if (i) bits |= (lastRow[j + 1] | lastRow[j]) << 1 | 1 | lastRow[j + 1];
473+
row[j] = bits;
474+
if (bits & mask) {
463475
finalScore = calcScore(i, currentLocation);
464476
if (finalScore <= currentThreshold) {
465477
currentThreshold = finalScore;
@@ -471,11 +483,13 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
471483
}
472484
}
473485
if (calcScore(i + 1, expectedLocation) > currentThreshold) break;
474-
lastBitArr = bitArr;
486+
const swap = lastRow;
487+
lastRow = row;
488+
row = swap;
475489
}
476490
if (computeMatches && bestLocation >= 0) {
477491
const matchEnd = Math.min(textLen - 1, bestLocation + patternLen - 1 + bestErrors);
478-
for (let k = bestLocation; k <= matchEnd; k += 1) if (patternAlphabet[text[k]]) matchMask[k] = 1;
492+
for (let k = bestLocation; k <= matchEnd; k += 1) if (patternAlphabet[text.charCodeAt(k)]) matchMask[k] = 1;
479493
}
480494
const result = {
481495
isMatch: bestLocation >= 0,
@@ -494,8 +508,8 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
494508
function createPatternAlphabet(pattern) {
495509
const mask = {};
496510
for (let i = 0, len = pattern.length; i < len; i += 1) {
497-
const char = pattern.charAt(i);
498-
mask[char] = (mask[char] || 0) | 1 << len - i - 1;
511+
const code = pattern.charCodeAt(i);
512+
mask[code] = (mask[code] || 0) | 1 << len - i - 1;
499513
}
500514
return mask;
501515
}

‎dist/fuse.cjs‎

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

408408
//#endregion
409409
//#region src/search/bitap/search.ts
410+
let bitArr = /* @__PURE__ */ new Int32Array(256);
411+
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);
418+
}
410419
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 } = {}) {
411420
if (pattern.length > 32) throw new Error(PATTERN_LENGTH_TOO_LARGE(32));
412421
const patternLen = pattern.length;
@@ -437,7 +446,9 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
437446
}
438447
}
439448
bestLocation = -1;
440-
let lastBitArr = [];
449+
ensureBitArrCapacity(textLen + patternLen + 2);
450+
let row = bitArr;
451+
let lastRow = lastBitArr;
441452
let finalScore = 1;
442453
let bestErrors = 0;
443454
let binMax = patternLen + textLen;
@@ -453,14 +464,15 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
453464
binMax = binMid;
454465
let start = Math.max(1, expectedLocation - binMid + 1);
455466
const finish = findAllMatches ? textLen : Math.min(expectedLocation + binMid, textLen) + patternLen;
456-
const bitArr = Array(finish + 2);
457-
bitArr[finish + 1] = (1 << i) - 1;
467+
row.fill(0, 0, finish + 2);
468+
row[finish + 1] = (1 << i) - 1;
458469
for (let j = finish; j >= start; j -= 1) {
459470
const currentLocation = j - 1;
460-
const charMatch = patternAlphabet[text[currentLocation]];
461-
bitArr[j] = (bitArr[j + 1] << 1 | 1) & charMatch;
462-
if (i) bitArr[j] |= (lastBitArr[j + 1] | lastBitArr[j]) << 1 | 1 | lastBitArr[j + 1];
463-
if (bitArr[j] & mask) {
471+
const charMatch = currentLocation < textLen ? patternAlphabet[text.charCodeAt(currentLocation)] : 0;
472+
let bits = (row[j + 1] << 1 | 1) & charMatch;
473+
if (i) bits |= (lastRow[j + 1] | lastRow[j]) << 1 | 1 | lastRow[j + 1];
474+
row[j] = bits;
475+
if (bits & mask) {
464476
finalScore = calcScore(i, currentLocation);
465477
if (finalScore <= currentThreshold) {
466478
currentThreshold = finalScore;
@@ -472,11 +484,13 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
472484
}
473485
}
474486
if (calcScore(i + 1, expectedLocation) > currentThreshold) break;
475-
lastBitArr = bitArr;
487+
const swap = lastRow;
488+
lastRow = row;
489+
row = swap;
476490
}
477491
if (computeMatches && bestLocation >= 0) {
478492
const matchEnd = Math.min(textLen - 1, bestLocation + patternLen - 1 + bestErrors);
479-
for (let k = bestLocation; k <= matchEnd; k += 1) if (patternAlphabet[text[k]]) matchMask[k] = 1;
493+
for (let k = bestLocation; k <= matchEnd; k += 1) if (patternAlphabet[text.charCodeAt(k)]) matchMask[k] = 1;
480494
}
481495
const result = {
482496
isMatch: bestLocation >= 0,
@@ -495,8 +509,8 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
495509
function createPatternAlphabet(pattern) {
496510
const mask = {};
497511
for (let i = 0, len = pattern.length; i < len; i += 1) {
498-
const char = pattern.charAt(i);
499-
mask[char] = (mask[char] || 0) | 1 << len - i - 1;
512+
const code = pattern.charCodeAt(i);
513+
mask[code] = (mask[code] || 0) | 1 << len - i - 1;
500514
}
501515
return mask;
502516
}

‎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: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,15 @@ function convertMaskToIndices(matchmask = [], minMatchCharLength = Config.minMat
406406

407407
//#endregion
408408
//#region src/search/bitap/search.ts
409+
let bitArr = /* @__PURE__ */ new Int32Array(256);
410+
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);
417+
}
409418
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 } = {}) {
410419
if (pattern.length > 32) throw new Error(PATTERN_LENGTH_TOO_LARGE(32));
411420
const patternLen = pattern.length;
@@ -436,7 +445,9 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
436445
}
437446
}
438447
bestLocation = -1;
439-
let lastBitArr = [];
448+
ensureBitArrCapacity(textLen + patternLen + 2);
449+
let row = bitArr;
450+
let lastRow = lastBitArr;
440451
let finalScore = 1;
441452
let bestErrors = 0;
442453
let binMax = patternLen + textLen;
@@ -452,14 +463,15 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
452463
binMax = binMid;
453464
let start = Math.max(1, expectedLocation - binMid + 1);
454465
const finish = findAllMatches ? textLen : Math.min(expectedLocation + binMid, textLen) + patternLen;
455-
const bitArr = Array(finish + 2);
456-
bitArr[finish + 1] = (1 << i) - 1;
466+
row.fill(0, 0, finish + 2);
467+
row[finish + 1] = (1 << i) - 1;
457468
for (let j = finish; j >= start; j -= 1) {
458469
const currentLocation = j - 1;
459-
const charMatch = patternAlphabet[text[currentLocation]];
460-
bitArr[j] = (bitArr[j + 1] << 1 | 1) & charMatch;
461-
if (i) bitArr[j] |= (lastBitArr[j + 1] | lastBitArr[j]) << 1 | 1 | lastBitArr[j + 1];
462-
if (bitArr[j] & mask) {
470+
const charMatch = currentLocation < textLen ? patternAlphabet[text.charCodeAt(currentLocation)] : 0;
471+
let bits = (row[j + 1] << 1 | 1) & charMatch;
472+
if (i) bits |= (lastRow[j + 1] | lastRow[j]) << 1 | 1 | lastRow[j + 1];
473+
row[j] = bits;
474+
if (bits & mask) {
463475
finalScore = calcScore(i, currentLocation);
464476
if (finalScore <= currentThreshold) {
465477
currentThreshold = finalScore;
@@ -471,11 +483,13 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
471483
}
472484
}
473485
if (calcScore(i + 1, expectedLocation) > currentThreshold) break;
474-
lastBitArr = bitArr;
486+
const swap = lastRow;
487+
lastRow = row;
488+
row = swap;
475489
}
476490
if (computeMatches && bestLocation >= 0) {
477491
const matchEnd = Math.min(textLen - 1, bestLocation + patternLen - 1 + bestErrors);
478-
for (let k = bestLocation; k <= matchEnd; k += 1) if (patternAlphabet[text[k]]) matchMask[k] = 1;
492+
for (let k = bestLocation; k <= matchEnd; k += 1) if (patternAlphabet[text.charCodeAt(k)]) matchMask[k] = 1;
479493
}
480494
const result = {
481495
isMatch: bestLocation >= 0,
@@ -494,8 +508,8 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
494508
function createPatternAlphabet(pattern) {
495509
const mask = {};
496510
for (let i = 0, len = pattern.length; i < len; i += 1) {
497-
const char = pattern.charAt(i);
498-
mask[char] = (mask[char] || 0) | 1 << len - i - 1;
511+
const code = pattern.charCodeAt(i);
512+
mask[code] = (mask[code] || 0) | 1 << len - i - 1;
499513
}
500514
return mask;
501515
}

‎dist/fuse.worker.min.mjs‎

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

‎dist/fuse.worker.mjs‎

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

396396
//#endregion
397397
//#region src/search/bitap/search.ts
398+
let bitArr = /* @__PURE__ */ new Int32Array(256);
399+
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);
406+
}
398407
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 } = {}) {
399408
if (pattern.length > 32) throw new Error(PATTERN_LENGTH_TOO_LARGE(32));
400409
const patternLen = pattern.length;
@@ -425,7 +434,9 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
425434
}
426435
}
427436
bestLocation = -1;
428-
let lastBitArr = [];
437+
ensureBitArrCapacity(textLen + patternLen + 2);
438+
let row = bitArr;
439+
let lastRow = lastBitArr;
429440
let finalScore = 1;
430441
let bestErrors = 0;
431442
let binMax = patternLen + textLen;
@@ -441,14 +452,15 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
441452
binMax = binMid;
442453
let start = Math.max(1, expectedLocation - binMid + 1);
443454
const finish = findAllMatches ? textLen : Math.min(expectedLocation + binMid, textLen) + patternLen;
444-
const bitArr = Array(finish + 2);
445-
bitArr[finish + 1] = (1 << i) - 1;
455+
row.fill(0, 0, finish + 2);
456+
row[finish + 1] = (1 << i) - 1;
446457
for (let j = finish; j >= start; j -= 1) {
447458
const currentLocation = j - 1;
448-
const charMatch = patternAlphabet[text[currentLocation]];
449-
bitArr[j] = (bitArr[j + 1] << 1 | 1) & charMatch;
450-
if (i) bitArr[j] |= (lastBitArr[j + 1] | lastBitArr[j]) << 1 | 1 | lastBitArr[j + 1];
451-
if (bitArr[j] & mask) {
459+
const charMatch = currentLocation < textLen ? patternAlphabet[text.charCodeAt(currentLocation)] : 0;
460+
let bits = (row[j + 1] << 1 | 1) & charMatch;
461+
if (i) bits |= (lastRow[j + 1] | lastRow[j]) << 1 | 1 | lastRow[j + 1];
462+
row[j] = bits;
463+
if (bits & mask) {
452464
finalScore = calcScore(i, currentLocation);
453465
if (finalScore <= currentThreshold) {
454466
currentThreshold = finalScore;
@@ -460,11 +472,13 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
460472
}
461473
}
462474
if (calcScore(i + 1, expectedLocation) > currentThreshold) break;
463-
lastBitArr = bitArr;
475+
const swap = lastRow;
476+
lastRow = row;
477+
row = swap;
464478
}
465479
if (computeMatches && bestLocation >= 0) {
466480
const matchEnd = Math.min(textLen - 1, bestLocation + patternLen - 1 + bestErrors);
467-
for (let k = bestLocation; k <= matchEnd; k += 1) if (patternAlphabet[text[k]]) matchMask[k] = 1;
481+
for (let k = bestLocation; k <= matchEnd; k += 1) if (patternAlphabet[text.charCodeAt(k)]) matchMask[k] = 1;
468482
}
469483
const result = {
470484
isMatch: bestLocation >= 0,
@@ -483,8 +497,8 @@ function search(text, pattern, patternAlphabet, { location = Config.location, di
483497
function createPatternAlphabet(pattern) {
484498
const mask = {};
485499
for (let i = 0, len = pattern.length; i < len; i += 1) {
486-
const char = pattern.charAt(i);
487-
mask[char] = (mask[char] || 0) | 1 << len - i - 1;
500+
const code = pattern.charCodeAt(i);
501+
mask[code] = (mask[code] || 0) | 1 << len - i - 1;
488502
}
489503
return mask;
490504
}

0 commit comments

Comments
 (0)