mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 02:12:00 +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 {
|
||||
// Goal seek: `? * 1.2 = 1000` → find x where lhs(x) = rhs(x)
|
||||
const hasUnknown = filtered.some((tk) => tk.t === "unknown");
|
||||
if (hasUnknown) {
|
||||
const assignIdx = filtered.findIndex((tk) => tk.t === "assign");
|
||||
if (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 };
|
||||
let hasUnknown = false;
|
||||
let assignIdx = -1;
|
||||
const len = filtered.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
const t = filtered[i].t;
|
||||
if (t === "unknown") {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue