Skip to content

Conversation

@woodruffw
Copy link
Member

Summary

Was reading this code while looking at adding PEP 792 support and noticed these: by using the borrowing deserializer instead of the owning one, we can probably save a good number of small-ish String allocations in the PypiFile and PyxFile deserializers.

(These deserializers in turn are probably pretty hot, since large installations/resolutions require pulling lots of candidates from indices.)

Test Plan

No functional change expected; I'm not sure what the best way to microbenchmark this is. I'll try and contrive something locally.

@woodruffw woodruffw self-assigned this Dec 22, 2025
@woodruffw woodruffw added the performance Potential performance improvement label Dec 22, 2025
@woodruffw woodruffw temporarily deployed to uv-test-registries December 22, 2025 21:22 — with GitHub Actions Inactive
@woodruffw woodruffw merged commit 7865672 into main Dec 22, 2025
102 checks passed
@woodruffw woodruffw deleted the ww/alloc branch December 22, 2025 21:38
@woodruffw
Copy link
Member Author

woodruffw commented Dec 25, 2025

Note: I realized after-the-fact that this could be subtly incorrect. For example, sometimes serde can't borrow a string because it needs to de-escape it from its JSON or other serialized form.

In practice that's probably fine in this context, since we don't really expect any weird escaping to happen on these key names. But it means that we can't just go around swapping <String> for <&str> with full generality.

Ref: serde-rs/serde#1413

Ref: https://users.rust-lang.org/t/solved-serde-deserialize-str-containig-special-chars/27218

@konstin
Copy link
Member

konstin commented Jan 5, 2026

Can we use Cow<str, _> here, which is both correct and doesn't allocate in the regular case?

konstin pushed a commit that referenced this pull request Jan 5, 2026
## Summary

Minor, noticed this with #17221. CodSpeed has deprecated
`instrumentation` and replaced it with `simulation`, which has the same
meaning:

https://codspeed.io/docs/instruments/cpu/overview#legacy-terminology

## Test Plan

No functional changes.

---------

Signed-off-by: William Woodruff <[email protected]>
zaniebot pushed a commit to zaniebot/uv that referenced this pull request Jan 5, 2026
Replace `String` with `Cow<'_, str>` when deserializing map keys. This
allows serde to borrow strings directly from the input in the common
case (no escaping needed), while still correctly handling cases where
de-escaping is required.

Addresses feedback from astral-sh#17221.
@woodruffw
Copy link
Member Author

woodruffw commented Jan 5, 2026

Can we use Cow<str, _> here, which is both correct and doesn't allocate in the regular case?

Yeah, that would be the most correct/general solution! I think we could probably do the same for every callsite of String::deserialize as well, although some of those may not meaningfully benefit from saving a copy if we need an owned String anyways.

For example, impl<'a> serde::Deserialize<'a> for PythonRequest can probably be cow'd.

zaniebot pushed a commit to zaniebot/uv that referenced this pull request Jan 5, 2026
Replace `String` with `Cow<'_, str>` when deserializing map keys. This
allows serde to borrow strings directly from the input in the common
case (no escaping needed), while still correctly handling cases where
de-escaping is required.

Addresses feedback from astral-sh#17221.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Potential performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants