Skip to content

perf(lsp): don't clone asset text #28165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cli/lsp/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use super::text::LineIndex;
use super::tsc;
use super::tsc::AssetDocument;
use crate::graph_util::CliJsrUrlProvider;
use crate::tsc::MaybeStaticSource;

pub const DOCUMENT_SCHEMES: [&str; 5] =
["data", "blob", "file", "http", "https"];
Expand Down Expand Up @@ -218,10 +219,12 @@ impl AssetOrDocument {
}
}

pub fn text(&self) -> Arc<str> {
pub fn text(&self) -> MaybeStaticSource {
match self {
AssetOrDocument::Asset(a) => a.text(),
AssetOrDocument::Document(d) => d.text.clone(),
AssetOrDocument::Document(d) => {
MaybeStaticSource::Computed(d.text.clone())
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2805,7 +2805,7 @@ impl Inner {
}
Ok(span.to_folding_range(
asset_or_doc.line_index(),
asset_or_doc.text().as_bytes(),
asset_or_doc.text().as_ref().as_bytes(),
self.config.line_folding_only_capable(),
))
})
Expand Down
15 changes: 8 additions & 7 deletions cli/lsp/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ use crate::args::jsr_url;
use crate::args::FmtOptionsConfig;
use crate::lsp::logging::lsp_warn;
use crate::tsc;
use crate::tsc::MaybeStaticSource;
use crate::tsc::ResolveArgs;
use crate::tsc::MISSING_DEPENDENCY_SPECIFIER;
use crate::util::path::relative_specifier;
Expand Down Expand Up @@ -1403,18 +1404,18 @@ impl TsServer {
#[derive(Debug, Clone)]
pub struct AssetDocument {
specifier: ModuleSpecifier,
text: Arc<str>,
text: MaybeStaticSource,
line_index: Arc<LineIndex>,
maybe_navigation_tree: Option<Arc<NavigationTree>>,
}

impl AssetDocument {
pub fn new(specifier: ModuleSpecifier, text: impl AsRef<str>) -> Self {
let text = text.as_ref();
pub fn new(specifier: ModuleSpecifier, text: MaybeStaticSource) -> Self {
let line_index = Arc::new(LineIndex::new(text.as_ref()));
Self {
specifier,
text: text.into(),
line_index: Arc::new(LineIndex::new(text)),
text,
line_index,
maybe_navigation_tree: None,
}
}
Expand All @@ -1430,7 +1431,7 @@ impl AssetDocument {
}
}

pub fn text(&self) -> Arc<str> {
pub fn text(&self) -> MaybeStaticSource {
self.text.clone()
}

Expand Down Expand Up @@ -4622,7 +4623,7 @@ enum LoadError {
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct LoadResponse {
data: Arc<str>,
data: MaybeStaticSource,
script_kind: i32,
version: Option<String>,
is_cjs: bool,
Expand Down
3 changes: 2 additions & 1 deletion cli/tsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ pub enum StaticAssetSource {
}

/// Like a `Cow` but the owned form is an `Arc<str>` instead of `String`
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum MaybeStaticSource {
Computed(Arc<str>),
Static(&'static str),
Expand Down
Loading