Skip to content

Commit

Permalink
Shrink Dist from 496 bytes to 240 by boxing File
Browse files Browse the repository at this point in the history
`Dist` was standing out when profiling stack sizes with top-type-sizes. Here, we trade an allocation per `Dist` for a more reasonable stack size.
  • Loading branch information
konstin committed Jan 18, 2024
1 parent 7b80943 commit b2c8927
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
7 changes: 7 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ seahash = { version = "4.1.0" }
serde = { version = "1.0.194" }
serde_json = { version = "1.0.111" }
sha2 = { version = "0.10.8" }
static_assertions = { version = "1.1.0" }
tar = { version = "0.4.40" }
target-lexicon = { version = "0.12.13" }
tempfile = { version = "3.9.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/distribution-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sha2 = { workspace = true }
static_assertions = { workspace = true }
thiserror = { workspace = true }
url = { workspace = true }
13 changes: 9 additions & 4 deletions crates/distribution-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;

use anyhow::Result;
use static_assertions::assert_eq_size;
use url::Url;

use distribution_filename::{DistFilename, SourceDistFilename, WheelFilename};
Expand Down Expand Up @@ -147,7 +148,7 @@ pub enum SourceDist {
#[derive(Debug, Clone)]
pub struct RegistryBuiltDist {
pub filename: WheelFilename,
pub file: File,
pub file: Box<File>,
pub index: IndexUrl,
}

Expand All @@ -172,7 +173,7 @@ pub struct PathBuiltDist {
#[derive(Debug, Clone)]
pub struct RegistrySourceDist {
pub filename: SourceDistFilename,
pub file: File,
pub file: Box<File>,
pub index: IndexUrl,
}

Expand Down Expand Up @@ -201,21 +202,25 @@ pub struct PathSourceDist {
pub editable: bool,
}

assert_eq_size!(Dist, [u8; 240]);
assert_eq_size!(BuiltDist, [u8; 240]);
assert_eq_size!(SourceDist, [u8; 168]);

impl Dist {
/// Create a [`Dist`] for a registry-based distribution.
pub fn from_registry(filename: DistFilename, file: File, index: IndexUrl) -> Self {
match filename {
DistFilename::WheelFilename(filename) => {
Self::Built(BuiltDist::Registry(RegistryBuiltDist {
filename,
file,
file: Box::new(file),
index,
}))
}
DistFilename::SourceDistFilename(filename) => {
Self::Source(SourceDist::Registry(RegistrySourceDist {
filename,
file,
file: Box::new(file),
index,
}))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/puffin-client/src/flat_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl FlatIndex {

let dist = Dist::Built(BuiltDist::Registry(RegistryBuiltDist {
filename,
file,
file: Box::new(file),
index,
}));
match distributions.0.entry(version) {
Expand All @@ -238,7 +238,7 @@ impl FlatIndex {
DistFilename::SourceDistFilename(filename) => {
let dist = Dist::Source(SourceDist::Registry(RegistrySourceDist {
filename: filename.clone(),
file,
file: Box::new(file),
index,
}));
match distributions.0.entry(filename.version.clone()) {
Expand Down

0 comments on commit b2c8927

Please sign in to comment.