-
Notifications
You must be signed in to change notification settings - Fork 219
perf(ui): optimize terminal cell width measurement (#579) #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "hunkdiff": patch | ||
| --- | ||
|
|
||
| Optimize terminal cell width measurement so diffs with CJK, emoji, and chrome-glyph runs render faster: single-scalar clusters now measure through a fast zero-width check plus the East Asian Width table instead of string-width's expensive emoji regexes, while multi-scalar clusters still defer to string-width for identical results. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Benchmark Hunk's scalar fast path and complex-cluster fallback against string-width. | ||
| import { performance } from "node:perf_hooks"; | ||
| import stringWidth from "string-width"; | ||
| import { measureTextWidth } from "../src/ui/lib/text"; | ||
|
|
||
| const ITERATIONS = 2_000; | ||
| const WARMUP_ITERATIONS = 50; | ||
| const CJK_SCALAR_LINES = [ | ||
| "export const message = 日本語のコメントです。変更内容を確認してください。", | ||
| "export function 測定値を計算する(入力: number) { return 入力 * 2; }", | ||
| "中文注释内容:这个变更优化了终端单元格宽度测量。", | ||
| "请检查新增、删除和未修改的代码行是否正确对齐。", | ||
| "한국어 주석 내용과 함수 이름을 함께 측정합니다.", | ||
| "터미널 셀 너비를 빠르게 계산하고 정렬을 유지합니다.", | ||
| ] as const; | ||
| const EMOJI_SCALAR_LINES = [ | ||
| "🚀 ✨ 🔧 💡 🎯 📦 🔍 standalone emoji scalars", | ||
| "✅ 🚧 🐛 🎉 🧪 📊 terminal status glyphs", | ||
| ] as const; | ||
| const COMPLEX_CLUSTER_LINES = [ | ||
| "🧑💻 👩🔬 👨👩👧👦 complex emoji clusters stay aligned", | ||
| "e\u0301 a\u0308 o\u0302 u\u0308 combining clusters keep their reference widths", | ||
| ] as const; | ||
|
|
||
| type WidthMeasure = (text: string) => number; | ||
| type WidthCorpus = readonly string[]; | ||
|
|
||
| /** Measure repeated width calls and retain their total so the work stays observable. */ | ||
| function measureWidthCalls(measure: WidthMeasure, corpus: WidthCorpus, iterations: number) { | ||
| let checksum = 0; | ||
| const start = performance.now(); | ||
|
|
||
| for (let iteration = 0; iteration < iterations; iteration += 1) { | ||
| for (const line of corpus) { | ||
| checksum += measure(line); | ||
| } | ||
| } | ||
|
|
||
| return { elapsedMs: performance.now() - start, checksum }; | ||
| } | ||
|
|
||
| /** Verify and time one deterministic terminal-text shape. */ | ||
| function measureScenario(name: string, corpus: WidthCorpus) { | ||
| for (const line of corpus) { | ||
| const actual = measureTextWidth(line); | ||
| const reference = stringWidth(line); | ||
| if (actual !== reference) { | ||
| throw new Error(`Width mismatch for ${JSON.stringify(line)}: ${actual} !== ${reference}`); | ||
| } | ||
| } | ||
|
|
||
| measureWidthCalls(stringWidth, corpus, WARMUP_ITERATIONS); | ||
| measureWidthCalls(measureTextWidth, corpus, WARMUP_ITERATIONS); | ||
|
|
||
| const reference = measureWidthCalls(stringWidth, corpus, ITERATIONS); | ||
| const optimized = measureWidthCalls(measureTextWidth, corpus, ITERATIONS); | ||
| if (optimized.checksum !== reference.checksum) { | ||
| throw new Error(`Width checksum mismatch: ${optimized.checksum} !== ${reference.checksum}`); | ||
| } | ||
|
|
||
| const speedup = reference.elapsedMs / optimized.elapsedMs; | ||
| console.log(`METRIC ${name}_text_width_ms=${optimized.elapsedMs.toFixed(2)}`); | ||
| // External reference timings are informational and should not gate Hunk releases. | ||
| console.log(`METRIC competitor_string_width_${name}_ms=${reference.elapsedMs.toFixed(2)}`); | ||
| console.log(`METRIC ${name}_width_measurements=${ITERATIONS * corpus.length}`); | ||
| console.log(`METRIC ${name}_width_checksum=${optimized.checksum}`); | ||
| console.log(`${name} width speedup versus string-width: ${speedup.toFixed(2)}x`); | ||
| } | ||
|
|
||
| measureScenario("cjk_scalar", CJK_SCALAR_LINES); | ||
| measureScenario("emoji_scalar", EMOJI_SCALAR_LINES); | ||
| measureScenario("complex_cluster", COMPLEX_CLUSTER_LINES); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relying on implicit transitive dependencies can cause confusion, so I added get-east-asian-width as an explicit dependency. It was already included transitively via string-width.