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

refactor: clone-on-write when needed for InternedString #14808

Merged
merged 1 commit into from
Nov 11, 2024
Merged
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
11 changes: 8 additions & 3 deletions src/cargo/util/interning.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde::{Serialize, Serializer};
use serde_untagged::UntaggedEnumVisitor;
use std::borrow::Borrow;
use std::borrow::Cow;
use std::cmp::Ordering;
use std::collections::HashSet;
use std::ffi::OsStr;
Expand Down Expand Up @@ -46,7 +47,7 @@ impl<'a> From<&'a String> for InternedString {

impl From<String> for InternedString {
fn from(item: String) -> Self {
InternedString::new(&item)
InternedString::from_cow(item.into())
}
}

Expand All @@ -72,9 +73,13 @@ impl Eq for InternedString {}

impl InternedString {
pub fn new(str: &str) -> InternedString {
InternedString::from_cow(str.into())
}

fn from_cow<'a>(str: Cow<'a, str>) -> InternedString {
let mut cache = interned_storage();
let s = cache.get(str).copied().unwrap_or_else(|| {
let s = str.to_string().leak();
let s = cache.get(str.as_ref()).copied().unwrap_or_else(|| {
let s = str.into_owned().leak();
cache.insert(s);
s
});
Expand Down