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:
@@ -151,7 +151,7 @@ fn test_select_language(cx: &mut App) {
|
||||
let registry = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
|
||||
registry.add(Arc::new(Language::new(
|
||||
LanguageConfig {
|
||||
name: LanguageName::new("Rust"),
|
||||
name: LanguageName::new_static("Rust"),
|
||||
matcher: LanguageMatcher {
|
||||
path_suffixes: vec!["rs".to_string()],
|
||||
..Default::default()
|
||||
@@ -173,7 +173,7 @@ fn test_select_language(cx: &mut App) {
|
||||
)));
|
||||
registry.add(Arc::new(Language::new(
|
||||
LanguageConfig {
|
||||
name: LanguageName::new("Make"),
|
||||
name: LanguageName::new_static("Make"),
|
||||
matcher: LanguageMatcher {
|
||||
path_suffixes: vec!["Makefile".to_string(), "mk".to_string()],
|
||||
..Default::default()
|
||||
@@ -3728,7 +3728,7 @@ fn ruby_lang() -> Language {
|
||||
fn html_lang() -> Language {
|
||||
Language::new(
|
||||
LanguageConfig {
|
||||
name: LanguageName::new("HTML"),
|
||||
name: LanguageName::new_static("HTML"),
|
||||
block_comment: Some(BlockCommentConfig {
|
||||
start: "<!--".into(),
|
||||
prefix: "".into(),
|
||||
|
||||
@@ -982,7 +982,7 @@ impl<T> Override<T> {
|
||||
impl Default for LanguageConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: LanguageName::new(""),
|
||||
name: LanguageName::new_static(""),
|
||||
code_fence_block_name: None,
|
||||
grammar: None,
|
||||
matcher: LanguageMatcher::default(),
|
||||
@@ -2756,9 +2756,9 @@ mod tests {
|
||||
assert_eq!(
|
||||
languages.language_names(),
|
||||
&[
|
||||
LanguageName::new("JSON"),
|
||||
LanguageName::new("Plain Text"),
|
||||
LanguageName::new("Rust"),
|
||||
LanguageName::new_static("JSON"),
|
||||
LanguageName::new_static("Plain Text"),
|
||||
LanguageName::new_static("Rust"),
|
||||
]
|
||||
);
|
||||
|
||||
@@ -2769,9 +2769,9 @@ mod tests {
|
||||
assert_eq!(
|
||||
languages.language_names(),
|
||||
&[
|
||||
LanguageName::new("JSON"),
|
||||
LanguageName::new("Plain Text"),
|
||||
LanguageName::new("Rust"),
|
||||
LanguageName::new_static("JSON"),
|
||||
LanguageName::new_static("Plain Text"),
|
||||
LanguageName::new_static("Rust"),
|
||||
]
|
||||
);
|
||||
|
||||
@@ -2782,9 +2782,9 @@ mod tests {
|
||||
assert_eq!(
|
||||
languages.language_names(),
|
||||
&[
|
||||
LanguageName::new("JSON"),
|
||||
LanguageName::new("Plain Text"),
|
||||
LanguageName::new("Rust"),
|
||||
LanguageName::new_static("JSON"),
|
||||
LanguageName::new_static("Plain Text"),
|
||||
LanguageName::new_static("Rust"),
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
@@ -43,12 +43,18 @@ impl LanguageName {
|
||||
Self(SharedString::new(s))
|
||||
}
|
||||
|
||||
pub fn new_static(s: &'static str) -> Self {
|
||||
Self(SharedString::new_static(s))
|
||||
}
|
||||
|
||||
pub fn from_proto(s: String) -> Self {
|
||||
Self(SharedString::from(s))
|
||||
}
|
||||
|
||||
pub fn to_proto(&self) -> String {
|
||||
self.0.to_string()
|
||||
}
|
||||
|
||||
pub fn lsp_id(&self) -> String {
|
||||
match self.0.as_ref() {
|
||||
"Plain Text" => "plaintext".to_string(),
|
||||
@@ -87,9 +93,9 @@ impl std::fmt::Display for LanguageName {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for LanguageName {
|
||||
fn from(str: &'a str) -> LanguageName {
|
||||
LanguageName(SharedString::new(str))
|
||||
impl From<&'static str> for LanguageName {
|
||||
fn from(str: &'static str) -> Self {
|
||||
Self(SharedString::new_static(str))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user