Skip to content

Commit 4f147e7

Browse files
committed
perf(lsp): avoid String cloning for fs.get_document
1 parent a4ac3ce commit 4f147e7

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

crates/oxc_language_server/src/file_system.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use std::sync::Arc;
2+
13
use tower_lsp_server::ls_types::Uri;
24

35
use crate::{ConcurrentHashMap, LanguageId, TextDocument};
46

57
#[derive(Debug, Default)]
68
pub struct LSPFileSystem {
7-
files: ConcurrentHashMap<Uri, (LanguageId, String)>,
9+
files: ConcurrentHashMap<Uri, (LanguageId, Arc<str>)>,
810
}
911

1012
impl LSPFileSystem {
@@ -14,11 +16,11 @@ impl LSPFileSystem {
1416

1517
pub fn set(&self, uri: Uri, content: String) {
1618
let language_id = self.get_language_id(&uri).unwrap_or_default();
17-
self.files.pin().insert(uri, (language_id, content));
19+
self.files.pin().insert(uri, (language_id, Arc::from(content)));
1820
}
1921

2022
pub fn set_with_language(&self, uri: Uri, language_id: LanguageId, content: String) {
21-
self.files.pin().insert(uri, (language_id, content));
23+
self.files.pin().insert(uri, (language_id, Arc::from(content)));
2224
}
2325

2426
pub fn get_language_id(&self, uri: &Uri) -> Option<LanguageId> {
@@ -31,7 +33,7 @@ impl LSPFileSystem {
3133
|(language_id, content)| TextDocument {
3234
uri,
3335
language_id: language_id.clone(),
34-
text: Some(content.clone()),
36+
text: Some(Arc::clone(content)),
3537
},
3638
)
3739
}

crates/oxc_language_server/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ pub type ConcurrentHashMap<K, V> = papaya::HashMap<K, V, FxBuildHasher>;
2424
pub struct TextDocument<'a> {
2525
pub uri: &'a Uri,
2626
pub language_id: LanguageId,
27-
pub text: Option<String>,
27+
pub text: Option<Arc<str>>,
2828
}
2929

3030
impl<'a> TextDocument<'a> {
31-
pub fn new(uri: &'a Uri, language_id: LanguageId, text: Option<String>) -> Self {
31+
pub fn new(uri: &'a Uri, language_id: LanguageId, text: Option<Arc<str>>) -> Self {
3232
Self { uri, language_id, text }
3333
}
3434
}

crates/oxc_language_server/src/worker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ mod tests {
726726
.run_diagnostic(&TextDocument::new(
727727
&uri,
728728
LanguageId::default(),
729-
Some("helloworld".to_string()),
729+
Some(Arc::from("helloworld")),
730730
))
731731
.await
732732
.unwrap();
@@ -790,7 +790,7 @@ mod tests {
790790
.run_diagnostic_on_change(&TextDocument::new(
791791
&uri,
792792
LanguageId::default(),
793-
Some("helloworld".to_string()),
793+
Some(Arc::from("helloworld")),
794794
))
795795
.await
796796
.unwrap();
@@ -853,7 +853,7 @@ mod tests {
853853
.run_diagnostic_on_save(&TextDocument::new(
854854
&uri,
855855
LanguageId::default(),
856-
Some("helloworld".to_string()),
856+
Some(Arc::from("helloworld")),
857857
))
858858
.await
859859
.unwrap();

0 commit comments

Comments
 (0)