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:
@@ -746,7 +746,7 @@ async fn test_running_multiple_instances_of_a_single_server_in_one_worktree(
|
||||
worktree_id,
|
||||
path: rel_path("project-b/source_file.py").into(),
|
||||
},
|
||||
LanguageName::new("Python"),
|
||||
LanguageName::new_static("Python"),
|
||||
cx,
|
||||
)
|
||||
})
|
||||
@@ -762,7 +762,7 @@ async fn test_running_multiple_instances_of_a_single_server_in_one_worktree(
|
||||
worktree_id,
|
||||
path: rel_path("project-b/source_file.py").into(),
|
||||
},
|
||||
LanguageName::new("Python"),
|
||||
LanguageName::new_static("Python"),
|
||||
cx,
|
||||
)
|
||||
})
|
||||
|
||||
@@ -111,7 +111,7 @@ impl Project {
|
||||
);
|
||||
let toolchains = project_path_contexts
|
||||
.filter(|_| detect_venv)
|
||||
.map(|p| self.active_toolchain(p, LanguageName::new("Python"), cx))
|
||||
.map(|p| self.active_toolchain(p, LanguageName::new_static("Python"), cx))
|
||||
.collect::<Vec<_>>();
|
||||
let lang_registry = self.languages.clone();
|
||||
cx.spawn(async move |project, cx| {
|
||||
@@ -311,7 +311,7 @@ impl Project {
|
||||
);
|
||||
let toolchains = project_path_contexts
|
||||
.filter(|_| detect_venv)
|
||||
.map(|p| self.active_toolchain(p, LanguageName::new("Python"), cx))
|
||||
.map(|p| self.active_toolchain(p, LanguageName::new_static("Python"), cx))
|
||||
.collect::<Vec<_>>();
|
||||
let remote_client = self.remote_client.clone();
|
||||
let shell = match &remote_client {
|
||||
|
||||
Reference in New Issue
Block a user