From 387059c6b2b72c8c3f868c932401fccfc635c95a Mon Sep 17 00:00:00 2001 From: tidely <43219534+tidely@users.noreply.github.com> Date: Mon, 8 Dec 2025 20:57:02 +0200 Subject: [PATCH] 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` 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 --- crates/dap_adapters/src/python.rs | 2 +- crates/editor/src/editor_tests.rs | 2 +- .../src/extension_store_test.rs | 14 +++---- crates/language/src/buffer_tests.rs | 6 +-- crates/language/src/language.rs | 20 +++++----- crates/language/src/language_registry.rs | 12 ++++-- .../src/extension_lsp_adapter.rs | 2 +- crates/languages/src/json.rs | 4 +- crates/languages/src/python.rs | 4 +- crates/languages/src/tailwind.rs | 40 ++++++++++++------- crates/languages/src/typescript.rs | 6 +-- crates/languages/src/vtsls.rs | 6 +-- crates/project/src/project_tests.rs | 4 +- crates/project/src/terminals.rs | 4 +- crates/repl/src/kernels/mod.rs | 2 +- crates/vim/src/normal/paste.rs | 2 +- 16 files changed, 74 insertions(+), 56 deletions(-) diff --git a/crates/dap_adapters/src/python.rs b/crates/dap_adapters/src/python.rs index 2f84193ac9343cac9ff1cf52eb648bad1cd77896..a45e16dc32180e1e4ed3b2ec01c92ad07ffc5e93 100644 --- a/crates/dap_adapters/src/python.rs +++ b/crates/dap_adapters/src/python.rs @@ -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 diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index fe1ce5cbdb7e5503136845630fcaaff7aade855a..75aa29be7a31ef6a4707e538ea21a2ca45aff395 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -9970,7 +9970,7 @@ async fn test_autoindent_disabled_with_nested_language(cx: &mut TestAppContext) ], ..Default::default() }, - name: LanguageName::new("rust"), + name: LanguageName::new_static("rust"), ..Default::default() }, Some(tree_sitter_rust::LANGUAGE.into()), diff --git a/crates/extension_host/src/extension_store_test.rs b/crates/extension_host/src/extension_store_test.rs index 6d3aadeb5ac498b3948d871a0a87f7ecf49b6bd8..54b090347ffad3ffed444827f5cb60c120d25ad7 100644 --- a/crates/extension_host/src/extension_store_test.rs +++ b/crates/extension_host/src/extension_store_test.rs @@ -307,9 +307,9 @@ async fn test_extension_store(cx: &mut TestAppContext) { assert_eq!( language_registry.language_names(), [ - LanguageName::new("ERB"), - LanguageName::new("Plain Text"), - LanguageName::new("Ruby"), + LanguageName::new_static("ERB"), + LanguageName::new_static("Plain Text"), + LanguageName::new_static("Ruby"), ] ); assert_eq!( @@ -463,9 +463,9 @@ async fn test_extension_store(cx: &mut TestAppContext) { assert_eq!( language_registry.language_names(), [ - LanguageName::new("ERB"), - LanguageName::new("Plain Text"), - LanguageName::new("Ruby"), + LanguageName::new_static("ERB"), + LanguageName::new_static("Plain Text"), + LanguageName::new_static("Ruby"), ] ); assert_eq!( @@ -523,7 +523,7 @@ async fn test_extension_store(cx: &mut TestAppContext) { assert_eq!( language_registry.language_names(), - [LanguageName::new("Plain Text")] + [LanguageName::new_static("Plain Text")] ); assert_eq!(language_registry.grammar_names(), []); }); diff --git a/crates/language/src/buffer_tests.rs b/crates/language/src/buffer_tests.rs index 6b5d2450fe72f46b728be0f5b151801fe2e7fa70..54e2ef4065460547f4a3f86db7d3a3986dff65eb 100644 --- a/crates/language/src/buffer_tests.rs +++ b/crates/language/src/buffer_tests.rs @@ -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: "