From 65efee1d76b350473edbf181be5010472d6028b8 Mon Sep 17 00:00:00 2001 From: konsti Date: Fri, 5 Jan 2024 21:14:11 +0100 Subject: [PATCH] Add compare_release fast path (#799) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Looking at the profile for tf-models-nightly after #789, `compare_release` is the single biggest item. Adding a fast path, we avoid paying the cost for padding releases with 0s when they are the same length, resulting in a 16% for this pathological case. Note that this mainly happens because tf-models-nightly is almost all large dev releases that hit the slow path. **Before** ![image](https://github.com/astral-sh/puffin/assets/6826232/0d2b4553-da69-4cdb-966b-0894a6dd5d94) **After** ![image](https://github.com/astral-sh/puffin/assets/6826232/6d484808-9d16-408d-823e-a12d321802a5) ``` $ hyperfine --warmup 1 --runs 3 "target/profiling/main pip-compile -q scripts/requirements/tf-models-nightly.txt" "target/profiling/puffin pip-compile -q scripts/requirements/tf-models-nightly.txt" Benchmark 1: target/profiling/main pip-compile -q scripts/requirements/tf-models-nightly.txt Time (mean ± σ): 11.963 s ± 0.225 s [User: 11.478 s, System: 0.451 s] Range (min … max): 11.747 s … 12.196 s 3 runs Benchmark 2: target/profiling/puffin pip-compile -q scripts/requirements/tf-models-nightly.txt Time (mean ± σ): 10.317 s ± 0.720 s [User: 9.885 s, System: 0.404 s] Range (min … max): 9.501 s … 10.860 s 3 runs Summary target/profiling/puffin pip-compile -q scripts/requirements/tf-models-nightly.txt ran 1.16 ± 0.08 times faster than target/profiling/main pip-compile -q scripts/requirements/tf-models-nightly.txt ``` --- crates/pep440-rs/src/version.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/pep440-rs/src/version.rs b/crates/pep440-rs/src/version.rs index a90bce803..ce26dce01 100644 --- a/crates/pep440-rs/src/version.rs +++ b/crates/pep440-rs/src/version.rs @@ -2092,6 +2092,9 @@ impl<'source> FromPyObject<'source> for Version { /// Compare the release parts of two versions, e.g. `4.3.1` > `4.2`, `1.1.0` == /// `1.1` and `1.16` < `1.19` pub(crate) fn compare_release(this: &[u64], other: &[u64]) -> Ordering { + if this.len() == other.len() { + return this.cmp(other); + } // "When comparing release segments with different numbers of components, the shorter segment // is padded out with additional zeros as necessary" for (this, other) in this.iter().chain(std::iter::repeat(&0)).zip(