mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 02:12:00 +03:00
perf: defer document string split allocation in numeric search
In `src/ui/search.ts`, numeric threshold searches previously called `doc.text.split("\n")` eagerly on every open document. This commit changes `lines` to be lazily allocated only if an actual numeric search match is found, eliminating unnecessary memory allocations and loop overhead on documents that do not match.
Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
868a34497b
commit
6297c66f84
1 changed files with 42 additions and 12 deletions
|
|
@ -30,9 +30,13 @@ export interface SearchController {
|
||||||
open(): void;
|
open(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const $ = <T extends HTMLElement>(sel: string): T => document.querySelector(sel) as T;
|
const $ = <T extends HTMLElement>(sel: string): T =>
|
||||||
|
document.querySelector(sel) as T;
|
||||||
|
|
||||||
export function parseResultQuery(engine: SumEngine, q: string): { op: string; threshold: Value } | null {
|
export function parseResultQuery(
|
||||||
|
engine: SumEngine,
|
||||||
|
q: string,
|
||||||
|
): { op: string; threshold: Value } | null {
|
||||||
const m = /^(>=|<=|>|<|=|~)\s*(.+)$/.exec(q.trim());
|
const m = /^(>=|<=|>|<|=|~)\s*(.+)$/.exec(q.trim());
|
||||||
if (!m) return null;
|
if (!m) return null;
|
||||||
const v = engine.evaluateExpression(m[2].trim());
|
const v = engine.evaluateExpression(m[2].trim());
|
||||||
|
|
@ -50,22 +54,41 @@ export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
|
||||||
const hits: SearchHit[] = [];
|
const hits: SearchHit[] = [];
|
||||||
for (const doc of docs) {
|
for (const doc of docs) {
|
||||||
const results = deps.workspace.getCachedResults(doc);
|
const results = deps.workspace.getCachedResults(doc);
|
||||||
const lines = doc.text.split("\n");
|
let lines: string[] | undefined;
|
||||||
for (let i = 0; i < results.length; i++) {
|
for (let i = 0; i < results.length; i++) {
|
||||||
const r = results[i];
|
const r = results[i];
|
||||||
if (!r.value || r.value.kind !== "quantity") continue;
|
if (!r.value || r.value.kind !== "quantity") continue;
|
||||||
const v = r.value.value;
|
const v = r.value.value;
|
||||||
let match = false;
|
let match = false;
|
||||||
switch (rq.op) {
|
switch (rq.op) {
|
||||||
case ">": match = v.gt(th); break;
|
case ">":
|
||||||
case ">=": match = v.gte(th); break;
|
match = v.gt(th);
|
||||||
case "<": match = v.lt(th); break;
|
break;
|
||||||
case "<=": match = v.lte(th); break;
|
case ">=":
|
||||||
case "=": match = v.eq(th); break;
|
match = v.gte(th);
|
||||||
case "~": match = !th.isZero() && v.minus(th).abs().div(th.abs()).lte(0.01); break;
|
break;
|
||||||
|
case "<":
|
||||||
|
match = v.lt(th);
|
||||||
|
break;
|
||||||
|
case "<=":
|
||||||
|
match = v.lte(th);
|
||||||
|
break;
|
||||||
|
case "=":
|
||||||
|
match = v.eq(th);
|
||||||
|
break;
|
||||||
|
case "~":
|
||||||
|
match = !th.isZero() && v.minus(th).abs().div(th.abs()).lte(0.01);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (match) {
|
if (match) {
|
||||||
hits.push({ docId: doc.id, docTitle: doc.title, line: i + 1, text: lines[i] ?? "", result: r.text ?? undefined });
|
if (!lines) lines = doc.text.split("\n");
|
||||||
|
hits.push({
|
||||||
|
docId: doc.id,
|
||||||
|
docTitle: doc.title,
|
||||||
|
line: i + 1,
|
||||||
|
text: lines[i] ?? "",
|
||||||
|
result: r.text ?? undefined,
|
||||||
|
});
|
||||||
if (hits.length >= 200) return hits;
|
if (hits.length >= 200) return hits;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -78,7 +101,12 @@ export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
|
||||||
const lines = doc.text.split("\n");
|
const lines = doc.text.split("\n");
|
||||||
for (let i = 0; i < lines.length; i++) {
|
for (let i = 0; i < lines.length; i++) {
|
||||||
if (lines[i].toLowerCase().includes(ql)) {
|
if (lines[i].toLowerCase().includes(ql)) {
|
||||||
hits.push({ docId: doc.id, docTitle: doc.title, line: i + 1, text: lines[i] });
|
hits.push({
|
||||||
|
docId: doc.id,
|
||||||
|
docTitle: doc.title,
|
||||||
|
line: i + 1,
|
||||||
|
text: lines[i],
|
||||||
|
});
|
||||||
if (hits.length >= 200) return hits;
|
if (hits.length >= 200) return hits;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -113,7 +141,9 @@ export function initSearch(deps: SearchDeps): SearchController {
|
||||||
docEl.textContent = hit.docTitle;
|
docEl.textContent = hit.docTitle;
|
||||||
const lineEl = document.createElement("div");
|
const lineEl = document.createElement("div");
|
||||||
lineEl.className = "line";
|
lineEl.className = "line";
|
||||||
lineEl.textContent = hit.result ? `${hit.text} = ${hit.result}` : hit.text;
|
lineEl.textContent = hit.result
|
||||||
|
? `${hit.text} = ${hit.result}`
|
||||||
|
: hit.text;
|
||||||
item.append(docEl, lineEl);
|
item.append(docEl, lineEl);
|
||||||
item.addEventListener("click", () => {
|
item.addEventListener("click", () => {
|
||||||
deps.onOpen(hit.docId, hit.line);
|
deps.onOpen(hit.docId, hit.line);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue