Skip to content

refactor: replace InternedString with Cow in IndexPackage #15559

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 3 commits into from
Jun 16, 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
7 changes: 3 additions & 4 deletions benches/benchsuite/src/bin/capture-last-use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use cargo::core::global_cache_tracker::{self, DeferredGlobalLastUse, GlobalCacheTracker};
use cargo::util::cache_lock::CacheLockMode;
use cargo::util::interning::InternedString;
use cargo::GlobalContext;
use rand::prelude::*;
use std::collections::HashMap;
Expand Down Expand Up @@ -57,7 +56,7 @@ fn main() {
let cache_dir = real_home.join("registry/cache");
for dir_ent in fs::read_dir(cache_dir).unwrap() {
let registry = dir_ent.unwrap();
let encoded_registry_name = InternedString::new(&registry.file_name().to_string_lossy());
let encoded_registry_name = registry.file_name().to_string_lossy().into();
for krate in fs::read_dir(registry.path()).unwrap() {
let krate = krate.unwrap();
let meta = krate.metadata().unwrap();
Expand All @@ -77,7 +76,7 @@ fn main() {
let cache_dir = real_home.join("registry/src");
for dir_ent in fs::read_dir(cache_dir).unwrap() {
let registry = dir_ent.unwrap();
let encoded_registry_name = InternedString::new(&registry.file_name().to_string_lossy());
let encoded_registry_name = registry.file_name().to_string_lossy().into();
for krate in fs::read_dir(registry.path()).unwrap() {
let krate = krate.unwrap();
let meta = krate.metadata().unwrap();
Expand All @@ -95,7 +94,7 @@ fn main() {
let git_co_dir = real_home.join("git/checkouts");
for dir_ent in fs::read_dir(git_co_dir).unwrap() {
let git_source = dir_ent.unwrap();
let encoded_git_name = InternedString::new(&git_source.file_name().to_string_lossy());
let encoded_git_name = git_source.file_name().to_string_lossy().into();
for co in fs::read_dir(git_source.path()).unwrap() {
let co = co.unwrap();
let meta = co.metadata().unwrap();
Expand Down
3 changes: 1 addition & 2 deletions src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use cargo::ops::cargo_add::AddOptions;
use cargo::ops::cargo_add::DepOp;
use cargo::ops::resolve_ws;
use cargo::util::command_prelude::*;
use cargo::util::interning::InternedString;
use cargo::util::toml_mut::manifest::DepTable;
use cargo::CargoResult;

Expand Down Expand Up @@ -287,7 +286,7 @@ fn parse_dependencies(gctx: &GlobalContext, matches: &ArgMatches) -> CargoResult
.map(String::as_str)
.flat_map(parse_feature)
{
let parsed_value = FeatureValue::new(InternedString::new(feature));
let parsed_value = FeatureValue::new(feature.into());
match parsed_value {
FeatureValue::Feature(_) => {
if 1 < crates.len() {
Expand Down
3 changes: 1 addition & 2 deletions src/bin/cargo/commands/rustc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::command_prelude::*;
use cargo::ops;
use cargo::util::interning::InternedString;

const PRINT_ARG_NAME: &str = "print";
const CRATE_TYPE_ARG_NAME: &str = "crate-type";
Expand Down Expand Up @@ -77,7 +76,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
ProfileChecking::LegacyRustc,
)?;
if compile_opts.build_config.requested_profile == "check" {
compile_opts.build_config.requested_profile = InternedString::new("dev");
compile_opts.build_config.requested_profile = "dev".into();
}
let target_args = values(args, "args");
compile_opts.target_rustc_args = if target_args.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl BuildConfig {
requested_kinds,
jobs,
keep_going,
requested_profile: InternedString::new("dev"),
requested_profile: "dev".into(),
intent,
message_format: MessageFormat::Human,
force_rebuild: false,
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ impl<'de> Deserialize<'de> for DepFingerprint {
let (pkg_id, name, public, hash) = <(u64, String, bool, u64)>::deserialize(d)?;
Ok(DepFingerprint {
pkg_id,
name: InternedString::new(&name),
name: name.into(),
public,
fingerprint: Arc::new(Fingerprint {
memoized_hash: Mutex::new(Some(hash)),
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ impl ArtifactKind {
_ => {
return kind
.strip_prefix("bin:")
.map(|bin_name| ArtifactKind::SelectedBinary(InternedString::new(bin_name)))
.map(|bin_name| ArtifactKind::SelectedBinary(bin_name.into()))
.ok_or_else(|| anyhow::anyhow!("'{}' is not a valid artifact specifier", kind))
}
})
Expand Down
11 changes: 2 additions & 9 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,7 @@ impl Package {
let crate_features = summary
.features()
.iter()
.map(|(k, v)| {
(
*k,
v.iter()
.map(|fv| InternedString::new(&fv.to_string()))
.collect(),
)
})
.map(|(k, v)| (*k, v.iter().map(|fv| fv.to_string().into()).collect()))
.collect();

SerializedPackage {
Expand Down Expand Up @@ -443,7 +436,7 @@ impl<'gctx> PackageSet<'gctx> {
))),
downloads_finished: 0,
downloaded_bytes: 0,
largest: (0, InternedString::new("")),
largest: (0, "".into()),
success: false,
updated_at: Cell::new(Instant::now()),
timeout,
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'de> de::Deserialize<'de> for PackageId {
let (field, rest) = string
.split_once(' ')
.ok_or_else(|| de::Error::custom("invalid serialized PackageId"))?;
let name = InternedString::new(field);
let name = field.into();

let (field, rest) = rest
.split_once(' ')
Expand Down
52 changes: 24 additions & 28 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Profiles {
// Merge with predefined profiles.
use std::collections::btree_map::Entry;
for (predef_name, mut predef_prof) in Self::predefined_profiles().into_iter() {
match profiles.entry(InternedString::new(predef_name)) {
match profiles.entry(predef_name.into()) {
Entry::Vacant(vac) => {
vac.insert(predef_prof);
}
Expand All @@ -119,9 +119,9 @@ impl Profiles {
/// Returns the hard-coded directory names for built-in profiles.
fn predefined_dir_names() -> HashMap<InternedString, InternedString> {
[
(InternedString::new("dev"), InternedString::new("debug")),
(InternedString::new("test"), InternedString::new("debug")),
(InternedString::new("bench"), InternedString::new("release")),
("dev".into(), "debug".into()),
("test".into(), "debug".into()),
("bench".into(), "release".into()),
]
.into()
}
Expand All @@ -134,12 +134,12 @@ impl Profiles {
trim_paths_enabled: bool,
) {
profile_makers.by_name.insert(
InternedString::new("dev"),
"dev".into(),
ProfileMaker::new(Profile::default_dev(), profiles.get("dev").cloned()),
);

profile_makers.by_name.insert(
InternedString::new("release"),
"release".into(),
ProfileMaker::new(
Profile::default_release(trim_paths_enabled),
profiles.get("release").cloned(),
Expand Down Expand Up @@ -185,7 +185,7 @@ impl Profiles {
match &profile.dir_name {
None => {}
Some(dir_name) => {
self.dir_names.insert(name, InternedString::new(dir_name));
self.dir_names.insert(name, dir_name.into());
}
}

Expand Down Expand Up @@ -230,7 +230,7 @@ impl Profiles {
self.get_profile_maker(&inherits_name).unwrap().clone()
}
Some(inherits_name) => {
let inherits_name = InternedString::new(&inherits_name);
let inherits_name = inherits_name.into();
if !set.insert(inherits_name) {
bail!(
"profile inheritance loop detected with profile `{}` inheriting `{}`",
Expand Down Expand Up @@ -297,7 +297,7 @@ impl Profiles {
CompileKind::Target(target) => target.short_name(),
};
if target.contains("-apple-") {
profile.split_debuginfo = Some(InternedString::new("unpacked"));
profile.split_debuginfo = Some("unpacked".into());
}
}

Expand Down Expand Up @@ -455,7 +455,7 @@ impl ProfileMaker {
// basically turning down the optimization level and avoid limiting
// codegen units. This ensures that we spend little time optimizing as
// well as enabling parallelism by not constraining codegen units.
profile.opt_level = InternedString::new("0");
profile.opt_level = "0".into();
profile.codegen_units = None;

// For build dependencies, we usually don't need debuginfo, and
Expand Down Expand Up @@ -531,12 +531,12 @@ fn merge_toml_overrides(
/// Does not merge overrides (see `merge_toml_overrides`).
fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
if let Some(ref opt_level) = toml.opt_level {
profile.opt_level = InternedString::new(&opt_level.0);
profile.opt_level = opt_level.0.as_str().into();
}
match toml.lto {
Some(StringOrBool::Bool(b)) => profile.lto = Lto::Bool(b),
Some(StringOrBool::String(ref n)) if is_off(n.as_str()) => profile.lto = Lto::Off,
Some(StringOrBool::String(ref n)) => profile.lto = Lto::Named(InternedString::new(n)),
Some(StringOrBool::String(ref n)) => profile.lto = Lto::Named(n.into()),
None => {}
}
if toml.codegen_backend.is_some() {
Expand All @@ -552,7 +552,7 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
profile.debug_assertions = debug_assertions;
}
if let Some(split_debuginfo) = &toml.split_debuginfo {
profile.split_debuginfo = Some(InternedString::new(split_debuginfo));
profile.split_debuginfo = Some(split_debuginfo.into());
}
if let Some(rpath) = toml.rpath {
profile.rpath = rpath;
Expand All @@ -578,16 +578,12 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
profile.trim_paths = Some(trim_paths.clone());
}
profile.strip = match toml.strip {
Some(StringOrBool::Bool(true)) => {
Strip::Resolved(StripInner::Named(InternedString::new("symbols")))
}
Some(StringOrBool::Bool(true)) => Strip::Resolved(StripInner::Named("symbols".into())),
Some(StringOrBool::Bool(false)) => Strip::Resolved(StripInner::None),
Some(StringOrBool::String(ref n)) if n.as_str() == "none" => {
Strip::Resolved(StripInner::None)
}
Some(StringOrBool::String(ref n)) => {
Strip::Resolved(StripInner::Named(InternedString::new(n)))
}
Some(StringOrBool::String(ref n)) => Strip::Resolved(StripInner::Named(n.into())),
None => Strip::Deferred(StripInner::None),
};
}
Expand Down Expand Up @@ -635,8 +631,8 @@ pub struct Profile {
impl Default for Profile {
fn default() -> Profile {
Profile {
name: InternedString::new(""),
opt_level: InternedString::new("0"),
name: "".into(),
opt_level: "0".into(),
root: ProfileRoot::Debug,
lto: Lto::Bool(false),
codegen_backend: None,
Expand Down Expand Up @@ -710,7 +706,7 @@ impl Profile {
/// Returns a built-in `dev` profile.
fn default_dev() -> Profile {
Profile {
name: InternedString::new("dev"),
name: "dev".into(),
root: ProfileRoot::Debug,
debuginfo: DebugInfo::Resolved(TomlDebugInfo::Full),
debug_assertions: true,
Expand All @@ -724,9 +720,9 @@ impl Profile {
fn default_release(trim_paths_enabled: bool) -> Profile {
let trim_paths = trim_paths_enabled.then(|| TomlTrimPathsValue::Object.into());
Profile {
name: InternedString::new("release"),
name: "release".into(),
root: ProfileRoot::Release,
opt_level: InternedString::new("3"),
opt_level: "3".into(),
trim_paths,
..Profile::default()
}
Expand Down Expand Up @@ -1274,13 +1270,13 @@ fn merge_config_profiles(
profile.merge(&config_profile);
}
if let Some(inherits) = &profile.inherits {
check_to_add.insert(InternedString::new(inherits));
check_to_add.insert(inherits.into());
}
}
// Add the built-in profiles. This is important for things like `cargo
// test` which implicitly use the "dev" profile for dependencies.
for name in &["dev", "release", "test", "bench"] {
check_to_add.insert(InternedString::new(name));
for name in ["dev", "release", "test", "bench"] {
check_to_add.insert(name.into());
}
// Add config-only profiles.
// Need to iterate repeatedly to get all the inherits values.
Expand All @@ -1291,7 +1287,7 @@ fn merge_config_profiles(
if !profiles.contains_key(name.as_str()) {
if let Some(config_profile) = get_config_profile(ws, &name)? {
if let Some(inherits) = &config_profile.inherits {
check_to_add.insert(InternedString::new(inherits));
check_to_add.insert(inherits.into());
}
profiles.insert(name, config_profile);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl CliFeatures {
.flat_map(|s| s.split_whitespace())
.flat_map(|s| s.split(','))
.filter(|s| !s.is_empty())
.map(InternedString::new)
.map(|s| s.into())
.map(FeatureValue::new)
.collect()
}
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/core/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,15 @@ impl FeatureValue {
Some((dep, dep_feat)) => {
let dep_name = dep.strip_suffix('?');
FeatureValue::DepFeature {
dep_name: InternedString::new(dep_name.unwrap_or(dep)),
dep_feature: InternedString::new(dep_feat),
dep_name: dep_name.unwrap_or(dep).into(),
dep_feature: dep_feat.into(),
weak: dep_name.is_some(),
}
}
None => {
if let Some(dep_name) = feature.strip_prefix("dep:") {
FeatureValue::Dep {
dep_name: InternedString::new(dep_name),
dep_name: dep_name.into(),
}
} else {
FeatureValue::Feature(feature)
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ impl<'gctx> Workspace<'gctx> {
.flatten()
.unique()
.filter(|element| {
let feature = FeatureValue::new(InternedString::new(element));
let feature = FeatureValue::new(element.into());
!cli_features.features.contains(&feature) && !found_features.contains(&feature)
})
.sorted()
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ fn build_resolve_graph_r(
// Given that Cargo doesn't know which target it should resolve to,
// when an artifact dep is specified with { target = "target" },
// keep it with a special "<target>" string,
.or_else(|| Some(InternedString::new("<target>"))),
.or_else(|| Some("<target>".into())),
None => None,
};

Expand Down Expand Up @@ -334,7 +334,7 @@ fn build_resolve_graph_r(
},
// No lib target exists but contains artifact deps.
(None, 1..) => Dep {
name: InternedString::new(""),
name: "".into(),
pkg: pkg_id.to_spec(),
pkg_id,
dep_kinds,
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/registry/info/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub(super) fn pretty_view(
)?;
}

let activated = &[InternedString::new("default")];
let activated = &["default".into()];
let resolved_features = resolve_features(activated, summary.features());
pretty_features(
resolved_features.clone(),
Expand Down
Loading