language: Add LanguageName::new_static to reduce allocations (#44380)

Implements a specialized constructor `LanguageName::new_static` for
`&'static str` which reduces allocations.

`LanguageName::new` always backs the underlying `SharedString` with an
owned `Arc<str>` even when a `&'static str` is passed. This makes us
allocate each time we create a new `LanguageName` no matter what.
Creating a specialized constructor for `&'static str` allows us to
essentially construct them for free.

Additional change:
Encourages using explicit constructors to avoid needless allocations.
Currently there were no instances of this trait being called where the
lifetime was not `'static` saving another 48 locations of allocation.

```rust
impl<'a> From<&'a str> for LanguageName {
    fn from(str: &'a str) -> Self {
        Self(SharedString::new(str))
    }
}

// to 

impl From<&'static str> for LanguageName {
    fn from(str: &'static str) -> Self {
        Self(SharedString::new_static(str))
    }
}

```

Release Notes:

- N/A
This commit is contained in:
tidely
2025-12-08 19:57:02 +01:00
committed by GitHub
parent 4a382b2797
commit 387059c6b2
16 changed files with 74 additions and 56 deletions
+1 -1
View File
@@ -717,7 +717,7 @@ mod test {
cx.update_global(|store: &mut SettingsStore, cx| {
store.update_user_settings(cx, |settings| {
settings.project.all_languages.languages.0.insert(
LanguageName::new("Rust").0,
LanguageName::new_static("Rust").0,
LanguageSettingsContent {
auto_indent_on_paste: Some(false),
..Default::default()