Skip to content

Commit 238757d

Browse files
authored
Merge pull request #328 from Eh2406/master
Faster Ord when Eq
2 parents 89504eb + 75856ef commit 238757d

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/identifier.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,18 @@ impl Drop for Identifier {
257257
}
258258
}
259259

260+
impl Identifier {
261+
pub(crate) fn ptr_eq(&self, rhs: &Self) -> bool {
262+
self.head == rhs.head && self.tail == rhs.tail
263+
}
264+
}
265+
260266
impl PartialEq for Identifier {
261267
fn eq(&self, rhs: &Self) -> bool {
262-
if self.is_empty_or_inline() {
268+
if self.ptr_eq(rhs) {
263269
// Fast path (most common)
264-
self.head == rhs.head && self.tail == rhs.tail
265-
} else if rhs.is_empty_or_inline() {
270+
true
271+
} else if self.is_empty_or_inline() || rhs.is_empty_or_inline() {
266272
false
267273
} else {
268274
// SAFETY: both reprs are in the heap allocated representation.

src/impls.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ impl PartialOrd for BuildMetadata {
5050

5151
impl Ord for Prerelease {
5252
fn cmp(&self, rhs: &Self) -> Ordering {
53+
if self.identifier.ptr_eq(&rhs.identifier) {
54+
return Ordering::Equal;
55+
}
5356
match self.is_empty() {
54-
true if rhs.is_empty() => return Ordering::Equal,
5557
// A real release compares greater than prerelease.
5658
true => return Ordering::Greater,
5759
// Prerelease compares less than the real release.
@@ -105,6 +107,9 @@ impl Ord for Prerelease {
105107

106108
impl Ord for BuildMetadata {
107109
fn cmp(&self, rhs: &Self) -> Ordering {
110+
if self.identifier.ptr_eq(&rhs.identifier) {
111+
return Ordering::Equal;
112+
}
108113
let lhs = self.as_str().split('.');
109114
let mut rhs = rhs.as_str().split('.');
110115

0 commit comments

Comments
 (0)