Skip to content
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

join: avoid extra allocations when using -i #6774

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/uu/join/BENCHMARKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The following options can have a non-trivial impact on performance:

- `-a`/`-v` if one of the two files has significantly more lines than the other
- `-j`/`-1`/`-2` cause work to be done to grab the appropriate field
- `-i` adds a call to `to_ascii_lowercase()` that adds some time for allocating and dropping memory for the lowercase key
- `-i` uses our custom code for case-insensitive text comparisons
- `--nocheck-order` causes some calls of `Input::compare` to be skipped

The content of the files being joined has a very significant impact on the performance.
Expand Down
40 changes: 37 additions & 3 deletions src/uu/join/src/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,40 @@
}
}

/// Byte slice wrapper whose Ord implementation is case-insensitive on ASCII.
#[derive(Eq)]

Check warning on line 292 in src/uu/join/src/join.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/join/src/join.rs#L292

Added line #L292 was not covered by tests
struct CaseInsensitiveSlice<'a> {
v: &'a [u8],
}

impl Ord for CaseInsensitiveSlice<'_> {
fn cmp(&self, other: &Self) -> Ordering {
if let Some((s, o)) =
std::iter::zip(self.v.iter(), other.v.iter()).find(|(s, o)| !s.eq_ignore_ascii_case(o))
{
// first characters that differ, return the case-insensitive comparison
let s = s.to_ascii_lowercase();
let o = o.to_ascii_lowercase();
s.cmp(&o)
} else {
// one of the strings is a substring or equal of the other
self.v.len().cmp(&other.v.len())
}
}
}

impl PartialOrd for CaseInsensitiveSlice<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}

Check warning on line 316 in src/uu/join/src/join.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/join/src/join.rs#L314-L316

Added lines #L314 - L316 were not covered by tests
}

impl PartialEq for CaseInsensitiveSlice<'_> {
fn eq(&self, other: &Self) -> bool {
self.v.eq_ignore_ascii_case(other.v)
}

Check warning on line 322 in src/uu/join/src/join.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/join/src/join.rs#L320-L322

Added lines #L320 - L322 were not covered by tests
}

/// Input processing parameters.
struct Input<Sep: Separator> {
separator: Sep,
Expand All @@ -307,9 +341,9 @@
fn compare(&self, field1: Option<&[u8]>, field2: Option<&[u8]>) -> Ordering {
if let (Some(field1), Some(field2)) = (field1, field2) {
if self.ignore_case {
field1
.to_ascii_lowercase()
.cmp(&field2.to_ascii_lowercase())
let field1 = CaseInsensitiveSlice { v: field1 };
let field2 = CaseInsensitiveSlice { v: field2 };
field1.cmp(&field2)
} else {
field1.cmp(field2)
}
Expand Down
Loading