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

[WIP] WASM demo #1746

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
169 changes: 169 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = [ "crates/*", "website/website-gen" ]
members = [ "crates/*", "website/website-gen", "website/src/wasm-demo" ]

[profile.release]
incremental = true
Expand Down
3 changes: 3 additions & 0 deletions crates/ra_ide_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ name = "ra_ide_api"
version = "0.1.0"
authors = ["rust-analyzer developers"]

[features]
wasm = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this is a feature? You should be able to just check for wasm via a normal cfg based on the target?!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1746 (comment)

Not sure which is better, but, with a feature, we can check that wasm build actually builds without needing to compile to wasm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, looks like you already had a discussion about this further up.


[dependencies]
format-buf = "1.0.0"
itertools = "0.8.0"
Expand Down
20 changes: 17 additions & 3 deletions crates/ra_ide_api/src/change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -134,7 +135,12 @@ impl LibraryData {
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)
}));
Expand Down Expand Up @@ -223,8 +229,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);
Expand All @@ -234,8 +244,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();

Expand Down
10 changes: 5 additions & 5 deletions crates/ra_ide_api/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{sync::Arc, time};
use std::sync::Arc;

use ra_db::{
salsa::{self, Database, Durability},
Expand All @@ -23,8 +23,8 @@ use crate::{
pub(crate) struct RootDatabase {
runtime: salsa::Runtime<RootDatabase>,
pub(crate) feature_flags: Arc<FeatureFlags>,
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 salsa::Database for RootDatabase {
Expand Down Expand Up @@ -55,8 +55,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),
};
db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
Expand Down
1 change: 1 addition & 0 deletions crates/ra_ide_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod typing;
mod matching_brace;
mod display;
mod inlay_hints;
mod wasm_shims;

#[cfg(test)]
mod marks;
Expand Down
37 changes: 33 additions & 4 deletions crates/ra_ide_api/src/symbol_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -79,10 +80,17 @@ pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol>

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() {
Expand All @@ -91,7 +99,14 @@ pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol>
}

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)
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -169,6 +187,7 @@ impl SymbolIndex {
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 {
Expand All @@ -178,6 +197,16 @@ impl SymbolIndex {
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)];
Expand Down
17 changes: 17 additions & 0 deletions crates/ra_ide_api/src/wasm_shims.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
File renamed without changes.
19 changes: 19 additions & 0 deletions website/src/wasm-demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "wasm_demo"
version = "0.1.0"
authors = ["rust-analyzer developers"]
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
console_error_panic_hook = "0.1.6"
console_log = "0.1"
log = "0.4"
wasm-bindgen = "0.2.50"
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.1"

ra_ide_api = { path = "../../../crates/ra_ide_api", features = ["wasm"] }
ra_syntax = { path = "../../../crates/ra_syntax" }
13 changes: 0 additions & 13 deletions website/src/wasm-demo/index.html

This file was deleted.

Loading