Skip to content
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

LSP: Formatting minified JS/JSON files is slow #20754

Closed
Andriamanitra opened this issue Oct 1, 2023 · 0 comments · Fixed by #20829
Closed

LSP: Formatting minified JS/JSON files is slow #20754

Andriamanitra opened this issue Oct 1, 2023 · 0 comments · Fixed by #20829
Assignees
Labels
lsp related to the language server perf performance related

Comments

@Andriamanitra
Copy link
Contributor

When formatting a 46KB minified .js file using textDocument/formatting request it takes about 5–6 seconds for deno lsp to respond. For my particular test file the language server responds with Content-Length: 546612 worth of TextEdit[] (that is about 11 times more than just sending the full file contents). For comparison formatting the same file takes only around 100ms using deno fmt.

Fix?

I think this could be fixed with some heuristics in the function that calculates text edits:

deno/cli/lsp/text.rs

Lines 209 to 258 in 8d24be1

pub fn get_edits(a: &str, b: &str, line_index: &LineIndex) -> Vec<TextEdit> {
if a == b {
return vec![];
}
let chunks = diff(a, b);
let mut text_edits = Vec::<TextEdit>::new();
let mut iter = chunks.iter().peekable();
let mut a_pos = TextSize::from(0);
loop {
let chunk = iter.next();
match chunk {
None => break,
Some(Chunk::Equal(e)) => {
a_pos += TextSize::from(e.encode_utf16().count() as u32);
}
Some(Chunk::Delete(d)) => {
let start = line_index.position_utf16(a_pos);
a_pos += TextSize::from(d.encode_utf16().count() as u32);
let end = line_index.position_utf16(a_pos);
let range = lsp::Range { start, end };
match iter.peek() {
Some(Chunk::Insert(i)) => {
iter.next();
text_edits.push(TextEdit {
range,
new_text: i.to_string(),
});
}
_ => text_edits.push(TextEdit {
range,
new_text: "".to_string(),
}),
}
}
Some(Chunk::Insert(i)) => {
let pos = line_index.position_utf16(a_pos);
let range = lsp::Range {
start: pos,
end: pos,
};
text_edits.push(TextEdit {
range,
new_text: i.to_string(),
});
}
}
}
text_edits
}

A simple one would be to skip calculating the diff and treating the whole file as one big text edit if the lengths of a and b differ significantly.

Version information:

deno 1.37.0 (release, x86_64-unknown-linux-gnu)
v8 11.8.172.3
typescript 5.2.2

@bartlomieju bartlomieju added perf performance related lsp related to the language server labels Oct 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lsp related to the language server perf performance related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants