Skip to content

Commit

Permalink
Merge pull request #328 from Eh2406/master
Browse files Browse the repository at this point in the history
Faster Ord when Eq
  • Loading branch information
dtolnay authored Dec 12, 2024
2 parents 89504eb + 75856ef commit 238757d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,18 @@ impl Drop for Identifier {
}
}

impl Identifier {
pub(crate) fn ptr_eq(&self, rhs: &Self) -> bool {
self.head == rhs.head && self.tail == rhs.tail
}
}

impl PartialEq for Identifier {
fn eq(&self, rhs: &Self) -> bool {
if self.is_empty_or_inline() {
if self.ptr_eq(rhs) {
// Fast path (most common)
self.head == rhs.head && self.tail == rhs.tail
} else if rhs.is_empty_or_inline() {
true
} else if self.is_empty_or_inline() || rhs.is_empty_or_inline() {
false
} else {
// SAFETY: both reprs are in the heap allocated representation.
Expand Down
7 changes: 6 additions & 1 deletion src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ impl PartialOrd for BuildMetadata {

impl Ord for Prerelease {
fn cmp(&self, rhs: &Self) -> Ordering {
if self.identifier.ptr_eq(&rhs.identifier) {
return Ordering::Equal;
}
match self.is_empty() {
true if rhs.is_empty() => return Ordering::Equal,
// A real release compares greater than prerelease.
true => return Ordering::Greater,
// Prerelease compares less than the real release.
Expand Down Expand Up @@ -105,6 +107,9 @@ impl Ord for Prerelease {

impl Ord for BuildMetadata {
fn cmp(&self, rhs: &Self) -> Ordering {
if self.identifier.ptr_eq(&rhs.identifier) {
return Ordering::Equal;
}
let lhs = self.as_str().split('.');
let mut rhs = rhs.as_str().split('.');

Expand Down

0 comments on commit 238757d

Please sign in to comment.