allow compiling ra_ide_api on wasm

This commit is contained in:
Aleksey Kladov 2019-09-20 20:38:16 +03:00
parent c733993658
commit 4d81432213
6 changed files with 76 additions and 12 deletions

View File

@ -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"

View File

@ -6,6 +6,7 @@
};
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 @@ pub fn prepare(
root_id: SourceRootId,
files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
) -> 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 @@ fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange)
}
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 @@ pub(crate) fn maybe_collect_garbage(&mut self) {
}
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();

View File

@ -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<RootDatabase>,
pub(crate) feature_flags: Arc<FeatureFlags>,
pub(crate) debug_data: Arc<DebugData>,
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<usize>, 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(),
};

View File

@ -40,6 +40,7 @@
mod matching_brace;
mod display;
mod inlay_hints;
mod wasm_shims;
#[cfg(test)]
mod marks;

View File

@ -38,6 +38,7 @@
SyntaxKind::{self, *},
SyntaxNode, SyntaxNodePtr, TextRange, WalkEvent,
};
#[cfg(not(feature = "wasm"))]
use rayon::prelude::*;
use crate::{db::RootDatabase, FileId, Query};
@ -79,10 +80,17 @@ fn clone(&self) -> Snap {
let buf: Vec<Arc<SymbolIndex>> = 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 @@ fn clone(&self) -> Snap {
}
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 @@ fn new(mut symbols: Vec<FileSymbol>) -> 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 @@ pub(crate) fn memory_size(&self) -> usize {
self.map.as_fst().size() + self.symbols.len() * mem::size_of::<FileSymbol>()
}
#[cfg(not(feature = "wasm"))]
pub(crate) fn for_files(
files: impl ParallelIterator<Item = (FileId, Parse<ast::SourceFile>)>,
) -> SymbolIndex {
@ -178,6 +197,16 @@ pub(crate) fn for_files(
SymbolIndex::new(symbols)
}
#[cfg(feature = "wasm")]
pub(crate) fn for_files(
files: impl Iterator<Item = (FileId, Parse<ast::SourceFile>)>,
) -> SymbolIndex {
let symbols = files
.flat_map(|(file_id, file)| source_file_to_file_symbols(&file.tree(), file_id))
.collect::<Vec<_>>();
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)];

View File

@ -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)
}
}