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
@@ -870,7 +870,7 @@ impl DebugAdapter for PythonDebugAdapter {
.active_toolchain(
delegate.worktree_id(),
base_path.into_arc(),
language::LanguageName::new(Self::LANGUAGE_NAME),
language::LanguageName::new_static(Self::LANGUAGE_NAME),
cx,
)
.await