Skip to content

Commit db9d161

Browse files
committed
perf(index): count words once per text and cache norm ids by word count
A text's field-length norm depends only on its word count, so the index now caches norm ids by word count: most texts need one array read instead of an exact-value lookup. Word counting is split out of the norm as countWords(), with the separator test written inline (when it was a helper, V8 stopped inlining it once the counter was inlined into the object build, making object builds ~25% slower). The norm's per-count cache is an array instead of a Map. Norms read from records (parseIndex, setIndexRecords) still use the exact-value table. toJSON() output is byte-identical to before (800 randomized comparisons: tabs, NBSP, blanks, add(), fieldNormWeight 0.5/1/2). vs main, 5 alternating rounds: string-list builds 1.19-1.66x faster, object builds 1.08-1.19x, token-search builds up to 1.16x, add/remove 5-11% faster; search and parseIndex unchanged. vs 7.5.0, builds are now equal or faster except index-creation's 100k string list (about 20% slower, ~1.5 ms), which tracks V8 heap state rather than this code path and is left for a follow-up. NormInterface gains an optional fromCount(); a norm without it (a custom { get, clear } assigned to FuseIndex.norm) is still honored via get(), and replacing the norm invalidates the cached ids.
1 parent 24fc39b commit db9d161

16 files changed

Lines changed: 436 additions & 214 deletions

‎dist/fuse.basic.cjs‎

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -196,30 +196,38 @@ const Config = Object.freeze({
196196

197197
//#endregion
198198
//#region src/tools/fieldNorm.ts
199-
function isWordSeparator(code) {
200-
return code >= 9 && code <= 13 || code === 32 || code === 160;
199+
function countWords(value) {
200+
let numTokens = 0;
201+
let inWord = false;
202+
for (let i = 0, len = value.length; i < len; i++) {
203+
const code = value.charCodeAt(i);
204+
if (code > 32 ? code !== 160 : code !== 32 && (code < 9 || code > 13)) {
205+
if (!inWord) {
206+
numTokens++;
207+
inWord = true;
208+
}
209+
} else inWord = false;
210+
}
211+
if (numTokens === 0) numTokens = 1;
212+
return numTokens;
201213
}
202214
function norm(weight = 1, mantissa = 3) {
203-
const cache = /* @__PURE__ */ new Map();
215+
let cache = [];
204216
const m = Math.pow(10, mantissa);
217+
const fromCount = (numTokens) => {
218+
const cached = cache[numTokens];
219+
if (cached !== void 0) return cached;
220+
const n = Math.round(m / Math.pow(numTokens, .5 * weight)) / m;
221+
cache[numTokens] = n;
222+
return n;
223+
};
205224
return {
206225
get(value) {
207-
let numTokens = 0;
208-
let inWord = false;
209-
for (let i = 0; i < value.length; i++) if (!isWordSeparator(value.charCodeAt(i))) {
210-
if (!inWord) {
211-
numTokens++;
212-
inWord = true;
213-
}
214-
} else inWord = false;
215-
if (numTokens === 0) numTokens = 1;
216-
if (cache.has(numTokens)) return cache.get(numTokens);
217-
const n = Math.round(m / Math.pow(numTokens, .5 * weight)) / m;
218-
cache.set(numTokens, n);
219-
return n;
226+
return fromCount(countWords(value));
220227
},
228+
fromCount,
221229
clear() {
222-
cache.clear();
230+
cache = [];
223231
}
224232
};
225233
}
@@ -246,6 +254,8 @@ var FuseIndex = class FuseIndex {
246254
this._normValues = [];
247255
this._normIds = /* @__PURE__ */ new Map();
248256
this._normCache = (/* @__PURE__ */ new Int32Array(1024)).fill(-1);
257+
this._idByCount = [];
258+
this._idByCountNorm = null;
249259
this._entValue = [];
250260
this.norm = norm(fieldNormWeight, 3);
251261
this.getFn = getFn;
@@ -314,6 +324,8 @@ var FuseIndex = class FuseIndex {
314324
copy._normValues = this._normValues.slice();
315325
copy._normIds = new Map(this._normIds);
316326
copy._normCache = this._normCache.slice();
327+
copy._idByCount = this._idByCount.slice();
328+
copy._idByCountNorm = this._idByCountNorm;
317329
copy._entValue = this._entValue.slice(0, this._entCount);
318330
return copy;
319331
}
@@ -350,7 +362,7 @@ var FuseIndex = class FuseIndex {
350362
recLen[n] = 1;
351363
entKey[n] = 0;
352364
entIdx[n] = -1;
353-
entNormId[n] = this._normIdOf(this.norm.get(doc));
365+
entNormId[n] = this._textNormId(doc);
354366
entValue[n] = doc;
355367
n += 1;
356368
}
@@ -477,7 +489,7 @@ var FuseIndex = class FuseIndex {
477489
_addString(doc, docIndex) {
478490
if (!isDefined(doc) || isBlank(doc)) return false;
479491
this._beginRecord(docIndex, 0);
480-
this._pushEntry(0, -1, this.norm.get(doc), doc);
492+
this._pushEntry(0, -1, this._textNormId(doc), doc);
481493
this._endRecord();
482494
return true;
483495
}
@@ -493,21 +505,21 @@ var FuseIndex = class FuseIndex {
493505
const item = value[i];
494506
if (!isDefined(item)) continue;
495507
if (isString(item)) {
496-
if (!isBlank(item)) this._pushEntry(keyIndex, i, this.norm.get(item), item);
508+
if (!isBlank(item)) this._pushEntry(keyIndex, i, this._textNormId(item), item);
497509
} else if (isDefined(item.v)) {
498510
const text = isString(item.v) ? item.v : toString(item.v);
499-
if (!isBlank(text)) this._pushEntry(keyIndex, item.i, this.norm.get(text), text);
511+
if (!isBlank(text)) this._pushEntry(keyIndex, item.i, this._textNormId(text), text);
500512
}
501513
}
502-
if (this._entCount === before) this._pushEntry(keyIndex, -2, 0, "");
503-
} else if (isString(value) && !isBlank(value)) this._pushEntry(keyIndex, -1, this.norm.get(value), value);
514+
if (this._entCount === before) this._pushEntry(keyIndex, -2, this._normIdOf(0), "");
515+
} else if (isString(value) && !isBlank(value)) this._pushEntry(keyIndex, -1, this._textNormId(value), value);
504516
}
505517
this._endRecord();
506518
}
507519
_appendRecord(record) {
508520
if (record.v !== void 0) {
509521
this._beginRecord(checkPosition(record.i), 0);
510-
this._pushEntry(0, -1, record.n, record.v);
522+
this._pushEntry(0, -1, this._normIdOf(record.n), record.v);
511523
this._endRecord();
512524
return;
513525
}
@@ -518,9 +530,9 @@ var FuseIndex = class FuseIndex {
518530
const value = fields[key];
519531
if (!isDefined(value)) continue;
520532
if (isArray(value)) {
521-
if (!value.length) this._pushEntry(key, -2, 0, "");
522-
for (const sub of value) this._pushEntry(key, checkPosition(sub.i), sub.n, sub.v);
523-
} else this._pushEntry(key, -1, value.n, value.v);
533+
if (!value.length) this._pushEntry(key, -2, this._normIdOf(0), "");
534+
for (const sub of value) this._pushEntry(key, checkPosition(sub.i), this._normIdOf(sub.n), sub.v);
535+
} else this._pushEntry(key, -1, this._normIdOf(value.n), value.v);
524536
}
525537
this._endRecord();
526538
}
@@ -537,6 +549,7 @@ var FuseIndex = class FuseIndex {
537549
this._entIdx = new Int32Array(this._entKey.length);
538550
this._entNormId = new Uint32Array(this._entKey.length);
539551
this._normValues = [];
552+
this._idByCount = [];
540553
this._normIds = /* @__PURE__ */ new Map();
541554
this._normCache.fill(-1);
542555
this._entValue = [];
@@ -568,19 +581,35 @@ var FuseIndex = class FuseIndex {
568581
this._recLen[r] = this._entCount - this._recStart[r];
569582
this._recCount += 1;
570583
}
571-
_pushEntry(key, pos, norm, value) {
584+
_pushEntry(key, pos, normId, value) {
572585
if (this._entCount === this._entKey.length) this._growEntries();
573586
const e = this._entCount;
574587
this._entKey[e] = key;
575588
this._entIdx[e] = pos;
576-
this._entNormId[e] = this._normIdOf(norm);
589+
this._entNormId[e] = normId;
577590
this._entValue[e] = value;
578591
this._entCount += 1;
579592
}
593+
_textNormId(text) {
594+
const { norm } = this;
595+
if (!norm.fromCount) return this._normIdOf(norm.get(text));
596+
if (norm !== this._idByCountNorm) {
597+
this._idByCount = [];
598+
this._idByCountNorm = norm;
599+
}
600+
const count = countWords(text);
601+
let id = this._idByCount[count];
602+
if (id === void 0) {
603+
id = this._normIdOf(norm.fromCount(count));
604+
this._idByCount[count] = id;
605+
}
606+
return id;
607+
}
580608
_normIdOf(norm) {
581609
const slot = norm * 1048576 & 1023;
582610
let normId = this._normCache[slot];
583-
if (normId < 0 || !Object.is(this._normValues[normId], norm)) {
611+
const hit = normId >= 0 ? this._normValues[normId] : void 0;
612+
if (hit !== norm || norm === 0 && 1 / hit !== 1 / norm) {
584613
const key = Object.is(norm, -0) ? NEGATIVE_ZERO_KEY : norm;
585614
let id = this._normIds.get(key);
586615
if (id === void 0) {

‎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: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -195,30 +195,38 @@ const Config = Object.freeze({
195195

196196
//#endregion
197197
//#region src/tools/fieldNorm.ts
198-
function isWordSeparator(code) {
199-
return code >= 9 && code <= 13 || code === 32 || code === 160;
198+
function countWords(value) {
199+
let numTokens = 0;
200+
let inWord = false;
201+
for (let i = 0, len = value.length; i < len; i++) {
202+
const code = value.charCodeAt(i);
203+
if (code > 32 ? code !== 160 : code !== 32 && (code < 9 || code > 13)) {
204+
if (!inWord) {
205+
numTokens++;
206+
inWord = true;
207+
}
208+
} else inWord = false;
209+
}
210+
if (numTokens === 0) numTokens = 1;
211+
return numTokens;
200212
}
201213
function norm(weight = 1, mantissa = 3) {
202-
const cache = /* @__PURE__ */ new Map();
214+
let cache = [];
203215
const m = Math.pow(10, mantissa);
216+
const fromCount = (numTokens) => {
217+
const cached = cache[numTokens];
218+
if (cached !== void 0) return cached;
219+
const n = Math.round(m / Math.pow(numTokens, .5 * weight)) / m;
220+
cache[numTokens] = n;
221+
return n;
222+
};
204223
return {
205224
get(value) {
206-
let numTokens = 0;
207-
let inWord = false;
208-
for (let i = 0; i < value.length; i++) if (!isWordSeparator(value.charCodeAt(i))) {
209-
if (!inWord) {
210-
numTokens++;
211-
inWord = true;
212-
}
213-
} else inWord = false;
214-
if (numTokens === 0) numTokens = 1;
215-
if (cache.has(numTokens)) return cache.get(numTokens);
216-
const n = Math.round(m / Math.pow(numTokens, .5 * weight)) / m;
217-
cache.set(numTokens, n);
218-
return n;
225+
return fromCount(countWords(value));
219226
},
227+
fromCount,
220228
clear() {
221-
cache.clear();
229+
cache = [];
222230
}
223231
};
224232
}
@@ -245,6 +253,8 @@ var FuseIndex = class FuseIndex {
245253
this._normValues = [];
246254
this._normIds = /* @__PURE__ */ new Map();
247255
this._normCache = (/* @__PURE__ */ new Int32Array(1024)).fill(-1);
256+
this._idByCount = [];
257+
this._idByCountNorm = null;
248258
this._entValue = [];
249259
this.norm = norm(fieldNormWeight, 3);
250260
this.getFn = getFn;
@@ -313,6 +323,8 @@ var FuseIndex = class FuseIndex {
313323
copy._normValues = this._normValues.slice();
314324
copy._normIds = new Map(this._normIds);
315325
copy._normCache = this._normCache.slice();
326+
copy._idByCount = this._idByCount.slice();
327+
copy._idByCountNorm = this._idByCountNorm;
316328
copy._entValue = this._entValue.slice(0, this._entCount);
317329
return copy;
318330
}
@@ -349,7 +361,7 @@ var FuseIndex = class FuseIndex {
349361
recLen[n] = 1;
350362
entKey[n] = 0;
351363
entIdx[n] = -1;
352-
entNormId[n] = this._normIdOf(this.norm.get(doc));
364+
entNormId[n] = this._textNormId(doc);
353365
entValue[n] = doc;
354366
n += 1;
355367
}
@@ -476,7 +488,7 @@ var FuseIndex = class FuseIndex {
476488
_addString(doc, docIndex) {
477489
if (!isDefined(doc) || isBlank(doc)) return false;
478490
this._beginRecord(docIndex, 0);
479-
this._pushEntry(0, -1, this.norm.get(doc), doc);
491+
this._pushEntry(0, -1, this._textNormId(doc), doc);
480492
this._endRecord();
481493
return true;
482494
}
@@ -492,21 +504,21 @@ var FuseIndex = class FuseIndex {
492504
const item = value[i];
493505
if (!isDefined(item)) continue;
494506
if (isString(item)) {
495-
if (!isBlank(item)) this._pushEntry(keyIndex, i, this.norm.get(item), item);
507+
if (!isBlank(item)) this._pushEntry(keyIndex, i, this._textNormId(item), item);
496508
} else if (isDefined(item.v)) {
497509
const text = isString(item.v) ? item.v : toString(item.v);
498-
if (!isBlank(text)) this._pushEntry(keyIndex, item.i, this.norm.get(text), text);
510+
if (!isBlank(text)) this._pushEntry(keyIndex, item.i, this._textNormId(text), text);
499511
}
500512
}
501-
if (this._entCount === before) this._pushEntry(keyIndex, -2, 0, "");
502-
} else if (isString(value) && !isBlank(value)) this._pushEntry(keyIndex, -1, this.norm.get(value), value);
513+
if (this._entCount === before) this._pushEntry(keyIndex, -2, this._normIdOf(0), "");
514+
} else if (isString(value) && !isBlank(value)) this._pushEntry(keyIndex, -1, this._textNormId(value), value);
503515
}
504516
this._endRecord();
505517
}
506518
_appendRecord(record) {
507519
if (record.v !== void 0) {
508520
this._beginRecord(checkPosition(record.i), 0);
509-
this._pushEntry(0, -1, record.n, record.v);
521+
this._pushEntry(0, -1, this._normIdOf(record.n), record.v);
510522
this._endRecord();
511523
return;
512524
}
@@ -517,9 +529,9 @@ var FuseIndex = class FuseIndex {
517529
const value = fields[key];
518530
if (!isDefined(value)) continue;
519531
if (isArray(value)) {
520-
if (!value.length) this._pushEntry(key, -2, 0, "");
521-
for (const sub of value) this._pushEntry(key, checkPosition(sub.i), sub.n, sub.v);
522-
} else this._pushEntry(key, -1, value.n, value.v);
532+
if (!value.length) this._pushEntry(key, -2, this._normIdOf(0), "");
533+
for (const sub of value) this._pushEntry(key, checkPosition(sub.i), this._normIdOf(sub.n), sub.v);
534+
} else this._pushEntry(key, -1, this._normIdOf(value.n), value.v);
523535
}
524536
this._endRecord();
525537
}
@@ -536,6 +548,7 @@ var FuseIndex = class FuseIndex {
536548
this._entIdx = new Int32Array(this._entKey.length);
537549
this._entNormId = new Uint32Array(this._entKey.length);
538550
this._normValues = [];
551+
this._idByCount = [];
539552
this._normIds = /* @__PURE__ */ new Map();
540553
this._normCache.fill(-1);
541554
this._entValue = [];
@@ -567,19 +580,35 @@ var FuseIndex = class FuseIndex {
567580
this._recLen[r] = this._entCount - this._recStart[r];
568581
this._recCount += 1;
569582
}
570-
_pushEntry(key, pos, norm, value) {
583+
_pushEntry(key, pos, normId, value) {
571584
if (this._entCount === this._entKey.length) this._growEntries();
572585
const e = this._entCount;
573586
this._entKey[e] = key;
574587
this._entIdx[e] = pos;
575-
this._entNormId[e] = this._normIdOf(norm);
588+
this._entNormId[e] = normId;
576589
this._entValue[e] = value;
577590
this._entCount += 1;
578591
}
592+
_textNormId(text) {
593+
const { norm } = this;
594+
if (!norm.fromCount) return this._normIdOf(norm.get(text));
595+
if (norm !== this._idByCountNorm) {
596+
this._idByCount = [];
597+
this._idByCountNorm = norm;
598+
}
599+
const count = countWords(text);
600+
let id = this._idByCount[count];
601+
if (id === void 0) {
602+
id = this._normIdOf(norm.fromCount(count));
603+
this._idByCount[count] = id;
604+
}
605+
return id;
606+
}
579607
_normIdOf(norm) {
580608
const slot = norm * 1048576 & 1023;
581609
let normId = this._normCache[slot];
582-
if (normId < 0 || !Object.is(this._normValues[normId], norm)) {
610+
const hit = normId >= 0 ? this._normValues[normId] : void 0;
611+
if (hit !== norm || norm === 0 && 1 / hit !== 1 / norm) {
583612
const key = Object.is(norm, -0) ? NEGATIVE_ZERO_KEY : norm;
584613
let id = this._normIds.get(key);
585614
if (id === void 0) {

0 commit comments

Comments
 (0)