Skip to content

perf(cat,wc): i18n length gate + wc ASCII fast path (1.41x/1.44x -> floor/0.64x vs MSYS) - #1533

Merged
caomengxuan666 merged 1 commit into
mainfrom
perf/baseline-sweep
Nov 7, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
perf/baseline-sweep

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Follow-up to the PERF-BASELINE.md hotspots (branch perf/baseline-sweep, method + raw data in the same tree).

  • cat (src/utils/i18n.cppm): gate translate_legacy at 256B — catalog legacy keys are source-literal short strings (longest in-tree 92B, zh-CN's 1061 keys max 59B), so hashing a whole 64KiB buffer was guaranteed miss + 3 heap allocs. 64MiB cat: 0.318s → 0.122s (−62%), now at the engine copy floor.
  • wc (src/commands/wc.cpp): 7-bit ASCII fast loop mirroring consume_codepoint's ASCII branch, falling back to the UTF-8 decoder on first non-ASCII byte. 64MiB wc: 0.409s → 0.243s (0.64x vs MSYS GNU).
  • Correctness: 315-case three-way differential (GNU / pre / post) — zero divergence; real zh-CN catalog installed → wc short lines still translate, cat bulk output byte-identical. Unit: wc 32/32, i18n 2/2 (+4 new), cat 44/45 (the 1 is a pre-existing env flake reproduced on unmodified base).

Not touched (different layer, documented in PERF-BASELINE.md): cat's ifstream read path and the ~20ms process spawn floor.

…(refs PERF-BASELINE)

cat 64MiB: 0.318s -> 0.122s (-62%, at the engine copy floor; the
translate_legacy FNV-1a hash + 3 heap allocs ran over every 64KiB
block although legacy catalog keys are short literals - the longest
across the tree is 92B, the zh-CN catalog's 1061 keys top out at 59B,
so >256B text can never be a key. Class-level invariant, gated inside
translate_legacy, not bypassed in cat.)

wc 64MiB: 0.409s -> 0.243s (0.64x vs MSYS GNU, was 1.07x): a 7-bit
ASCII fast loop byte-mirrors consume_codepoint's ASCII branch and
hands control back to the UTF-8 decoder at the first >=0x80 byte,
sharing word/line/width state; carry across chunk borders unchanged.

Correctness: 315-case three-way differential (wc: 12 fixtures x 12
option sets x {GNU 5.3.20, pre, post} + 15 cat payload md5s) - zero
divergence, pre == post == GNU; under a real zh-CN catalog wc short
lines still translate and cat bulk output stays byte-identical.
Unit tests: wc 32/32, winuxcmd i18n 2/2, 4 new cases; cat 44/45 with
the one failure (nul_txt_reads_file_issue1333) reproduced on the
unmodified base build - pre-existing environment flake, not this
diff. Full method and tables in PERF-BASELINE.md (after-fix section).
@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — verdict: CAN MERGE.

I read the full diff (5 files, 437 lines) and the CI is now fully green
after the rerun, so both legs check out.

Content. The two fixes are sound and the numbers hold up:

  • translate_legacy 256B gate: the class invariant is real — legacy keys
    are FNV-1a digests of short source literals (longest in-tree 92B), so a

    256B buffer can never match; the gate removes a guaranteed-miss hash +
    allocs per 64KiB chunk. The new test pins both directions (short "1"
    still translates, a 300B bulk text whose digest is in the catalog stays
    verbatim). cat 64MiB 0.318s → 0.122s, at the engine copy floor.

  • wc ASCII fast loop: it inlines exactly the ASCII arms of
    consume_codepoint and stops at the first byte ≥ 0x80, deferring to
    the UTF-8 decoder with its carry logic. I hand-checked the control
    test's expected counts (2 lines / 4 words / 12 chars / 12 bytes /
    max width 3) against the new arms — consistent. wc 64MiB 0.409s →
    0.243s (0.64x vs MSYS). The 150-case three-way matrix reports zero
    divergence.

CI. The first differential run's oracle failure was a savannah
infrastructure flake (gnulib clone HTTP 500, retried, failed again) —
the third occurrence of this signature (#1529, #1532, now #1533), not
PR-related. After gh run rerun --failed, run 42321274345 is all
success (oracle, baseline, baseline-growth, compare), and the CMake
run's x64-default ctest job is success too. i18n-drift is green — the
PR adds no new user-visible strings.

Notes for the merge (not blockers):

  • The PR body has no "本 PR 删除了什么" section. The deletion is the
    old wc printable-run fast path in count_stream, replaced by the new
    ASCII loop — please carry that into the merge body.
  • The one cat unit failure (cat.nul_txt_reads_file_issue1333) is
    reported as pre-existing on the author's machine (fails identically
    with the fixes stashed); it is not a CI leg and not a regression
    signal here.

Merge decision is the user's / Windows side's — I am not merging.

@caomengxuan666
caomengxuan666 merged commit f42959a into main Nov 7, 2026
16 of 17 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 10, 2026
The rust2 bridge drained Rust stdout/stderr through safePrint /
safeErrorPrint, which apply the legacy i18n catalog lookup
(translate_legacy) to the whole payload. For true/false (no output) that
was unobservable, but a data-printing command — basename's result line,
printenv's value, pwd's path — must reach the handle byte-for-byte: an
entry whose FNV-1a key hashes to the output bytes would otherwise
silently replace command DATA. The #1533 length gate only narrows the
lookup; a data output path has to bypass translation entirely.

Add safePrintRaw / safeErrorPrintRaw (mirrors of the translating
writers minus translate_legacy, sharing capture handles, console/file
detection, byte counting, SIGPIPE mapping and write-failure notes) and
route the bridge's drain through them.

- src/utils/console.cppm: add the two raw writers next to the UTF-8
  overloads they mirror.
- src/bridge/rust2_bridge.cppm: drain through the raw writers; comment
  states why translation must not apply here.

Refs #1488
@caomengxuan666
caomengxuan666 deleted the perf/baseline-sweep branch November 18, 2026 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant