Skip to content

Commit

Permalink
use web-time in case of wasm (#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma-andex authored Dec 12, 2023
1 parent 610d640 commit 70eda6b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions burn-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ wasm-sync = []
[target.'cfg(target_family = "wasm")'.dependencies]
async-trait = { workspace = true }
getrandom = { workspace = true, features = ["js"] }
web-time = { version = "0.2.3" }

[dependencies]
# ** Please make sure all dependencies support no_std when std is disabled **
Expand Down
4 changes: 3 additions & 1 deletion burn-common/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use alloc::vec::Vec;
use core::fmt::Display;
use core::time::Duration;

#[cfg(feature = "std")]
#[cfg(all(not(target_family = "wasm"), feature = "std"))]
use std::time::Instant;
#[cfg(all(target_family = "wasm", feature = "std"))]
use web_time::Instant;

/// Results of a benchmark run.
#[derive(new, Debug)]
Expand Down
3 changes: 3 additions & 0 deletions burn-compute/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ spin = { workspace = true }
log = { workspace = true }
hashbrown = { workspace = true }

[target.'cfg(target_family = "wasm")'.dependencies]
web-time = { version = "0.2.3" }

[dev-dependencies]
serial_test = "2.0.0"
11 changes: 8 additions & 3 deletions burn-compute/src/memory_management/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use crate::{
use alloc::{sync::Arc, vec::Vec};
use hashbrown::HashMap;

#[cfg(all(not(target_family = "wasm"), feature = "std"))]
use std::time;
#[cfg(all(target_family = "wasm", feature = "std"))]
use web_time as time;

// The ChunkId allows to keep track of how many references there are to a specific chunk.
memory_id_type!(ChunkId);
// The SliceId allows to keep track of how many references there are to a specific slice.
Expand Down Expand Up @@ -48,9 +53,9 @@ pub enum DeallocStrategy {
/// Once every period of time
PeriodTime {
/// Number of time before triggering the deallocation.
period: std::time::Duration,
period: time::Duration,
/// Current state. Should start at now.
state: std::time::Instant,
state: time::Instant,
},
/// Never deallocate.
Never,
Expand Down Expand Up @@ -100,7 +105,7 @@ impl DeallocStrategy {
#[cfg(feature = "std")]
DeallocStrategy::PeriodTime { period, state } => {
if &state.elapsed() > period {
*state = std::time::Instant::now();
*state = time::Instant::now();
true
} else {
false
Expand Down
4 changes: 4 additions & 0 deletions burn-compute/src/tune/tuner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use core::marker::PhantomData;
#[cfg(target_family = "wasm")]
use web_time::Duration;

#[cfg(not(target_family = "wasm"))]
use core::time::Duration;

use alloc::boxed::Box;
Expand Down

0 comments on commit 70eda6b

Please sign in to comment.