Skip to content

Refactor HashDigest APIs - #21139

Merged
woodruffw merged 15 commits into
mainfrom
ww/hash
Sep 16, 2026
Merged

woodruffw merged 15 commits into
mainfrom
ww/hash

Conversation

@woodruffw

@woodruffw woodruffw commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

Summary

WIP, not ready for review.

This came from #21131 and me thinking about ways to make more invalid hash states unrepresentable.

TL;DR:

  • HashDigest is now an enum with size-controlled variants;
  • HashDigest and HashAlgorithm are now both explicit about Blake2b being Blake2b256
  • Hash digests themselves now flow into a Digest wrapper that preserves alphabet/length invariants

Test Plan

Added tests.

The original approach here bumped several cache buckets, but I'm now preserving the original wire layout with HashDigestWire so that we don't churn on an internal change.

This also needs benchmarking. No perf regressions, and microbenchmarks are a lot faster. But hex parsing doesn't really dominate our performance anyways.

@woodruffw woodruffw self-assigned this Aug 14, 2026
@woodruffw woodruffw added the internal A refactor or improvement that is not user-facing label Aug 14, 2026
Comment thread crates/uv-client/src/html.rs Resolved
Comment thread crates/uv-client/src/registry_client.rs Resolved
Comment thread crates/uv-pypi-types/src/simple_json.rs Resolved
Comment thread crates/uv-resolver/src/lock/mod.rs Resolved
Comment thread crates/uv-resolver/src/lock/mod.rs Resolved
Comment thread crates/uv/tests/pip/pip_sync.rs Resolved
Comment thread crates/uv/tests/pip/pip_sync.rs Resolved
Comment thread crates/uv/tests/pip/pip_sync.rs Resolved
@astral-sh-bot

astral-sh-bot Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

uv test inventory changes

This PR changes the tests when compared with the main base revision.

  • Added tests: 6
  • Removed tests: 0
  • Changed suites: 4
uv-client: +1 / -0

Added:

  • uv-client::html::tests::reject_invalid_hashes

Removed: none

uv-pypi-types: +3 / -0

Added:

  • uv-pypi-types::simple_json::tests::hash_digest_rejects_invalid_digests
  • uv-pypi-types::simple_json::tests::hash_digest_variants
  • uv-pypi-types::simple_json::tests::hash_digests_round_trip_hashes

Removed: none

uv::pip: +1 / -0

Added:

  • uv::pip::pip_sync::require_hashes_url_malformed

Removed: none

uv::pip_install: +1 / -0

Added:

  • uv::pip_install::pip_install::pep_751_install_invalid_hashes

Removed: none


/// Calculate the requested hashes of a file.
async fn hash_file(
async fn hash_file<const COUNT: usize>(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I love const generics.

)]
#[serde(transparent)]
#[rkyv(derive(Debug))]
pub struct Digest<const BYTES: usize>(SmallString);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note that despite the const generic here, we're still using a SmallString rather than forcing every Digest into a static array. I think this is preferable in terms of stack object size, especially enums.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can check the size change with mem::size_of. I think we want a Box<T> here, if the hashes are longer than 8 bytes we're not in the small variant anyway and Option<Box<T>> is 8 byte. This is a code style question though, I don't think it changes perf.

)]
#[serde(transparent)]
#[rkyv(derive(Debug))]
pub struct Digest<const BYTES: usize>(SmallString);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can check the size change with mem::size_of. I think we want a Box<T> here, if the hashes are longer than 8 bytes we're not in the small variant anyway and Option<Box<T>> is 8 byte. This is a code style question though, I don't think it changes perf.

Comment thread crates/uv-pypi-types/src/simple_json.rs Resolved
}

/// Encode the exact number of digest bytes as lowercase hexadecimal.
pub fn from_bytes(bytes: [u8; BYTES]) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't std (with {:x} iirc) do that for us?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think so, but we'd either need to switch to a growing String here or pre-allocate and use a fallible write! (not actually fallible, but Never isn't stable yet). I'll look at it though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I benched this locally and it's almost 10x slower per-byte, which is surprising to me. I suspect it's because Rust's generic formatter needs to do more booking here.

Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: William Woodruff <[email protected]>

Fix TODO

Signed-off-by: William Woodruff <[email protected]>

Remove chum tests

Signed-off-by: William Woodruff <[email protected]>
Signed-off-by: William Woodruff <[email protected]>
@woodruffw
woodruffw enabled auto-merge (squash) September 16, 2026 16:28
@woodruffw
woodruffw merged commit 108a358 into main Sep 16, 2026
83 checks passed
@woodruffw
woodruffw deleted the ww/hash branch September 16, 2026 16:31
@woodruffw woodruffw mentioned this pull request Sep 17, 2026

This branch was previously deployed

1 inactive deployment
automations — ee87c2c4 Deployed Sep 16, 2026 by woodruffw via review / security review #46919
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal A refactor or improvement that is not user-facing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants