Skip to content

cut: fix more compat issues - #13194

Draft
sylvestre wants to merge 3 commits into
uutils:mainfrom
sylvestre:mb-non-utf8
Draft

cut: fix more compat issues#13194
sylvestre wants to merge 3 commits into
uutils:mainfrom
sylvestre:mb-non-utf8

Conversation

@sylvestre

Copy link
Copy Markdown
Contributor

Add locale-aware character handling so -c counts characters, -b -n keeps multibyte characters whole, and -d accepts a single multibyte delimiter.

Should make test tests/cut/mb-non-utf8.sh pass

@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/date/resolution (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/tail/symlink (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/tail-n0f (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/cut/mb-non-utf8 is no longer failing!
Congrats! The gnu test tests/cut/bounded-memory is now passing!

Comment thread tests/by-util/test_cut.rs Outdated
Comment thread tests/by-util/test_cut.rs Outdated
Comment thread tests/by-util/test_cut.rs Outdated
Comment thread tests/by-util/test_cut.rs Outdated
Comment thread tests/by-util/test_cut.rs Outdated
@codspeed-hq

codspeed-hq Bot commented Jun 29, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 26.55%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 344 untouched benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation cut_characters 17.1 ms 23.3 ms -26.55%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing sylvestre:mb-non-utf8 (e34f4db) with main (4e95dfe)

Open in CodSpeed

Footnotes

  1. 46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@sylvestre
sylvestre force-pushed the mb-non-utf8 branch 2 times, most recently from 523d612 to 5919e1d Compare July 4, 2026 16:36
@sylvestre
sylvestre force-pushed the mb-non-utf8 branch 3 times, most recently from 79b2f7b to e2f69cc Compare August 2, 2026 08:23
@sylvestre sylvestre changed the title cut: support multibyte characters in non-UTF-8 locales cut: fix more compat issues Aug 2, 2026
@sylvestre
sylvestre marked this pull request as draft August 2, 2026 08:25
wasmtime's argument marshaling requires valid UTF-8, so a raw non-UTF-8
byte delimiter can't be passed through to the WASI binary the way it
can natively on Linux.
The character path walked the line one character at a time through the
encoding decoder and looked the locale encoding up per character, which
dominated the per-line cost on mixed ASCII and multi-byte text.

Take runs of ASCII bytes a machine word at a time. They are single-byte
characters in every encoding handled here, so byte offset and character
position move together across a run, and only the bytes above 0x7F need
the decoder. Bundle the parts that are fixed for the whole run -- the
ranges, the output delimiter and whether it was given, the position mode
and the encoding -- into a CharCut built once by cut_chars, and make the
line body a method on it, so the per-line call passes one pointer rather
than seven arguments.

In uucore, MbEncoding becomes the public Encoding and locale_encoding()
returns it by value, so a caller decoding many characters resolves the
locale once and keeps it in a register instead of reaching through a
OnceLock per character. is_multibyte_locale() had no callers left.

Instruction counts against the parent commit (cachegrind, LC_ALL=C.UTF-8):

  -c 5-30, 100k mixed short lines       30.44M -> 28.95M
  -c 20-70, 20k long multibyte lines    44.38M -> 41.02M

That is roughly 1.1x in wall clock on both shapes; the machine was too
loaded to quote a tighter figure. The single-byte path (LC_ALL=C) and
field mode are unchanged.

Selecting a range of characters still costs more than the same range of
bytes, and always will: -c used to be an alias for -b, and characters
have to be decoded to be counted.

Tests cover advance directly -- character counting for -c, byte counting
for -b -n, and the word boundary crossings -- plus a cut -c case over
mixed ASCII and multi-byte lines.
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