Skip to content

[perf] 增量 tokenize 工单:lexer 每行全缓冲重扫确证 O(N²)(#130 热点已定位,nvm.sh 形态 1469×) #155

Closed

Description

@caomengxuan666

证据(target-perf/REPORT.md,release 构建,双侧同构 EPOCHREALTIME 计时)

  • 构造法二分:顶层并列数量线性、函数体线性、嵌套深度超二次;"单个 { 组跨 N 行"(nvm.sh 形态)纯二次:1000/2000/4000 行 = 896/3588/14263ms(行数 ×2 → 时间 ×4.0),GNU 9.7ms → 1469×。
  • 主热点:src/lexer/mod.rs:351 每读入一条物理行就对整个累积缓冲区重新 tokenize_plain(mod.rs:340-345 注释自认此设计);叠加 mod.rs:318/321/326 三个 O(L) 全缓冲 has_unclosed_* 扫描。
  • 次因:support.rs:308 matching_brace_group_end 嵌套重扫。

修复方向

增量 tokenize:LexerParseState 重放机制已在,缺增量分词——新行只 tokenize 增量部分并续接到既有状态。与 #131(extglob 解析期门控)的"按命令边界增量解析"设计同族,可共用车道。补全链 source 30× 的数字也随本项回落。关联 #130(已评论热点确证)。

Activity

  1. added
    enhancement
    on Sep 26, 2026
  2. caomengxuan666 commented on Sep 26, 2026

    @caomengxuan666
    MemberAuthor

    Fixed on wt/e-lexer @7a2a1e5e (rubash#155 主热点;#130 的 onegrp/nvm 形态随之消除).

    Fix: admission-whitelist fast path in tokenize_with_heredocs — while the previous pass joined via the token-level brace-group signal (which proves quotes/comsub/compound were all closed), a physical line with no quote/escape/expansion/brace/paren/comment/heredoc bytes (' " $ { } ( ) # < \) and noposixsubstring skips the O(buffer) re-tokenize and the three has_unclosed_* full scans entirely. GNU owner: parse.y:3557 read_token streaming model. All intermediate results were already discarded by the joincontinue`; the accepted stream still comes from the unchanged final full pass, so outputs are byte-identical by construction.

    Numbers (release, median-3, -n):

    onegrp before after
    1000 896ms 73ms
    2000 3588ms 95ms
    4000 14263ms 120ms (119x, GNU 9.7ms)
    8000 — 205ms (net-of-startup scaling linear)

    fb/plain/blk unchanged.

    Verification: 452-file completions-core -n iso matrix 452/452 identical before/after; #130 four shapes + rich/comsub/case/unclosed/CRLF fixtures byte-identical before/after (run + -n + stdin); WSL GNU 5.3.0 stdout+rc identical; lib tests 458 green (5 new). Artifacts: target/issue-suites/results/e-lexer/ (shapes/, rubash-before.exe, matrix-diff.txt) in the wt/e-lexer worktree.

    Remaining (this issue's secondary cause, root cause corrected): the nst nested-depth shape is NOT matching_brace_group_end — the folded-group path (brace_command.rs:29) bypasses it. Measured: nst100 one-line=130ms vs two-line=506ms (release); the near-cubic cost comes from per-{ skip_brace-to-EOF scans inside a single join pass (scanner.rs { branch), summed over the fold-parse recursion levels — needs a resumable skip_brace scanner restructure (separate task).

  3. caomengxuan666 commented on Sep 26, 2026

    @caomengxuan666
    MemberAuthor

    已在 cbfbebd 落地(7a2a1e5e):准入白名单快路径(rubash#117 规则——白名单而非症状黑名单)——上一轮 join 由 token 级 brace-group 信号触发且新行不含任何可改变判定的字节时,整段跳过 O(缓冲区) 重扫;输出按构造字节不变(中间产物本就被丢弃)。nvm.sh 形态 14263ms → 120ms(119×,线性,GNU 9.7ms,验收 4× 裕量);before/after 二进制输出字节全同(run/-n/stdin 交互 × 四形态 fixture);452 文件 -n iso 矩阵 452/452 全同;461 lib 测试(5 新)。残余:嵌套深度维度(nst)根因已纠正并另单跟踪(真因=单遍内逐 { 的 skip_brace 扫到 EOF 在折叠解析递归各层求和的近三次方,需 skip_brace 可续传重构)。#131 的准入模式可复用性分析见评论区。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions