Skip to content

perf: 按鍵熱路徑的段級重排快取與更正索引,行為不變 - #51

Merged
kevinwatt merged 7 commits into
0c-biggo:mainfrom
tc3oliver:perf/keystroke-hotpath
Nov 4, 2026
Merged

kevinwatt merged 7 commits into
0c-biggo:mainfrom
tc3oliver:perf/keystroke-hotpath

Conversation

@tc3oliver

Copy link
Copy Markdown
Contributor

問題與結果

長句持續輸入時,每按一鍵都會對整句重跑神經重排的 N-best 與評分,個人更正查詢也逐 span 組字串比對。句子越長越慢,快取命中後剩下的成本仍然不小。

這個 PR 只改成本,不改行為:同樣的按鍵序列,產生的 Snapshot 與 main 逐字節相同。沒有使用者可見的變化,個人資料格式也沒變。

commit 內容
perf(decode) offer 去重 offer 去重時拼好的文字直接傳給新增的 extendText,不再拼第二次
perf(session) 重排段 新增段級快取 rerankMemo:段內輸入(讀音、段內顯示、前文、個人資料版本等)都沒變就重放上次的 winner 與提示,不重跑 N-best。評分器暫時失敗不記錄
perf(personal) 更正比對 有聲調輸入改走既有的 correctionIndex,逐音節核對,不再逐 span 組鍵
perf(session) 補全與語音提示 輸入摘要(sugKey)沒變就沿用上次的補全建議與語音提示
perf(decode) massScale massScale 跨搜尋保留,經選配介面 MassVersion(Model 實作為 (Gen, Store.Time))失效
perf(session) strconv 簽章、rerank 鍵、補全摘要改用 strconv 組字串,不走 fmt
test(session) golden 神經重排的隨機操作 golden:60 個 seed、每個 70 個操作,每個 seed 存輸出的 SHA-256

驗證

沒有下載 assets/,以下全部使用 repo 內的 toy 詞庫。

與 main 的差分:golden 是在分支上產生的,不能自證等價,所以另外對 merge-base(1338efa)做差分。把隨機操作測試的主體複製到 base 的 worktree,兩邊跑同一組 seed、輸出逐行 cmp:

  • 60 與 120 個 seed,兩邊各跑兩次,四組輸出完全相同(60 seed 共 24,849 行,120 seed 共 49,265 行)。
  • 最後一版 golden(每 seed 存 SHA-256)放進 base worktree,TestRerankGoldenTranscript 直接通過。
  • go test ./core/session -run TestRerankGolden -count=2 通過;-update 重寫後 git diff 為空。

審查中找到並已修正的問題:對照 main 檢查快取輸入時,發現兩處會與 main 不同。隨機操作測試沒有抓到,所以是靠讀程式碼確認的:

  • refreshSelection 在 pathSig 不變時換掉 s.path,sugKey 沒收 path,補全建議會沿用舊路徑的結果。現在 sugKey 收整條 path。
  • 段級摘要沒收文件左文 contextLeft、ContextRunes、SeparateContinuation,也沒收 shownPath 詞的位置與旗標。現在都收了。
  • prepareMass 比較評分源身分時,遇到不可比較的型別會 panic。現在遇到就不保留。

新增的 TestRerankSuggestionFresh 檢查沿用的補全建議等於重算結果;它在修正前的程式碼上跑 1500 個 seed 也沒失敗,所以第一點目前沒有能重現的測試。

全套檢查(Linux,tip 為最後一個 commit):

  • python3 scripts/check_public_tree.py:通過
  • go vet ./...:通過
  • go test -count=1 ./...:通過
  • make -C platforms/linux-fcitx5 test:通過
  • make test-upgrade:34 個測試通過
  • make test-release:通過(輸出有一行 fatal: Needed a single revision,來自 test_source_release.py,該套件通過)

Benchmark:base 與分支在同一台機器上交替跑 3 輪取中位數(AMD EPYC 9575F,toy 詞庫):

基準 ns/op base → 分支 B/op allocs/op
RerankTypingLongSentence 33,245,826 → 21,853,828(−34.3%) 8,094,648 → 6,675,593(−17.5%) 127,806 → 81,068(−36.6%)
 其中每鍵 373,549 → 245,549 ns/key(−34.3%)
RerankCachedLongSegment 1,766,472 → 17,203(−99.0%) 969,190 → 7,588(−99.2%) 4,599 → 66(−98.6%)
OverturnsUserLongSegment 99,171 → 99,172(0%) 37,712 → 37,712 571 → 571
CorrectionLookupsAtCapacity 119,613 → 12,851(−89.3%) 17,394 → 1,136(−93.5%) 438 → 40(−90.9%)

沒有基準變慢。否決路徑(OverturnsUserLongSegment)如預期不變。

benchmark 原始輸出(3 輪)
# base
BenchmarkRerankCachedLongSegment-128      	     703	   1733836 ns/op	  969226 B/op	    4599 allocs/op
BenchmarkOverturnsUserLongSegment-128     	   12273	     97933 ns/op	   37713 B/op	     571 allocs/op
BenchmarkRerankTypingLongSentence-128     	      34	  33245826 ns/op	    373549 ns/key	 8095005 B/op	  127807 allocs/op
BenchmarkCorrectionLookupsAtCapacity-128     	   10000	    119613 ns/op	   17394 B/op	     438 allocs/op
BenchmarkRerankCachedLongSegment-128      	     708	   1766472 ns/op	  969190 B/op	    4599 allocs/op
BenchmarkOverturnsUserLongSegment-128     	   12060	     99263 ns/op	   37712 B/op	     571 allocs/op
BenchmarkRerankTypingLongSentence-128     	      34	  33432247 ns/op	    375643 ns/key	 8093543 B/op	  127805 allocs/op
BenchmarkCorrectionLookupsAtCapacity-128     	   10000	    119650 ns/op	   17393 B/op	     438 allocs/op
BenchmarkRerankCachedLongSegment-128      	     674	   1792379 ns/op	  969185 B/op	    4599 allocs/op
BenchmarkOverturnsUserLongSegment-128     	   12105	     99171 ns/op	   37712 B/op	     571 allocs/op
BenchmarkRerankTypingLongSentence-128     	      34	  33234218 ns/op	    373418 ns/key	 8094648 B/op	  127806 allocs/op
BenchmarkCorrectionLookupsAtCapacity-128     	   10000	    104022 ns/op	   17394 B/op	     438 allocs/op

# 分支
BenchmarkRerankCachedLongSegment-128      	   73825	     17203 ns/op	    7588 B/op	      66 allocs/op
BenchmarkOverturnsUserLongSegment-128     	   12169	     98164 ns/op	   37712 B/op	     571 allocs/op
BenchmarkRerankTypingLongSentence-128     	      55	  21748131 ns/op	    244361 ns/key	 6675807 B/op	   81068 allocs/op
BenchmarkCorrectionLookupsAtCapacity-128     	  144424	     12573 ns/op	    1136 B/op	      40 allocs/op
BenchmarkRerankCachedLongSegment-128      	   68916	     17425 ns/op	    7589 B/op	      66 allocs/op
BenchmarkOverturnsUserLongSegment-128     	   10000	    100056 ns/op	   37712 B/op	     571 allocs/op
BenchmarkRerankTypingLongSentence-128     	      56	  21853828 ns/op	    245549 ns/key	 6675141 B/op	   81067 allocs/op
BenchmarkCorrectionLookupsAtCapacity-128     	  137770	     13382 ns/op	    1136 B/op	      40 allocs/op
BenchmarkRerankCachedLongSegment-128      	   72774	     17201 ns/op	    7588 B/op	      66 allocs/op
BenchmarkOverturnsUserLongSegment-128     	   12080	     99172 ns/op	   37712 B/op	     571 allocs/op
BenchmarkRerankTypingLongSentence-128     	      56	  21925570 ns/op	    246355 ns/key	 6675593 B/op	   81068 allocs/op
BenchmarkCorrectionLookupsAtCapacity-128     	  171423	     12851 ns/op	    1136 B/op	      40 allocs/op

指令:

go test ./core/session -bench 'RerankTypingLongSentence|RerankCachedLongSegment|OverturnsUserLongSegment' -run '^$' -benchmem -count=1
go test ./core/personal -bench 'CorrectionLookupsAtCapacity' -run '^$' -benchmem -count=1

尚未驗證:

  • 沒有 assets/:以上數字與差分都用 toy 詞庫,真實詞庫下的加速比與等價性沒測。
  • 真實 onnxruntime scorer 沒測,隨機操作使用確定性的偽評分器。
  • Windows 與 macOS 沒測;make test-upgrade 與 make test-release 只在 Linux 跑。

相容性與證據

  • 個人資料格式、IPC 協定、公共 API 都沒變。
  • MassVersion 是選配介面,第三方 Adaptive 實作不必修改;沒實作的來源維持每次重建。
  • 鍵格式的變動(%.9g 改最短往返表示)只影響記憶體內的快取鍵,沒有寫入檔案或經過 IPC。
  • 新增快取的失效條件:
    • rerankMemo、sugPrev:reset()(提交、取消)清空;輸入摘要含 Gen、Store.Time,個人資料變動就不命中。
    • massScale:(Gen, Store.Time) 或評分源改變、HasAdaptation() 轉為 false 時清空。
  • 沿用的既有防線都在:rerankCache、d.cache TopM、topPrefixed、correctionIndex/habitIndex、Store.Time 的每小時量化、extendShown、neuralKeeps/overturnsUser、weakPrefix。
  • 已知限制(production 都不會碰到):
    • 段級摘要沒收 Rerank.NeutralExpand、NeutralBases 與 Scorer 身分;這些只在開 session 時設定,main 的 rerankCache 鍵也沒收。
    • 換掉 Model.Store 卻不遞增 Gen,快取會過期;production 都在第一次解碼前換好。
  • 摘要裡仍有幾處 fmt.Fprintf,沒有改成 strconv。

之後可以做(這個 PR 不含):decode.extend 的 backpointer、Python prepare() 的 O(c·n²)、IPC 雙寫、個人片語 view.Exact 的線性掃描、Engine.Do 持鎖跨同步評分。建議先用真實資產量過,再決定要不要繼續。

@kevinwatt
kevinwatt merged commit e3c7e93 into 0c-biggo:main Nov 4, 2026
@kevinwatt

Copy link
Copy Markdown
Contributor

已合併,會隨 0.2.9 發布。合併後我們補了四處,用 0.2.8 的資料包和內附神經模型重跑,快照和改動前逐行相同。

  1. 段級重排快取打字時不會命中。 segRerankDigest 收了 len(s.shownSyls),這是整串的長度,每打一個音節前面各段的摘要都會變。BenchmarkRerankTypingLongSentence 的打字流程命中 0/82,真實資料包上的隨機操作 49,873 次只命中 67 次;說明裡那 −34% 其實來自補全沿用、massScale 和更正索引。extendShown 只看 g.End <= min(end, len(shownSyls)),改成 max(start, min(end, len(s.shownSyls))) 後同一流程命中 51 次。真實資料包上神經選字的每鍵引擎時間 5.49 → 2.97 ms,端到端 12.2 → 9.2 ms,p99 85.7 → 61.0 ms。
  2. 更正比對不等價。 exactCorrections 把已存更正和當次手選都送進 pick。原本同鍵的手選會蓋掉已存紀錄,手選的詞在 tombstone 裡時這個鍵整個不算;現在 tombstone 只擋掉手選那筆,已存那筆會生效。重現:Store 有 (ㄕㄜˊ ㄇㄜ˙, 左 ㄨㄛˇ, 右 ㄉㄜ˙) → 甚麼、Tombstones["什麼"],手選「什麼」時「什麼」的成本是 4.0,應該是沒有成本。index 迴圈改成遇到同鍵的手選就跳過。
  3. golden 換成開關快取比對。 存雜湊的 golden 之後每次刻意改 session 行為都得 -update,失敗時也看不出差在哪。現在每個 seed 開快取、關快取各跑一次,兩次快照必須相同,不同時印出第一個不同的位置。
  4. massScale 加上限。 版本不變時跨搜尋保留沒有上限,暫停學習又沒跨小時的話一直累積(約 9 萬個請求後約 10 萬筆)。超過 16,384 筆就清掉重算。

另外,第 1 點修好後快取會真的命中,評分器暫時失敗時就和原本不同:原本 rerankCache 滿 128 筆清空後,下一鍵每段都要重評,失敗的段落會暫時退回統計結果;段級快取直接重放上次的決定。說明裡的「逐字節相同」只在評分器沒失敗時成立。

@kevinwatt

Copy link
Copy Markdown
Contributor

已隨 0.2.9 發布。正式套件上用 350 句(10,343 鍵)加 120 句含游標移動、刪除與開候選的操作,0.2.8 與 0.2.9 交替跑兩輪,送出的字逐字相同;每鍵 p99 從約 45–52 ms 降到 37–42 ms。

pohsun4work pushed a commit to pohsun4work/0c-ime that referenced this pull request Nov 6, 2026
…keystrokes

Sync ime 0.2.9 (PR 0c-biggo#51 is already here, so its ime port is not repeated;
ime's review follow-ups on it are included): "My vocabulary" exports to a
text file and imports on another computer, a new optional
shift-symbol-halfwidth setting types half-width punctuation with Shift
(0c-biggo#50), mixed input composes a medial or final typed one key late by
position (20u4 電, 9j4 外, u1,6 別), and the segment rerank cache, the
correctionIndex and the kept massScale (capped at 16,384) nearly halve the
per-key engine time with the neural reranker; the golden transcript test
becomes a caches on/off comparison per seed. Windows links the C runtime
statically for x64 too, a changed previous version no longer blocks
installing, the keyboard compartment is opened on activation for IMM32
programs, IMM32 text boxes get English autocomplete and Ctrl shortcuts in
the right order, Caps Lock lower case works without a caret, and the
status summary lists recent programs from tsf.log. Dictionary and models
are unchanged.
@tc3oliver
tc3oliver deleted the perf/keystroke-hotpath branch November 13, 2026 05:06
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.

2 participants