Skip to content

Commit 279528f

Browse files
committed
uniq: cache locale check instead of querying env vars per line
is_c_locale() was called on every line inside key_end_index() when -w/--check-chars is set, doing up to 3 std::env::var_os() lookups each time. Locale env vars can't change mid-process, so this was pure per-line overhead, causing uniq -w to be ~5x slower than GNU uniq even for small -w values. Compute is_c_locale() once at startup and cache it on the Uniq struct instead. Fixes #13199
1 parent 06f5f2b commit 279528f

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

‎src/uu/uniq/src/uniq.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct Uniq {
5555
slice_stop: Option<usize>,
5656
ignore_case: bool,
5757
zero_terminated: bool,
58+
is_c_locale: bool,
5859
}
5960

6061
#[derive(Default)]
@@ -202,7 +203,7 @@ impl Uniq {
202203
if remainder.is_empty() {
203204
return key_start;
204205
}
205-
if Self::is_c_locale() {
206+
if self.is_c_locale {
206207
// for C or POSIX we count bytes
207208
key_start + remainder.len().min(limit)
208209
} else if let Ok(valid) = std::str::from_utf8(remainder) {
@@ -680,6 +681,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
680681
slice_stop: opt_parsed(options::CHECK_CHARS, &matches)?,
681682
ignore_case: matches.get_flag(options::IGNORE_CASE),
682683
zero_terminated: matches.get_flag(options::ZERO_TERMINATED),
684+
is_c_locale: Uniq::is_c_locale(),
683685
};
684686

685687
if uniq.show_counts && uniq.all_repeated {

0 commit comments

Comments
 (0)