Remove `serde::Serialize` implementations for rkyv-able structs (#7663)

## Summary

Random, but I noticed that we can remove a ton of serialize and
deserialize derives by using `rkyv` for the flat-index caches. (We
already use `rkyv` for these same structs in the registry cache.)
This commit is contained in:
Charlie Marsh 2024-09-24 13:23:47 -04:00 committed by GitHub
parent c4c5378c0b
commit 538b0f1099
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 31 additions and 32 deletions

View File

@ -21,9 +21,7 @@ pub enum FileConversionError {
} }
/// Internal analog to [`pypi_types::File`]. /// Internal analog to [`pypi_types::File`].
#[derive( #[derive(Debug, Clone, Hash, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
Debug, Clone, Hash, Serialize, Deserialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize,
)]
#[rkyv(derive(Debug))] #[rkyv(derive(Debug))]
pub struct File { pub struct File {
pub dist_info_metadata: bool, pub dist_info_metadata: bool,
@ -68,9 +66,7 @@ impl File {
} }
/// While a registry file is generally a remote URL, it can also be a file if it comes from a directory flat indexes. /// While a registry file is generally a remote URL, it can also be a file if it comes from a directory flat indexes.
#[derive( #[derive(Debug, Clone, Hash, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
Debug, Clone, Hash, Serialize, Deserialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize,
)]
#[rkyv(derive(Debug))] #[rkyv(derive(Debug))]
pub enum FileLocation { pub enum FileLocation {
/// URL relative to the base URL. /// URL relative to the base URL.

View File

@ -93,7 +93,6 @@ impl CoreMetadata {
PartialEq, PartialEq,
Eq, Eq,
Hash, Hash,
Serialize,
Deserialize, Deserialize,
rkyv::Archive, rkyv::Archive,
rkyv::Deserialize, rkyv::Deserialize,

View File

@ -765,7 +765,7 @@ impl CacheBucket {
fn to_str(self) -> &'static str { fn to_str(self) -> &'static str {
match self { match self {
Self::SourceDistributions => "sdists-v4", Self::SourceDistributions => "sdists-v4",
Self::FlatIndex => "flat-index-v0", Self::FlatIndex => "flat-index-v1",
Self::Git => "git-v0", Self::Git => "git-v0",
Self::Interpreter => "interpreter-v2", Self::Interpreter => "interpreter-v2",
// Note that when bumping this, you'll also need to bump it // Note that when bumping this, you'll also need to bump it

View File

@ -11,7 +11,7 @@ use uv_cache::{Cache, CacheBucket};
use crate::cached_client::{CacheControl, CachedClientError}; use crate::cached_client::{CacheControl, CachedClientError};
use crate::html::SimpleHtml; use crate::html::SimpleHtml;
use crate::{Connectivity, Error, ErrorKind, RegistryClient}; use crate::{Connectivity, Error, ErrorKind, OwnedArchive, RegistryClient};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum FlatIndexError { pub enum FlatIndexError {
@ -170,7 +170,7 @@ impl<'a> FlatIndexClient<'a> {
let SimpleHtml { base, files } = SimpleHtml::parse(&text, &url) let SimpleHtml { base, files } = SimpleHtml::parse(&text, &url)
.map_err(|err| Error::from_html_err(err, url.clone()))?; .map_err(|err| Error::from_html_err(err, url.clone()))?;
let files: Vec<File> = files let unarchived: Vec<File> = files
.into_iter() .into_iter()
.filter_map(|file| { .filter_map(|file| {
match File::try_from(file, base.as_url()) { match File::try_from(file, base.as_url()) {
@ -183,7 +183,7 @@ impl<'a> FlatIndexClient<'a> {
} }
}) })
.collect(); .collect();
Ok::<Vec<File>, CachedClientError<Error>>(files) OwnedArchive::from_unarchived(&unarchived)
} }
.boxed_local() .boxed_local()
.instrument(info_span!("parse_flat_index_html", url = % url)) .instrument(info_span!("parse_flat_index_html", url = % url))
@ -191,7 +191,7 @@ impl<'a> FlatIndexClient<'a> {
let response = self let response = self
.client .client
.cached_client() .cached_client()
.get_serde( .get_cacheable(
flat_index_request, flat_index_request,
&cache_entry, &cache_entry,
cache_control, cache_control,
@ -201,7 +201,11 @@ impl<'a> FlatIndexClient<'a> {
match response { match response {
Ok(files) => { Ok(files) => {
let files = files let files = files
.into_iter() .iter()
.map(|file| {
rkyv::deserialize::<File, rkyv::rancor::Error>(file)
.expect("archived version always deserializes")
})
.filter_map(|file| { .filter_map(|file| {
Some(( Some((
DistFilename::try_from_normalized_filename(&file.filename)?, DistFilename::try_from_normalized_filename(&file.filename)?,

View File

@ -8,7 +8,6 @@ use futures::{FutureExt, TryStreamExt};
use http::HeaderMap; use http::HeaderMap;
use reqwest::{Client, Response, StatusCode}; use reqwest::{Client, Response, StatusCode};
use reqwest_middleware::ClientWithMiddleware; use reqwest_middleware::ClientWithMiddleware;
use serde::{Deserialize, Serialize};
use tracing::{info_span, instrument, trace, warn, Instrument}; use tracing::{info_span, instrument, trace, warn, Instrument};
use url::Url; use url::Url;
@ -723,9 +722,7 @@ impl RegistryClient {
} }
} }
#[derive( #[derive(Default, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
Default, Debug, Serialize, Deserialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize,
)]
#[rkyv(derive(Debug))] #[rkyv(derive(Debug))]
pub struct VersionFiles { pub struct VersionFiles {
pub wheels: Vec<VersionWheel>, pub wheels: Vec<VersionWheel>,
@ -756,27 +753,25 @@ impl VersionFiles {
} }
} }
#[derive(Debug, Serialize, Deserialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)] #[derive(Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[rkyv(derive(Debug))] #[rkyv(derive(Debug))]
pub struct VersionWheel { pub struct VersionWheel {
pub name: WheelFilename, pub name: WheelFilename,
pub file: File, pub file: File,
} }
#[derive(Debug, Serialize, Deserialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)] #[derive(Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[rkyv(derive(Debug))] #[rkyv(derive(Debug))]
pub struct VersionSourceDist { pub struct VersionSourceDist {
pub name: SourceDistFilename, pub name: SourceDistFilename,
pub file: File, pub file: File,
} }
#[derive( #[derive(Default, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
Default, Debug, Serialize, Deserialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize,
)]
#[rkyv(derive(Debug))] #[rkyv(derive(Debug))]
pub struct SimpleMetadata(Vec<SimpleMetadatum>); pub struct SimpleMetadata(Vec<SimpleMetadatum>);
#[derive(Debug, Serialize, Deserialize, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)] #[derive(Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[rkyv(derive(Debug))] #[rkyv(derive(Debug))]
pub struct SimpleMetadatum { pub struct SimpleMetadatum {
pub version: Version, pub version: Version,

View File

@ -3,7 +3,7 @@ use std::fmt::{Display, Formatter};
use std::str::FromStr; use std::str::FromStr;
use std::sync::LazyLock; use std::sync::LazyLock;
use serde::{Deserialize, Deserializer, Serialize}; use serde::{Deserialize, Deserializer};
use crate::{validate_and_normalize_owned, validate_and_normalize_ref, InvalidNameError}; use crate::{validate_and_normalize_owned, validate_and_normalize_ref, InvalidNameError};
@ -12,7 +12,7 @@ use crate::{validate_and_normalize_owned, validate_and_normalize_ref, InvalidNam
/// See: /// See:
/// - <https://peps.python.org/pep-0735/> /// - <https://peps.python.org/pep-0735/>
/// - <https://packaging.python.org/en/latest/specifications/name-normalization/> /// - <https://packaging.python.org/en/latest/specifications/name-normalization/>
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)] #[derive(Debug, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct GroupName(String); pub struct GroupName(String);

View File

@ -33,7 +33,8 @@ pub enum PyprojectTomlError {
} }
/// A `pyproject.toml` as specified in PEP 517. /// A `pyproject.toml` as specified in PEP 517.
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
#[cfg_attr(test, derive(Serialize))]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct PyProjectToml { pub struct PyProjectToml {
/// PEP 621-compliant project metadata. /// PEP 621-compliant project metadata.
@ -111,7 +112,8 @@ impl AsRef<[u8]> for PyProjectToml {
/// PEP 621 project metadata (`project`). /// PEP 621 project metadata (`project`).
/// ///
/// See <https://packaging.python.org/en/latest/specifications/pyproject-toml>. /// See <https://packaging.python.org/en/latest/specifications/pyproject-toml>.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] #[derive(Deserialize, Debug, Clone, PartialEq)]
#[cfg_attr(test, derive(Serialize))]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct Project { pub struct Project {
/// The name of the project /// The name of the project
@ -133,7 +135,8 @@ pub struct Project {
pub(crate) scripts: Option<serde::de::IgnoredAny>, pub(crate) scripts: Option<serde::de::IgnoredAny>,
} }
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] #[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(Serialize))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Tool { pub struct Tool {
pub uv: Option<ToolUv>, pub uv: Option<ToolUv>,
@ -141,7 +144,8 @@ pub struct Tool {
// NOTE(charlie): When adding fields to this struct, mark them as ignored on `Options` in // NOTE(charlie): When adding fields to this struct, mark them as ignored on `Options` in
// `crates/uv-settings/src/settings.rs`. // `crates/uv-settings/src/settings.rs`.
#[derive(Serialize, Deserialize, OptionsMetadata, Debug, Clone, PartialEq, Eq)] #[derive(Deserialize, OptionsMetadata, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(Serialize))]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct ToolUv { pub struct ToolUv {
@ -287,9 +291,9 @@ pub struct ToolUv {
pub constraint_dependencies: Option<Vec<pep508_rs::Requirement<VerbatimParsedUrl>>>, pub constraint_dependencies: Option<Vec<pep508_rs::Requirement<VerbatimParsedUrl>>>,
} }
#[derive(Serialize, Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(Serialize))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ToolUvSources(BTreeMap<PackageName, Source>); pub struct ToolUvSources(BTreeMap<PackageName, Source>);
impl ToolUvSources { impl ToolUvSources {
@ -346,7 +350,8 @@ impl<'de> serde::de::Deserialize<'de> for ToolUvSources {
} }
} }
#[derive(Serialize, Deserialize, OptionsMetadata, Default, Debug, Clone, PartialEq, Eq)] #[derive(Deserialize, OptionsMetadata, Default, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(test, derive(Serialize))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "kebab-case", deny_unknown_fields)] #[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ToolUvWorkspace { pub struct ToolUvWorkspace {