Skip to content

rev: reverse by character in UTF-8 locales - #657

Open
SichenLiang wants to merge 1 commit into
uutils:mainfrom
SichenLiang:rev-utf8-characters
Open

SichenLiang wants to merge 1 commit into
uutils:mainfrom
SichenLiang:rev-utf8-characters

Conversation

@SichenLiang

Copy link
Copy Markdown
Contributor

In a UTF-8 locale, reverse valid UTF-8 records by code point instead of by byte. Other locales keep the existing byte-reversal behavior.

The locale is taken from the first nonempty value of LC_ALL, LC_CTYPE, and LANG. The manual now documents that rule.

Records that are not valid UTF-8 are still reversed byte by byte and exit 0; util-linux reports an fgetwc() error instead.

On an x86_64 Linux machine, 64 MiB of records made entirely of multibyte characters took 5.9 to 7.2 times as much CPU as main, depending on code alignment, because they are now validated and reversed on character boundaries. Long ASCII records in a UTF-8 locale were 13% to 22% faster. In the C locale the code path is unchanged; a 7% difference there went away when both binaries were rebuilt with branch alignment. Short ASCII lines were about 3% slower.

Tests cover locale precedence, valid and invalid records, files, NUL-delimited input, and long records.

Fixes #656

Comment thread src/uu/rev/src/rev.rs
.then_some(ascii_prefix + remaining_prefix)
}

fn reverse_record(buf: &mut [u8], utf8: bool) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is a lot of complexity just to reverse a line.
could you please start with the simple version: from_utf8, reverse the bytes, then fix the multibyte sequences?
we can add fast paths later in a separate PR, with benchmarks

Comment thread src/uu/rev/src/rev.rs
pub const ZERO: &str = "zero";
}

// uucore's get_locale_encoding uses LC_COLLATE and accepts empty values.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if get_locale_encoding is wrong, could you please fix it in uucore instead of adding our own parser here?

Comment thread src/uu/rev/src/rev.rs

// Checking while swapping avoids a separate scan in ASCII-only mode.
// The caller handles the middle so the loop can use complete words.
#[clippy::msrv = "1.70.0"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why is this needed? rust-version is already 1.70 in the workspace

Comment thread src/uu/rev/src/rev.rs
let (front, back) = buf.split_at_mut(len / 2);
let mut done = 0;
for (a, b) in front.chunks_exact_mut(8).zip(back.rchunks_exact_mut(8)) {
let x = u64::from_ne_bytes(a.try_into().unwrap());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please avoid unwrap

Comment thread src/uu/rev/src/rev.rs
.is_some_and(|name| {
let codeset = match name.split_once('.') {
Some((_, codeset)) => codeset,
None if cfg!(target_vendor = "apple") => name,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why only apple?

When the first nonempty value of LC_ALL, LC_CTYPE and LANG names a
UTF-8 codeset, reverse valid UTF-8 records by code point. Keep byte
reversal in other locales and for records that are not valid UTF-8.
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.

rev: multibyte characters are reversed byte by byte in UTF-8 locales

2 participants