Skip to content

Commit

Permalink
dont make new InternedString in hot path
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Jul 8, 2024
1 parent e98f702 commit c8070a1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
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, DEFAULT_INTERNED_STRING};

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(DEFAULT_INTERNED_STRING) {
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, DEFAULT_INTERNED_STRING};
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(&DEFAULT_INTERNED_STRING) {
result.push(FeatureValue::Feature(DEFAULT_INTERNED_STRING));
}
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(&DEFAULT_INTERNED_STRING) {
result.push(FeatureValue::Feature(DEFAULT_INTERNED_STRING));
}

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, DEFAULT_INTERNED_STRING};
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"),
DEFAULT_INTERNED_STRING,
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(DEFAULT_INTERNED_STRING));
}
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 DEFAULT_INTERNED_STRING: InternedString = InternedString { inner: "default" };

fn cache_init() -> 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(DEFAULT_INTERNED_STRING.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 = cache_init();
let s = cache.get(str).copied().unwrap_or_else(|| {
let s = str.to_string().leak();
cache.insert(s);
s
Expand Down

0 comments on commit c8070a1

Please sign in to comment.