Skip to content

perf: accelerate printable ASCII text width - #297

Merged
djc merged 2 commits into
console-rs:mainfrom
dexhunter:perf/accelerate-printable-text-width
Sep 5, 2026
Merged

djc merged 2 commits into
console-rs:mainfrom
dexhunter:perf/accelerate-printable-text-width

Conversation

@dexhunter

@dexhunter dexhunter commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

measure_text_width currently runs printable ASCII through ANSI parsing and width calculation. This adds a boolean byte reduction that returns the byte length for printable ASCII when ANSI parsing is enabled. Other inputs retain the existing parser and width behavior.

On Rust 1.97.1 / Linux x86_64, a fixed five-corpus public-API microbenchmark improved from 320.91 ns to 90.79 ns (71.7% lower); a separate winner replay measured 90.58 ns. The mix includes three ASCII corpora, Unicode status text and colored status text. The latter two cases were about 7.0% and 2.8% slower, respectively; all eight fallback/tiny regression guards passed. These are function-latency measurements.

Validation passed all seven native feature configurations, formatting, clippy, release build and docs. Four added regression tests passed on both pristine and candidate code, and the evaluator checked 3,434,040 semantic cases in each of four ANSI/Unicode feature combinations.

The external autoresearch trajectory and exact source/metric bindings are available in Weco Observe.

The measurements and Observe source snapshots correspond to research commit 13501c50490950db912aa6cdba672825939708e1. Follow-up commit b63f12304da138f3ba40c9a56de858a2a057c485 only moves the same four regression tests into src/utils.rs; the measured implementation is unchanged. The seven feature configurations, formatting, clippy, release build and docs were validated again for this revision. The benchmark was not motivated by an application profile.

Use a boolean byte reduction before the existing ANSI parser and width
composition. Preserve control and Unicode fallback behavior.

Co-Authored-By: Aiden <aiden@weco.ai>

@djc djc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this actually show up as some kind of bottleneck in your application?

Comment thread tests/measure_text_width.rs Outdated
Comment thread src/utils.rs
Move the four text width regression fixtures beside the existing unit
tests without changing the measured implementation or assertions.

Co-Authored-By: Aiden <aiden@weco.ai>
@dexhunter

Copy link
Copy Markdown
Contributor Author

This came from benchmarking the public measure_text_width API; I don't have an application profile showing it as a bottleneck. The reported improvement is limited to that microbenchmark.

I tried all() first. Against the original implementation, inputs with a long ASCII prefix followed by Unicode or ANSI were about 22.1% and 20.1% slower, which failed the regression limits. The fold() version passed those same checks. The range includes printable ASCII and space; is_ascii_graphic() excludes space, while is_ascii() includes control bytes.

I've moved the four regression tests into src/utils.rs in b63f123. Their assertions and the implementation are unchanged. All seven feature configurations, formatting, clippy, release build and docs pass.

@djc
djc merged commit ed342d0 into console-rs:main Sep 5, 2026
20 checks passed
kodiakhq Bot pushed a commit to pdylanross/fatigue that referenced this pull request Sep 14, 2026
Bumps console from 0.16.4 to 0.16.6.

Release notes
Sourced from console's releases.

0.16.6
What's Changed

Fix truncate_str panicking mid-character without ansi-parsing by @​lenamonj in console-rs/console#296
perf: accelerate printable ASCII text width by @​dexhunter in console-rs/console#297
fix: measure the truncation tail in visible columns, not raw width by @​youdie006 in console-rs/console#298
Prepare 0.16.6 by @​djc in console-rs/console#299

0.16.5
What's Changed

Strip OSC and DCS sequences to support e.g. OSC 8 hyperlinks over tmux. by @​khoek in console-rs/console#280




Commits

4329b77 Bump version to 0.16.6
bdf46b0 utils: wrap tests in module
4f54213 fix: measure the truncation tail in visible columns
ed342d0 test: consolidate text width regression coverage
48b99e9 perf: accelerate printable ASCII text width
abf0358 Fix truncate_str panicking mid-character without ansi-parsing
ac3cb73 Bump version to 0.16.5
97a91ae ansi: strip OSC and DCS sequences
See full diff in compare view




Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options

You can trigger Dependabot actions by commenting on this PR:

@dependabot rebase will rebase this PR
@dependabot recreate will recreate this PR, overwriting any edits that have been made to it
@dependabot show <dependency name> ignore conditions will show all of the ignore conditions of the specified dependency
@dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
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