Skip to content

Commit

Permalink
fix: semantic tokens performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Feel-ix-343 committed Mar 28, 2024
1 parent 0028c45 commit b305b79
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
25 changes: 25 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::btree_set::Intersection;
use std::collections::HashSet;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -149,6 +150,9 @@ impl Backend {
}

async fn publish_diagnostics(&self) -> Result<()> {

let timer = std::time::Instant::now();

self.client
.log_message(MessageType::WARNING, "Diagnostics Started")
.await;
Expand Down Expand Up @@ -185,6 +189,16 @@ impl Backend {
.log_message(MessageType::WARNING, "Diagnostics Done")
.await;


let elapsed = timer.elapsed();

self.client
.log_message(
MessageType::WARNING,
format!("Diagnostics Done took {}ms", elapsed.as_millis()),
)
.await;

Ok(())
}

Expand Down Expand Up @@ -602,11 +616,22 @@ impl LanguageServer for Backend {

let settings = self.bind_settings(|settings| Ok(settings.clone())).await?;

let timer = std::time::Instant::now();

let path = params_path!(params)?;
let res = self
.bind_vault(|vault| Ok(tokens::semantic_tokens_full(vault, &path, params, &settings)))
.await;

let elapsed = timer.elapsed();

self.client
.log_message(
MessageType::WARNING,
format!("Semantic Tokens Done took {}ms", elapsed.as_millis()),
)
.await;

return res;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/tokens.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{iter, path::Path};
use std::{collections::HashSet, iter, path::Path};

use itertools::Itertools;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use tower_lsp::lsp_types::{SemanticToken, SemanticTokensParams, SemanticTokensResult};

use crate::{config::Settings, vault::{Referenceable, Vault}};
use crate::{config::Settings, diagnostics::path_unresolved_references, vault::{Referenceable, Vault}};

pub fn semantic_tokens_full(
vault: &Vault,
Expand All @@ -17,6 +18,8 @@ pub fn semantic_tokens_full(

let references_in_file = vault.select_references(Some(path))?;

let path_unresolved: Option<HashSet<_>> = path_unresolved_references(vault, path).map(|thing| thing.into_par_iter().map(|(_, reference)| reference).collect());

let tokens = references_in_file
.into_iter()
.sorted_by_key(|(_, reference)| {
Expand All @@ -28,9 +31,7 @@ pub fn semantic_tokens_full(
.fold(vec![], |acc, (path, reference)| {
let range = reference.data().range;

let is_unresolved = vault.select_referenceables_for_reference(reference, path)
.into_iter()
.any(|referenceable| referenceable.is_unresolved());
let is_unresolved = path_unresolved.as_ref().is_some_and(|unresolved| unresolved.contains(reference));

match acc[..] {
[] => vec![(
Expand Down Expand Up @@ -62,9 +63,9 @@ pub fn semantic_tokens_full(
.collect_vec(),
}
})
.into_iter()
.into_par_iter()
.map(|(_, token)| token)
.collect_vec(); // TODO: holy this is bad
.collect::<Vec<_>>(); // TODO: holy this is bad

Some(SemanticTokensResult::Tokens(
tower_lsp::lsp_types::SemanticTokens {
Expand Down

0 comments on commit b305b79

Please sign in to comment.