Skip to content

Use memchr for wheel parsing #10620

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

Merged
merged 4 commits into from
Jan 15, 2025
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/uv-distribution-filename/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ uv-pep440 = { workspace = true }
uv-platform-tags = { workspace = true }
uv-small-str = { workspace = true }

memchr = { workspace = true }
rkyv = { workspace = true, features = ["smallvec-1"] }
serde = { workspace = true }
smallvec = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/uv-distribution-filename/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod build_tag;
mod egg;
mod extension;
mod source_dist;
mod splitter;
mod wheel;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand Down
50 changes: 50 additions & 0 deletions crates/uv-distribution-filename/src/splitter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// A simple splitter that uses `memchr` to find the next delimiter.
pub(crate) struct MemchrSplitter<'a> {
memchr: memchr::Memchr<'a>,
haystack: &'a str,
offset: usize,
}

impl<'a> MemchrSplitter<'a> {
#[inline]
pub(crate) fn split(haystack: &'a str, delimiter: u8) -> Self {
Self {
memchr: memchr::Memchr::new(delimiter, haystack.as_bytes()),
haystack,
offset: 0,
}
}
}

impl<'a> Iterator for MemchrSplitter<'a> {
type Item = &'a str;

#[inline(always)]
#[allow(clippy::inline_always)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha. Oh golly Clippy would hate me...

fn next(&mut self) -> Option<Self::Item> {
match self.memchr.next() {
Some(index) => {
let start = self.offset;
self.offset = index + 1;
Some(&self.haystack[start..index])
}
None if self.offset < self.haystack.len() => {
let start = self.offset;
self.offset = self.haystack.len();
Some(&self.haystack[start..])
}
None => None,
}
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
// We know we'll return at least one item if there's remaining text.
let min = usize::from(self.offset < self.haystack.len());

// Maximum possible splits is remaining length divided by 2 (minimum one char between delimiters).
let max = (self.haystack.len() - self.offset + 1) / 2 + min;

(min, Some(max))
}
}
65 changes: 31 additions & 34 deletions crates/uv-distribution-filename/src/wheel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::{Display, Formatter};

use memchr::memchr;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::str::FromStr;
use thiserror::Error;
Expand All @@ -12,6 +13,7 @@ use uv_platform_tags::{
PlatformTag, TagCompatibility, Tags,
};

use crate::splitter::MemchrSplitter;
use crate::{BuildTag, BuildTagError};

#[derive(
Expand Down Expand Up @@ -145,64 +147,66 @@ impl WheelFilename {
// The wheel filename should contain either five or six entries. If six, then the third
// entry is the build tag. If five, then the third entry is the Python tag.
// https://www.python.org/dev/peps/pep-0427/#file-name-convention
let mut parts = stem.split('-');
let mut splitter = memchr::Memchr::new(b'-', stem.as_bytes());

let name = parts
.next()
.expect("split always yields 1 or more elements");

let Some(version) = parts.next() else {
let Some(version) = splitter.next() else {
return Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Must have a version".to_string(),
));
};

let Some(build_tag_or_python_tag) = parts.next() else {
let Some(build_tag_or_python_tag) = splitter.next() else {
return Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Must have a Python tag".to_string(),
));
};

let Some(python_tag_or_abi_tag) = parts.next() else {
let Some(python_tag_or_abi_tag) = splitter.next() else {
return Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Must have an ABI tag".to_string(),
));
};

let Some(abi_tag_or_platform_tag) = parts.next() else {
let Some(abi_tag_or_platform_tag) = splitter.next() else {
return Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Must have a platform tag".to_string(),
));
};

let (name, version, build_tag, python_tag, abi_tag, platform_tag) =
if let Some(platform_tag) = parts.next() {
if parts.next().is_some() {
let (name, version, build_tag, python_tag, abi_tag, platform_tag, is_large) =
if let Some(platform_tag) = splitter.next() {
if splitter.next().is_some() {
return Err(WheelFilenameError::InvalidWheelFileName(
filename.to_string(),
"Must have 5 or 6 components, but has more".to_string(),
));
}
(
name,
version,
Some(build_tag_or_python_tag),
python_tag_or_abi_tag,
abi_tag_or_platform_tag,
platform_tag,
&stem[..version],
&stem[version + 1..build_tag_or_python_tag],
Some(&stem[build_tag_or_python_tag + 1..python_tag_or_abi_tag]),
&stem[python_tag_or_abi_tag + 1..abi_tag_or_platform_tag],
&stem[abi_tag_or_platform_tag + 1..platform_tag],
&stem[platform_tag + 1..],
// Always take the slow path if a build tag is present.
true,
)
} else {
(
name,
version,
&stem[..version],
&stem[version + 1..build_tag_or_python_tag],
None,
build_tag_or_python_tag,
python_tag_or_abi_tag,
abi_tag_or_platform_tag,
&stem[build_tag_or_python_tag + 1..python_tag_or_abi_tag],
&stem[python_tag_or_abi_tag + 1..abi_tag_or_platform_tag],
&stem[abi_tag_or_platform_tag + 1..],
// Determine whether any of the tag types contain a period, which would indicate
// that at least one of the tag types includes multiple tags (which in turn
// necessitates taking the slow path).
memchr(b'.', stem[build_tag_or_python_tag..].as_bytes()).is_some(),
)
};

Expand All @@ -217,30 +221,23 @@ impl WheelFilename {
})
.transpose()?;

let tags = if build_tag.is_some()
|| python_tag.contains('.')
|| abi_tag.contains('.')
|| platform_tag.contains('.')
{
let tags = if is_large {
WheelTag::Large {
large: Box::new(WheelTagLarge {
build_tag,
python_tag: python_tag
.split('.')
python_tag: MemchrSplitter::split(python_tag, b'.')
.map(LanguageTag::from_str)
.collect::<Result<_, _>>()
.map_err(|err| {
WheelFilenameError::InvalidLanguageTag(filename.to_string(), err)
})?,
abi_tag: abi_tag
.split('.')
abi_tag: MemchrSplitter::split(abi_tag, b'.')
.map(AbiTag::from_str)
.collect::<Result<_, _>>()
.map_err(|err| {
WheelFilenameError::InvalidAbiTag(filename.to_string(), err)
})?,
platform_tag: platform_tag
.split('.')
platform_tag: MemchrSplitter::split(platform_tag, b'.')
.map(PlatformTag::from_str)
.collect::<Result<_, _>>()
.map_err(|err| {
Expand Down
Loading