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
+2 -2
View File
@@ -286,8 +286,8 @@ impl LspAdapter for JsonLspAdapter {
fn language_ids(&self) -> HashMap<LanguageName, String> {
[
(LanguageName::new("JSON"), "json".into()),
(LanguageName::new("JSONC"), "jsonc".into()),
(LanguageName::new_static("JSON"), "json".into()),
(LanguageName::new_static("JSONC"), "jsonc".into()),
]
.into_iter()
.collect()
+2 -2
View File
@@ -903,7 +903,7 @@ impl ContextProvider for PythonContextProvider {
fn selected_test_runner(location: Option<&Arc<dyn language::File>>, cx: &App) -> TestRunner {
const TEST_RUNNER_VARIABLE: &str = "TEST_RUNNER";
language_settings(Some(LanguageName::new("Python")), location, cx)
language_settings(Some(LanguageName::new_static("Python")), location, cx)
.tasks
.variables
.get(TEST_RUNNER_VARIABLE)
@@ -1397,7 +1397,7 @@ async fn venv_to_toolchain(venv: PythonEnvironment, fs: &dyn Fs) -> Option<Toolc
.to_str()?
.to_owned()
.into(),
language_name: LanguageName::new("Python"),
language_name: LanguageName::new_static("Python"),
as_json: serde_json::to_value(data).ok()?,
})
}
+26 -14
View File
@@ -174,20 +174,32 @@ impl LspAdapter for TailwindLspAdapter {
fn language_ids(&self) -> HashMap<LanguageName, String> {
HashMap::from_iter([
(LanguageName::new("Astro"), "astro".to_string()),
(LanguageName::new("HTML"), "html".to_string()),
(LanguageName::new("Gleam"), "html".to_string()),
(LanguageName::new("CSS"), "css".to_string()),
(LanguageName::new("JavaScript"), "javascript".to_string()),
(LanguageName::new("TypeScript"), "typescript".to_string()),
(LanguageName::new("TSX"), "typescriptreact".to_string()),
(LanguageName::new("Svelte"), "svelte".to_string()),
(LanguageName::new("Elixir"), "phoenix-heex".to_string()),
(LanguageName::new("HEEX"), "phoenix-heex".to_string()),
(LanguageName::new("ERB"), "erb".to_string()),
(LanguageName::new("HTML+ERB"), "erb".to_string()),
(LanguageName::new("PHP"), "php".to_string()),
(LanguageName::new("Vue.js"), "vue".to_string()),
(LanguageName::new_static("Astro"), "astro".to_string()),
(LanguageName::new_static("HTML"), "html".to_string()),
(LanguageName::new_static("Gleam"), "html".to_string()),
(LanguageName::new_static("CSS"), "css".to_string()),
(
LanguageName::new_static("JavaScript"),
"javascript".to_string(),
),
(
LanguageName::new_static("TypeScript"),
"typescript".to_string(),
),
(
LanguageName::new_static("TSX"),
"typescriptreact".to_string(),
),
(LanguageName::new_static("Svelte"), "svelte".to_string()),
(
LanguageName::new_static("Elixir"),
"phoenix-heex".to_string(),
),
(LanguageName::new_static("HEEX"), "phoenix-heex".to_string()),
(LanguageName::new_static("ERB"), "erb".to_string()),
(LanguageName::new_static("HTML+ERB"), "erb".to_string()),
(LanguageName::new_static("PHP"), "php".to_string()),
(LanguageName::new_static("Vue.js"), "vue".to_string()),
])
}
}
+3 -3
View File
@@ -822,9 +822,9 @@ impl LspAdapter for TypeScriptLspAdapter {
fn language_ids(&self) -> HashMap<LanguageName, String> {
HashMap::from_iter([
(LanguageName::new("TypeScript"), "typescript".into()),
(LanguageName::new("JavaScript"), "javascript".into()),
(LanguageName::new("TSX"), "typescriptreact".into()),
(LanguageName::new_static("TypeScript"), "typescript".into()),
(LanguageName::new_static("JavaScript"), "javascript".into()),
(LanguageName::new_static("TSX"), "typescriptreact".into()),
])
}
}
+3 -3
View File
@@ -296,9 +296,9 @@ impl LspAdapter for VtslsLspAdapter {
fn language_ids(&self) -> HashMap<LanguageName, String> {
HashMap::from_iter([
(LanguageName::new("TypeScript"), "typescript".into()),
(LanguageName::new("JavaScript"), "javascript".into()),
(LanguageName::new("TSX"), "typescriptreact".into()),
(LanguageName::new_static("TypeScript"), "typescript".into()),
(LanguageName::new_static("JavaScript"), "javascript".into()),
(LanguageName::new_static("TSX"), "typescriptreact".into()),
])
}
}