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

Run puffin and puffin_egui on wasm32 on web #112

Merged
merged 6 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- next-header -->
## [Unreleased] - ReleaseDate

- [PR#112](https://github.com/EmbarkStudios/puffin/pull/112) You can now compile and run `puffin` on the web if you enable the "web" feature.

## [0.14.0] - 2022-11-07

### Fixed
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 16 additions & 6 deletions puffin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ include = ["**/*.rs", "Cargo.toml", "README.md"]
[package.metadata.docs.rs]
all-features = true


[features]
default = []
packing = ["anyhow", "bincode", "parking_lot", "serde", "zstd", "ruzstd"]

# Feature for enabling loading/saving data to a binary stream and/or file.
serialization = ["packing"]

# Enable this to be able to run puffin inside a browser when compiling to wasm
web = ["instant/wasm-bindgen", "dep:js-sys"]


[dependencies]
byteorder = { version = "1.0" }
instant = { version = "0.1" }
once_cell = "1.0"

# Optional:
Expand All @@ -28,18 +41,15 @@ serde = { version = "1.0", features = ["derive", "rc"], optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
zstd = { version = "0.11.1", optional = true } # native only

# web:
[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = { version = "0.3", optional = true }
ruzstd = { version = "0.3.0", optional = true } # works on wasm


[dev-dependencies]
criterion = "0.4"

[features]
default = []
packing = ["anyhow", "bincode", "parking_lot", "serde", "zstd", "ruzstd"]
# Feature for enabling loading/saving data to a binary stream and/or file.
serialization = ["packing"]

[[bench]]
name = "benchmark"
harness = false
30 changes: 21 additions & 9 deletions puffin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,21 +536,33 @@ impl GlobalProfiler {
/// Returns a high-precision, monotonically increasing nanosecond count since unix epoch.
#[inline]
pub fn now_ns() -> NanoSecond {
// This can maybe be optimized
use once_cell::sync::Lazy;
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
fn nanos_since_epoch() -> NanoSecond {
#[cfg(feature = "web")]
{
(js_sys::Date::new_0().get_time() * 1e6) as _
}
#[cfg(not(feature = "web"))]
{
0 // We won't get correct date-times, but that's fine.
}
}

fn epoch_offset_and_start() -> (NanoSecond, Instant) {
#[cfg(not(target_arch = "wasm32"))]
fn nanos_since_epoch() -> NanoSecond {
if let Ok(duration_since_epoch) = std::time::UNIX_EPOCH.elapsed() {
let nanos_since_epoch = duration_since_epoch.as_nanos() as NanoSecond;
(nanos_since_epoch, Instant::now())
duration_since_epoch.as_nanos() as NanoSecond
} else {
// system time is set before 1970. this should be quite rare.
(0, Instant::now())
0 // system time is set before 1970. this should be quite rare.
}
}

static START_TIME: Lazy<(NanoSecond, Instant)> = Lazy::new(epoch_offset_and_start);
// This can maybe be optimized
use instant::Instant;
use once_cell::sync::Lazy;

static START_TIME: Lazy<(NanoSecond, Instant)> =
Lazy::new(|| (nanos_since_epoch(), Instant::now()));
START_TIME.0 + START_TIME.1.elapsed().as_nanos() as NanoSecond
}

Expand Down
2 changes: 2 additions & 0 deletions puffin_egui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ All notable changes to the egui crate will be documented in this file.
<!-- next-header -->
## [Unreleased] - ReleaseDate

- [PR#112](https://github.com/EmbarkStudios/puffin/pull/112) You can now compile and run `puffin_egui` on the web

## [0.18.0] - 2022-11-08

- Require `puffin` 0.14.0
Expand Down
3 changes: 2 additions & 1 deletion puffin_egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ include = [
[dependencies]
chrono = "0.4"
egui = "0.19.0"
indexmap = { version = "1.9.1", features = ["serde"] }
instant = "0.1"
natord = "1.0.9"
once_cell = "1.7"
puffin = { version = "0.14.0", path = "../puffin", features = ["packing"] }
serde = { version = "1.0", features = ["derive"], optional = true }
vec1 = "1.8"
indexmap = { version = "1.9.1", features = ["serde"] }

[dev-dependencies]
eframe = "0.19.0"
Expand Down
8 changes: 4 additions & 4 deletions puffin_egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ pub struct ProfilerUi {

/// When did we last run a pass to pack all the frames?
#[cfg_attr(feature = "serde", serde(skip))]
last_pack_pass: Option<std::time::Instant>,
last_pack_pass: Option<instant::Instant>,
}

impl Default for ProfilerUi {
Expand Down Expand Up @@ -428,16 +428,16 @@ impl ProfilerUi {
}
let last_pack_pass = self
.last_pack_pass
.get_or_insert_with(std::time::Instant::now);
.get_or_insert_with(instant::Instant::now);
let time_since_last_pack = last_pack_pass.elapsed();
if time_since_last_pack > std::time::Duration::from_secs(1) {
if time_since_last_pack > instant::Duration::from_secs(1) {
puffin::profile_scope!("pack_pass");
for frame in self.all_known_frames(frame_view) {
if !self.is_selected(frame_view, frame.frame_index()) {
frame.pack();
}
}
self.last_pack_pass = Some(std::time::Instant::now());
self.last_pack_pass = Some(instant::Instant::now());
}
}

Expand Down