Skip to content
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

dont make new constant InternedString in hot path #14211

Merged
merged 2 commits into from
Jul 8, 2024
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
4 changes: 2 additions & 2 deletions src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::core::{
};
use crate::sources::source::QueryKind;
use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
use crate::util::interning::{InternedString, INTERNED_DEFAULT};

use anyhow::Context as _;
use std::collections::{BTreeSet, HashMap, HashSet};
Expand Down Expand Up @@ -348,7 +348,7 @@ fn build_requirements<'a, 'b: 'a>(

let handle_default = |uses_default_features, reqs: &mut Requirements<'_>| {
if uses_default_features && s.features().contains_key("default") {
if let Err(e) = reqs.require_feature(InternedString::new("default")) {
if let Err(e) = reqs.require_feature(INTERNED_DEFAULT) {
return Err(e.into_activate_error(parent, s));
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::core::dependency::{ArtifactTarget, DepKind, Dependency};
use crate::core::resolver::types::FeaturesSet;
use crate::core::resolver::{Resolve, ResolveBehavior};
use crate::core::{FeatureValue, PackageId, PackageIdSpec, PackageSet, Workspace};
use crate::util::interning::InternedString;
use crate::util::interning::{InternedString, INTERNED_DEFAULT};
use crate::util::CargoResult;
use anyhow::{bail, Context};
use itertools::Itertools;
Expand Down Expand Up @@ -745,9 +745,8 @@ impl<'a, 'gctx> FeatureResolver<'a, 'gctx> {
.iter()
.map(|f| FeatureValue::new(*f))
.collect();
let default = InternedString::new("default");
if dep.uses_default_features() && feature_map.contains_key(&default) {
result.push(FeatureValue::Feature(default));
if dep.uses_default_features() && feature_map.contains_key(&INTERNED_DEFAULT) {
result.push(FeatureValue::Feature(INTERNED_DEFAULT));
}
result
}
Expand All @@ -762,9 +761,8 @@ impl<'a, 'gctx> FeatureResolver<'a, 'gctx> {
let feature_map = summary.features();

let mut result: Vec<FeatureValue> = cli_features.features.iter().cloned().collect();
let default = InternedString::new("default");
if cli_features.uses_default_features && feature_map.contains_key(&default) {
result.push(FeatureValue::Feature(default));
if cli_features.uses_default_features && feature_map.contains_key(&INTERNED_DEFAULT) {
result.push(FeatureValue::Feature(INTERNED_DEFAULT));
}

if cli_features.all_features {
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/ops/tree/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::dependency::DepKind;
use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures};
use crate::core::resolver::Resolve;
use crate::core::{FeatureMap, FeatureValue, Package, PackageId, PackageIdSpec, Workspace};
use crate::util::interning::InternedString;
use crate::util::interning::{InternedString, INTERNED_DEFAULT};
use crate::util::CargoResult;
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -415,7 +415,7 @@ fn add_pkg(
if dep.uses_default_features() {
add_feature(
graph,
InternedString::new("default"),
INTERNED_DEFAULT,
Some(from_index),
dep_index,
EdgeKind::Dep(dep.kind()),
Expand Down Expand Up @@ -505,7 +505,7 @@ fn add_cli_features(
}

if cli_features.uses_default_features {
to_add.insert(FeatureValue::Feature(InternedString::new("default")));
to_add.insert(FeatureValue::Feature(INTERNED_DEFAULT));
}
to_add.extend(cli_features.features.iter().cloned());

Expand Down
18 changes: 15 additions & 3 deletions src/cargo/util/interning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ use std::str;
use std::sync::Mutex;
use std::sync::OnceLock;

static STRING_CACHE: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
pub static INTERNED_DEFAULT: InternedString = InternedString { inner: "default" };

fn interned_storage() -> std::sync::MutexGuard<'static, HashSet<&'static str>> {
static STRING_CACHE: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
STRING_CACHE
.get_or_init(|| {
let mut out: HashSet<&'static str> = Default::default();
out.insert(INTERNED_DEFAULT.as_str());
Mutex::new(out)
})
.lock()
.unwrap()
}

#[derive(Clone, Copy)]
pub struct InternedString {
Expand Down Expand Up @@ -60,8 +72,8 @@ impl Eq for InternedString {}

impl InternedString {
pub fn new(str: &str) -> InternedString {
let mut cache = STRING_CACHE.get_or_init(Default::default).lock().unwrap();
let s = cache.get(str).cloned().unwrap_or_else(|| {
let mut cache = interned_storage();
let s = cache.get(str).copied().unwrap_or_else(|| {
let s = str.to_string().leak();
cache.insert(s);
s
Expand Down
Loading