Skip to content

Commit 3b9d9ea

Browse files
maccarlton25krisk
authored andcommitted
perf(*): normalize text once in TokenSearch and hole-sift MaxHeap
TokenSearch now case-folds and strips diacritics once per query term and once per field, instead of once inside each term's fuzzy searcher, which repeated the work for every query word. MaxHeap moves items with single-write hole sifts instead of three-write swaps. Output is unchanged (0 differences in 5,000 randomized comparisons vs main, including accented text, mixed case, tokenMatch any/all and limit). Token search on accented text with ignoreDiacritics: 1.6x (2 words) to 3.0x (6 words); plain accented 1.14-1.29x; ASCII 1.0-1.37x; repo benchmarks neutral. Closes #868
1 parent bde3799 commit 3b9d9ea

14 files changed

Lines changed: 241 additions & 134 deletions

‎dist/fuse.basic.cjs‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,10 @@ var MaxHeap = class {
931931
return this.heap.length;
932932
}
933933
insert(item) {
934-
if (this.size < this.limit) {
934+
const len = this.heap.length;
935+
if (len < this.limit) {
935936
this.heap.push(item);
936-
this._bubbleUp(this.size - 1);
937+
this._bubbleUp(len);
937938
} else if (this.comparator(item, this.heap[0]) < 0) {
938939
this.heap[0] = item;
939940
this._sinkDown(0);
@@ -943,32 +944,30 @@ var MaxHeap = class {
943944
return this.heap.sort(this.comparator);
944945
}
945946
_bubbleUp(i) {
946-
const heap = this.heap;
947+
const { heap, comparator } = this;
948+
const item = heap[i];
947949
while (i > 0) {
948950
const parent = i - 1 >> 1;
949-
if (this.comparator(heap[i], heap[parent]) <= 0) break;
950-
const tmp = heap[i];
951+
if (comparator(item, heap[parent]) <= 0) break;
951952
heap[i] = heap[parent];
952-
heap[parent] = tmp;
953953
i = parent;
954954
}
955+
heap[i] = item;
955956
}
956957
_sinkDown(i) {
957-
const heap = this.heap;
958+
const { heap, comparator } = this;
958959
const len = heap.length;
959-
let largest = i;
960-
do {
961-
i = largest;
960+
const item = heap[i];
961+
for (;;) {
962962
const left = 2 * i + 1;
963-
const right = 2 * i + 2;
964-
if (left < len && this.comparator(heap[left], heap[largest]) > 0) largest = left;
965-
if (right < len && this.comparator(heap[right], heap[largest]) > 0) largest = right;
966-
if (largest !== i) {
967-
const tmp = heap[i];
968-
heap[i] = heap[largest];
969-
heap[largest] = tmp;
970-
}
971-
} while (largest !== i);
963+
if (left >= len) break;
964+
let next = comparator(heap[left], item) > 0 ? left : -1;
965+
if (left + 1 < len && comparator(heap[left + 1], next < 0 ? item : heap[left]) > 0) next = left + 1;
966+
if (next < 0) break;
967+
heap[i] = heap[next];
968+
i = next;
969+
}
970+
heap[i] = item;
972971
}
973972
};
974973

‎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: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -930,9 +930,10 @@ var MaxHeap = class {
930930
return this.heap.length;
931931
}
932932
insert(item) {
933-
if (this.size < this.limit) {
933+
const len = this.heap.length;
934+
if (len < this.limit) {
934935
this.heap.push(item);
935-
this._bubbleUp(this.size - 1);
936+
this._bubbleUp(len);
936937
} else if (this.comparator(item, this.heap[0]) < 0) {
937938
this.heap[0] = item;
938939
this._sinkDown(0);
@@ -942,32 +943,30 @@ var MaxHeap = class {
942943
return this.heap.sort(this.comparator);
943944
}
944945
_bubbleUp(i) {
945-
const heap = this.heap;
946+
const { heap, comparator } = this;
947+
const item = heap[i];
946948
while (i > 0) {
947949
const parent = i - 1 >> 1;
948-
if (this.comparator(heap[i], heap[parent]) <= 0) break;
949-
const tmp = heap[i];
950+
if (comparator(item, heap[parent]) <= 0) break;
950951
heap[i] = heap[parent];
951-
heap[parent] = tmp;
952952
i = parent;
953953
}
954+
heap[i] = item;
954955
}
955956
_sinkDown(i) {
956-
const heap = this.heap;
957+
const { heap, comparator } = this;
957958
const len = heap.length;
958-
let largest = i;
959-
do {
960-
i = largest;
959+
const item = heap[i];
960+
for (;;) {
961961
const left = 2 * i + 1;
962-
const right = 2 * i + 2;
963-
if (left < len && this.comparator(heap[left], heap[largest]) > 0) largest = left;
964-
if (right < len && this.comparator(heap[right], heap[largest]) > 0) largest = right;
965-
if (largest !== i) {
966-
const tmp = heap[i];
967-
heap[i] = heap[largest];
968-
heap[largest] = tmp;
969-
}
970-
} while (largest !== i);
962+
if (left >= len) break;
963+
let next = comparator(heap[left], item) > 0 ? left : -1;
964+
if (left + 1 < len && comparator(heap[left + 1], next < 0 ? item : heap[left]) > 0) next = left + 1;
965+
if (next < 0) break;
966+
heap[i] = heap[next];
967+
i = next;
968+
}
969+
heap[i] = item;
971970
}
972971
};
973972

‎dist/fuse.cjs‎

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,9 +1283,10 @@ var MaxHeap = class {
12831283
return this.heap.length;
12841284
}
12851285
insert(item) {
1286-
if (this.size < this.limit) {
1286+
const len = this.heap.length;
1287+
if (len < this.limit) {
12871288
this.heap.push(item);
1288-
this._bubbleUp(this.size - 1);
1289+
this._bubbleUp(len);
12891290
} else if (this.comparator(item, this.heap[0]) < 0) {
12901291
this.heap[0] = item;
12911292
this._sinkDown(0);
@@ -1295,32 +1296,30 @@ var MaxHeap = class {
12951296
return this.heap.sort(this.comparator);
12961297
}
12971298
_bubbleUp(i) {
1298-
const heap = this.heap;
1299+
const { heap, comparator } = this;
1300+
const item = heap[i];
12991301
while (i > 0) {
13001302
const parent = i - 1 >> 1;
1301-
if (this.comparator(heap[i], heap[parent]) <= 0) break;
1302-
const tmp = heap[i];
1303+
if (comparator(item, heap[parent]) <= 0) break;
13031304
heap[i] = heap[parent];
1304-
heap[parent] = tmp;
13051305
i = parent;
13061306
}
1307+
heap[i] = item;
13071308
}
13081309
_sinkDown(i) {
1309-
const heap = this.heap;
1310+
const { heap, comparator } = this;
13101311
const len = heap.length;
1311-
let largest = i;
1312-
do {
1313-
i = largest;
1312+
const item = heap[i];
1313+
for (;;) {
13141314
const left = 2 * i + 1;
1315-
const right = 2 * i + 2;
1316-
if (left < len && this.comparator(heap[left], heap[largest]) > 0) largest = left;
1317-
if (right < len && this.comparator(heap[right], heap[largest]) > 0) largest = right;
1318-
if (largest !== i) {
1319-
const tmp = heap[i];
1320-
heap[i] = heap[largest];
1321-
heap[largest] = tmp;
1322-
}
1323-
} while (largest !== i);
1315+
if (left >= len) break;
1316+
let next = comparator(heap[left], item) > 0 ? left : -1;
1317+
if (left + 1 < len && comparator(heap[left + 1], next < 0 ? item : heap[left]) > 0) next = left + 1;
1318+
if (next < 0) break;
1319+
heap[i] = heap[next];
1320+
i = next;
1321+
}
1322+
heap[i] = item;
13241323
}
13251324
};
13261325

@@ -1409,18 +1408,23 @@ var TokenSearch = class {
14091408
});
14101409
const queryTerms = this.analyzer.tokenize(pattern);
14111410
const { df, fieldCount } = options._invertedIndex;
1411+
const { isCaseSensitive = Config.isCaseSensitive, ignoreDiacritics = Config.ignoreDiacritics } = options;
1412+
const normalize = this.normalize = (text) => {
1413+
text = isCaseSensitive ? text : text.toLowerCase();
1414+
return ignoreDiacritics ? stripDiacritics(text) : text;
1415+
};
14121416
this.termSearchers = [];
14131417
this.idfWeights = [];
14141418
for (const term of queryTerms) {
1415-
this.termSearchers.push(new FuzzySearch(term, {
1419+
this.termSearchers.push(new FuzzySearch(normalize(term), {
14161420
location: options.location,
14171421
threshold: options.threshold,
14181422
distance: options.distance,
14191423
includeMatches: options.includeMatches,
14201424
findAllMatches: options.findAllMatches,
14211425
minMatchCharLength: options.minMatchCharLength,
1422-
isCaseSensitive: options.isCaseSensitive,
1423-
ignoreDiacritics: options.ignoreDiacritics,
1426+
isCaseSensitive: true,
1427+
ignoreDiacritics: false,
14241428
ignoreLocation: true,
14251429
matchAlgorithm: options.matchAlgorithm
14261430
}));
@@ -1437,6 +1441,7 @@ var TokenSearch = class {
14371441
isMatch: false,
14381442
score: 1
14391443
};
1444+
text = this.normalize(text);
14401445
const allIndices = [];
14411446
let weightedScore = 0;
14421447
let maxPossibleScore = 0;

‎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: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,9 +1282,10 @@ var MaxHeap = class {
12821282
return this.heap.length;
12831283
}
12841284
insert(item) {
1285-
if (this.size < this.limit) {
1285+
const len = this.heap.length;
1286+
if (len < this.limit) {
12861287
this.heap.push(item);
1287-
this._bubbleUp(this.size - 1);
1288+
this._bubbleUp(len);
12881289
} else if (this.comparator(item, this.heap[0]) < 0) {
12891290
this.heap[0] = item;
12901291
this._sinkDown(0);
@@ -1294,32 +1295,30 @@ var MaxHeap = class {
12941295
return this.heap.sort(this.comparator);
12951296
}
12961297
_bubbleUp(i) {
1297-
const heap = this.heap;
1298+
const { heap, comparator } = this;
1299+
const item = heap[i];
12981300
while (i > 0) {
12991301
const parent = i - 1 >> 1;
1300-
if (this.comparator(heap[i], heap[parent]) <= 0) break;
1301-
const tmp = heap[i];
1302+
if (comparator(item, heap[parent]) <= 0) break;
13021303
heap[i] = heap[parent];
1303-
heap[parent] = tmp;
13041304
i = parent;
13051305
}
1306+
heap[i] = item;
13061307
}
13071308
_sinkDown(i) {
1308-
const heap = this.heap;
1309+
const { heap, comparator } = this;
13091310
const len = heap.length;
1310-
let largest = i;
1311-
do {
1312-
i = largest;
1311+
const item = heap[i];
1312+
for (;;) {
13131313
const left = 2 * i + 1;
1314-
const right = 2 * i + 2;
1315-
if (left < len && this.comparator(heap[left], heap[largest]) > 0) largest = left;
1316-
if (right < len && this.comparator(heap[right], heap[largest]) > 0) largest = right;
1317-
if (largest !== i) {
1318-
const tmp = heap[i];
1319-
heap[i] = heap[largest];
1320-
heap[largest] = tmp;
1321-
}
1322-
} while (largest !== i);
1314+
if (left >= len) break;
1315+
let next = comparator(heap[left], item) > 0 ? left : -1;
1316+
if (left + 1 < len && comparator(heap[left + 1], next < 0 ? item : heap[left]) > 0) next = left + 1;
1317+
if (next < 0) break;
1318+
heap[i] = heap[next];
1319+
i = next;
1320+
}
1321+
heap[i] = item;
13231322
}
13241323
};
13251324

@@ -1408,18 +1407,23 @@ var TokenSearch = class {
14081407
});
14091408
const queryTerms = this.analyzer.tokenize(pattern);
14101409
const { df, fieldCount } = options._invertedIndex;
1410+
const { isCaseSensitive = Config.isCaseSensitive, ignoreDiacritics = Config.ignoreDiacritics } = options;
1411+
const normalize = this.normalize = (text) => {
1412+
text = isCaseSensitive ? text : text.toLowerCase();
1413+
return ignoreDiacritics ? stripDiacritics(text) : text;
1414+
};
14111415
this.termSearchers = [];
14121416
this.idfWeights = [];
14131417
for (const term of queryTerms) {
1414-
this.termSearchers.push(new FuzzySearch(term, {
1418+
this.termSearchers.push(new FuzzySearch(normalize(term), {
14151419
location: options.location,
14161420
threshold: options.threshold,
14171421
distance: options.distance,
14181422
includeMatches: options.includeMatches,
14191423
findAllMatches: options.findAllMatches,
14201424
minMatchCharLength: options.minMatchCharLength,
1421-
isCaseSensitive: options.isCaseSensitive,
1422-
ignoreDiacritics: options.ignoreDiacritics,
1425+
isCaseSensitive: true,
1426+
ignoreDiacritics: false,
14231427
ignoreLocation: true,
14241428
matchAlgorithm: options.matchAlgorithm
14251429
}));
@@ -1436,6 +1440,7 @@ var TokenSearch = class {
14361440
isMatch: false,
14371441
score: 1
14381442
};
1443+
text = this.normalize(text);
14391444
const allIndices = [];
14401445
let weightedScore = 0;
14411446
let maxPossibleScore = 0;

‎dist/fuse.worker.min.mjs‎

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

0 commit comments

Comments
 (0)