From 4d814322132467694e4a38f6b7de50e57f7daf55 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 20 Sep 2019 20:38:16 +0300 Subject: allow compiling ra_ide_api on wasm --- crates/ra_ide_api/Cargo.toml | 3 +++ crates/ra_ide_api/src/change.rs | 20 ++++++++++++++++--- crates/ra_ide_api/src/db.rs | 10 +++++----- crates/ra_ide_api/src/lib.rs | 1 + crates/ra_ide_api/src/symbol_index.rs | 37 +++++++++++++++++++++++++++++++---- crates/ra_ide_api/src/wasm_shims.rs | 17 ++++++++++++++++ 6 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 crates/ra_ide_api/src/wasm_shims.rs (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/Cargo.toml b/crates/ra_ide_api/Cargo.toml index 000694620..6bbf9d5dd 100644 --- a/crates/ra_ide_api/Cargo.toml +++ b/crates/ra_ide_api/Cargo.toml @@ -4,6 +4,9 @@ name = "ra_ide_api" version = "0.1.0" authors = ["rust-analyzer developers"] +[features] +wasm = [] + [dependencies] format-buf = "1.0.0" itertools = "0.8.0" diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs index 0d52f5ffb..cc476a237 100644 --- a/crates/ra_ide_api/src/change.rs +++ b/crates/ra_ide_api/src/change.rs @@ -6,6 +6,7 @@ use ra_db::{ }; use ra_prof::{memory_usage, profile, Bytes}; use ra_syntax::SourceFile; +#[cfg(not(feature = "wasm"))] use rayon::prelude::*; use relative_path::RelativePathBuf; use rustc_hash::FxHashMap; @@ -143,7 +144,12 @@ impl LibraryData { root_id: SourceRootId, files: Vec<(FileId, RelativePathBuf, Arc)>, ) -> LibraryData { - let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| { + #[cfg(not(feature = "wasm"))] + let iter = files.par_iter(); + #[cfg(feature = "wasm")] + let iter = files.iter(); + + let symbol_index = SymbolIndex::for_files(iter.map(|(file_id, _, text)| { let parse = SourceFile::parse(text); (*file_id, parse) })); @@ -234,8 +240,12 @@ impl RootDatabase { } pub(crate) fn maybe_collect_garbage(&mut self) { + if cfg!(feature = "wasm") { + return; + } + if self.last_gc_check.elapsed() > GC_COOLDOWN { - self.last_gc_check = time::Instant::now(); + self.last_gc_check = crate::wasm_shims::Instant::now(); let retained_trees = syntax_tree_stats(self).retained; if retained_trees > 100 { log::info!("automatic garbadge collection, {} retained trees", retained_trees); @@ -245,8 +255,12 @@ impl RootDatabase { } pub(crate) fn collect_garbage(&mut self) { + if cfg!(feature = "wasm") { + return; + } + let _p = profile("RootDatabase::collect_garbage"); - self.last_gc = time::Instant::now(); + self.last_gc = crate::wasm_shims::Instant::now(); let sweep = SweepStrategy::default().discard_values().sweep_all_revisions(); diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs index 4c5159d61..afd9022ce 100644 --- a/crates/ra_ide_api/src/db.rs +++ b/crates/ra_ide_api/src/db.rs @@ -1,4 +1,4 @@ -use std::{sync::Arc, time}; +use std::sync::Arc; use ra_db::{ salsa::{self, Database, Durability}, @@ -25,8 +25,8 @@ pub(crate) struct RootDatabase { runtime: salsa::Runtime, pub(crate) feature_flags: Arc, pub(crate) debug_data: Arc, - pub(crate) last_gc: time::Instant, - pub(crate) last_gc_check: time::Instant, + pub(crate) last_gc: crate::wasm_shims::Instant, + pub(crate) last_gc_check: crate::wasm_shims::Instant, } impl hir::debug::HirDebugHelper for RootDatabase { @@ -69,8 +69,8 @@ impl RootDatabase { pub fn new(lru_capacity: Option, feature_flags: FeatureFlags) -> RootDatabase { let mut db = RootDatabase { runtime: salsa::Runtime::default(), - last_gc: time::Instant::now(), - last_gc_check: time::Instant::now(), + last_gc: crate::wasm_shims::Instant::now(), + last_gc_check: crate::wasm_shims::Instant::now(), feature_flags: Arc::new(feature_flags), debug_data: Default::default(), }; diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 412dc4d71..44d1ec77b 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -40,6 +40,7 @@ mod typing; mod matching_brace; mod display; mod inlay_hints; +mod wasm_shims; #[cfg(test)] mod marks; diff --git a/crates/ra_ide_api/src/symbol_index.rs b/crates/ra_ide_api/src/symbol_index.rs index a5729c368..02cdfbc60 100644 --- a/crates/ra_ide_api/src/symbol_index.rs +++ b/crates/ra_ide_api/src/symbol_index.rs @@ -38,6 +38,7 @@ use ra_syntax::{ SyntaxKind::{self, *}, SyntaxNode, SyntaxNodePtr, TextRange, WalkEvent, }; +#[cfg(not(feature = "wasm"))] use rayon::prelude::*; use crate::{db::RootDatabase, FileId, Query}; @@ -79,10 +80,17 @@ pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Vec let buf: Vec> = if query.libs { let snap = Snap(db.snapshot()); - db.library_roots() + #[cfg(not(feature = "wasm"))] + let buf = db + .library_roots() .par_iter() .map_with(snap, |db, &lib_id| db.0.library_symbols(lib_id)) - .collect() + .collect(); + + #[cfg(feature = "wasm")] + let buf = db.library_roots().iter().map(|&lib_id| snap.0.library_symbols(lib_id)).collect(); + + buf } else { let mut files = Vec::new(); for &root in db.local_roots().iter() { @@ -91,7 +99,14 @@ pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Vec } let snap = Snap(db.snapshot()); - files.par_iter().map_with(snap, |db, &file_id| db.0.file_symbols(file_id)).collect() + #[cfg(not(feature = "wasm"))] + let buf = + files.par_iter().map_with(snap, |db, &file_id| db.0.file_symbols(file_id)).collect(); + + #[cfg(feature = "wasm")] + let buf = files.iter().map(|&file_id| snap.0.file_symbols(file_id)).collect(); + + buf }; query.search(&buf) } @@ -135,9 +150,12 @@ impl SymbolIndex { fn cmp_key<'a>(s1: &'a FileSymbol) -> impl Ord + 'a { unicase::Ascii::new(s1.name.as_str()) } - + #[cfg(not(feature = "wasm"))] symbols.par_sort_by(|s1, s2| cmp_key(s1).cmp(&cmp_key(s2))); + #[cfg(feature = "wasm")] + symbols.sort_by(|s1, s2| cmp_key(s1).cmp(&cmp_key(s2))); + let mut builder = fst::MapBuilder::memory(); let mut last_batch_start = 0; @@ -169,6 +187,7 @@ impl SymbolIndex { self.map.as_fst().size() + self.symbols.len() * mem::size_of::() } + #[cfg(not(feature = "wasm"))] pub(crate) fn for_files( files: impl ParallelIterator)>, ) -> SymbolIndex { @@ -178,6 +197,16 @@ impl SymbolIndex { SymbolIndex::new(symbols) } + #[cfg(feature = "wasm")] + pub(crate) fn for_files( + files: impl Iterator)>, + ) -> SymbolIndex { + let symbols = files + .flat_map(|(file_id, file)| source_file_to_file_symbols(&file.tree(), file_id)) + .collect::>(); + SymbolIndex::new(symbols) + } + fn range_to_map_value(start: usize, end: usize) -> u64 { debug_assert![start <= (std::u32::MAX as usize)]; debug_assert![end <= (std::u32::MAX as usize)]; diff --git a/crates/ra_ide_api/src/wasm_shims.rs b/crates/ra_ide_api/src/wasm_shims.rs new file mode 100644 index 000000000..592dddf44 --- /dev/null +++ b/crates/ra_ide_api/src/wasm_shims.rs @@ -0,0 +1,17 @@ +#[cfg(not(feature = "wasm"))] +pub use std::time::Instant; + +#[cfg(feature = "wasm")] +#[derive(Clone, Copy, Debug)] +pub struct Instant; + +#[cfg(feature = "wasm")] +impl Instant { + pub fn now() -> Self { + Self + } + + pub fn elapsed(&self) -> std::time::Duration { + std::time::Duration::new(0, 0) + } +} -- cgit v1.2.3