mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 21:31:59 +03:00
⚡ [performance improvement] optimize token scanning in tryParseGoalSeek
Replaced dual array scans (`Array.prototype.some` and `Array.prototype.findIndex`) in `tryParseGoalSeek` with a single `for` loop that implements early bail-out logic once both the assignment operator and the unknown variable are found. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
37cfd2144b
commit
bf9183269b
1 changed files with 17 additions and 7 deletions
|
|
@ -98,15 +98,25 @@ function filterNoiseTokens(toks: Token[], knownVars: Set<string>): Token[] {
|
||||||
|
|
||||||
function tryParseGoalSeek(filtered: Token[], knownVars: Set<string>): Node | null {
|
function tryParseGoalSeek(filtered: Token[], knownVars: Set<string>): Node | null {
|
||||||
// Goal seek: `? * 1.2 = 1000` → find x where lhs(x) = rhs(x)
|
// Goal seek: `? * 1.2 = 1000` → find x where lhs(x) = rhs(x)
|
||||||
const hasUnknown = filtered.some((tk) => tk.t === "unknown");
|
let hasUnknown = false;
|
||||||
if (hasUnknown) {
|
let assignIdx = -1;
|
||||||
const assignIdx = filtered.findIndex((tk) => tk.t === "assign");
|
const len = filtered.length;
|
||||||
if (assignIdx >= 0) {
|
for (let i = 0; i < len; i++) {
|
||||||
const lhs = new Parser(filtered.slice(0, assignIdx), knownVars).parseSeq();
|
const t = filtered[i].t;
|
||||||
const rhs = new Parser(filtered.slice(assignIdx + 1), knownVars).parseSeq();
|
if (t === "unknown") {
|
||||||
if (lhs && rhs) return { k: "goalseek", lhs, rhs };
|
hasUnknown = true;
|
||||||
|
if (assignIdx !== -1) break;
|
||||||
|
} else if (t === "assign" && assignIdx === -1) {
|
||||||
|
assignIdx = i;
|
||||||
|
if (hasUnknown) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hasUnknown && assignIdx >= 0) {
|
||||||
|
const lhs = new Parser(filtered.slice(0, assignIdx), knownVars).parseSeq();
|
||||||
|
const rhs = new Parser(filtered.slice(assignIdx + 1), knownVars).parseSeq();
|
||||||
|
if (lhs && rhs) return { k: "goalseek", lhs, rhs };
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue