From 2c9f1d9be6cfb5c5a21a22dfa68a6faa38681ca4 Mon Sep 17 00:00:00 2001 From: Maximilian Ammann Date: Thu, 8 Sep 2022 11:16:55 +0200 Subject: [PATCH 01/52] Experiment with single threaded executor --- maplibre/src/stages/request_stage.rs | 9 ++++++--- web/Cargo.toml | 1 + web/src/platform/http_client.rs | 2 ++ web/src/platform/schedule_method.rs | 19 +++++++++++++------ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/maplibre/src/stages/request_stage.rs b/maplibre/src/stages/request_stage.rs index 1a67cb64..3fc11b51 100644 --- a/maplibre/src/stages/request_stage.rs +++ b/maplibre/src/stages/request_stage.rs @@ -142,9 +142,12 @@ where .schedule(Box::new(move || { Box::pin(async move { match client.fetch(&coords).await { - Ok(data) => state - .process_tile(request_id, data.into_boxed_slice()) - .unwrap(), + Ok(data) => { + log::warn!("fetching done"); + state + .process_tile(request_id, data.into_boxed_slice()) + .unwrap() + } Err(e) => { log::error!("{:?}", &e); state.tile_unavailable(&coords, request_id).unwrap() diff --git a/web/Cargo.toml b/web/Cargo.toml index ca492a73..a6bf395e 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -38,6 +38,7 @@ wasm-bindgen = "0.2.81" wasm-bindgen-futures = "0.4.31" console_log = { version = "0.2.0", features = ["color"] } tracing-wasm = { version = "0.2.1", optional = true } # FIXME: Low quality dependency +futures = "0.3.24" [dev-dependencies] wasm-bindgen-test = "0.3.31" diff --git a/web/src/platform/http_client.rs b/web/src/platform/http_client.rs index b390ed0d..474a719b 100644 --- a/web/src/platform/http_client.rs +++ b/web/src/platform/http_client.rs @@ -26,7 +26,9 @@ impl WHATWGFetchHttpClient { let scope = global.dyn_into::().unwrap(); // Call fetch on global scope + log::warn!("starting fetching"); let maybe_response = JsFuture::from(scope.fetch_with_request(&request)).await?; + log::warn!("fetching done 1"); assert!(maybe_response.is_instance_of::()); let response: Response = maybe_response.dyn_into().unwrap(); diff --git a/web/src/platform/schedule_method.rs b/web/src/platform/schedule_method.rs index 3d3c1d32..a8e18233 100644 --- a/web/src/platform/schedule_method.rs +++ b/web/src/platform/schedule_method.rs @@ -1,11 +1,13 @@ +use futures::executor::LocalPool; +use futures::task::{LocalSpawnExt, SpawnExt}; +use log::warn; use std::future::Future; +use super::pool::WorkerPool; use maplibre::{error::Error, io::scheduler::ScheduleMethod}; use wasm_bindgen::{prelude::*, JsCast}; use web_sys::Worker; -use super::pool::WorkerPool; - pub struct WebWorkerPoolScheduleMethod { pool: WorkerPool, } @@ -38,10 +40,15 @@ impl ScheduleMethod for WebWorkerPoolScheduleMethod { { self.pool .execute(move || { - wasm_bindgen_futures::future_to_promise(async move { - future_factory().await; - Ok(JsValue::undefined()) - }) + pool.spawner() + .spawn_local(async move { + future_factory().await; + }) + .unwrap(); + + warn!("Running tasks"); + pool.run_until_stalled(); + warn!("All tasks done"); }) .unwrap(); Ok(()) From 2b7616918d99c07fab9392cda0103bd5d0d3befa Mon Sep 17 00:00:00 2001 From: Maximilian Ammann Date: Thu, 8 Sep 2022 14:17:18 +0200 Subject: [PATCH 02/52] Avoid exiting process --- web/lib/build.mjs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/web/lib/build.mjs b/web/lib/build.mjs index ccdaf6f0..bc6eb5cc 100644 --- a/web/lib/build.mjs +++ b/web/lib/build.mjs @@ -107,7 +107,6 @@ const emitTypeScript = () => { if (child.status !== 0) { console.error("Failed to execute tsc") - process.exit(1) } } @@ -135,7 +134,6 @@ const wasmPack = () => { // Having package.json within another npm package is not supported. Remove that. unlink(`${getLibDirectory()}/src/wasm-pack/package.json`, (err) => { - if (err) throw err; }) } @@ -149,17 +147,22 @@ const watchResult = async (result) => { }); const update = async (path) => { - console.log(`Updating: ${path}`) - if (path.endsWith(".rs")) { - console.log("Rebuilding Rust...") - wasmPack(); + try { + console.log(`Updating: ${path}`) + if (path.endsWith(".rs")) { + console.log("Rebuilding Rust...") + wasmPack(); + } + + console.log("Rebuilding...") + await result.rebuild(); + + console.log("Emitting TypeScript types...") + emitTypeScript(); + } catch (e) { + console.error("Error while updating:") + console.error(e) } - - console.log("Rebuilding...") - await result.rebuild(); - - console.log("Emitting TypeScript types...") - emitTypeScript(); } console.log("Watching...") From 5b5be8ab4037c921bee92096791c422fe473f3fb Mon Sep 17 00:00:00 2001 From: Maximilian Ammann Date: Thu, 8 Sep 2022 16:18:07 +0200 Subject: [PATCH 03/52] Use custom profile and remove wasm-pack --- .cargo/config.toml | 15 + .github/workflows/library-web.yml | 4 +- .idea/runConfigurations/Build_WASM.xml | 2 +- Cargo.toml | 1 - justfile | 8 +- web/Cargo.toml | 9 +- web/demo/package-lock.json | 4 +- web/lib/.gitignore | 4 +- web/lib/build.mjs | 64 ++- web/lib/package-lock.json | 454 +----------------- web/lib/package.json | 4 +- web/lib/src/index.ts | 39 +- web/lib/src/legacy.worker.ts | 32 -- web/lib/src/pool.worker.ts | 4 +- web/lib/src/{types.ts => worker-types.ts} | 0 web/src/lib.rs | 2 +- web/src/platform/http_client.rs | 2 - web/src/platform/legacy_webworker_fetcher.rs | 52 -- web/src/platform/mod.rs | 2 +- web/src/platform/pool.rs | 26 +- ...dule_method.rs => pool_schedule_method.rs} | 17 +- 21 files changed, 123 insertions(+), 622 deletions(-) delete mode 100644 web/lib/src/legacy.worker.ts rename web/lib/src/{types.ts => worker-types.ts} (100%) delete mode 100644 web/src/platform/legacy_webworker_fetcher.rs rename web/src/platform/{schedule_method.rs => pool_schedule_method.rs} (72%) diff --git a/.cargo/config.toml b/.cargo/config.toml index 54e293df..a2471dc3 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -9,3 +9,18 @@ rustflags = [ "-C", "link-args=--shared-memory --import-memory", ] runner = 'wasm-bindgen-test-runner' + + +[profile.wasm-dev] +inherits = "dev" +opt-level = 's' +debug = true +debug-assertions = true +overflow-checks = true +panic = 'abort' + +[profile.wasm-release] +inherits = "release" +opt-level = 's' +lto = true +panic = 'abort' diff --git a/.github/workflows/library-web.yml b/.github/workflows/library-web.yml index 390aba83..8a2bb744 100644 --- a/.github/workflows/library-web.yml +++ b/.github/workflows/library-web.yml @@ -25,7 +25,7 @@ jobs: - uses: Swatinem/rust-cache@v1 - name: Build lib shell: bash - run: just web-lib build-webgl + run: just web-lib build-webgl --release - name: Build demo shell: bash run: just web-demo build @@ -55,7 +55,7 @@ jobs: - uses: Swatinem/rust-cache@v1 - name: Build lib shell: bash - run: just web-lib build + run: just web-lib build --release - name: Build demo shell: bash run: just web-demo build diff --git a/.idea/runConfigurations/Build_WASM.xml b/.idea/runConfigurations/Build_WASM.xml index 38cb5805..61f1b40b 100644 --- a/.idea/runConfigurations/Build_WASM.xml +++ b/.idea/runConfigurations/Build_WASM.xml @@ -1,6 +1,6 @@ -