Skip to content

Commit 73cc3d1

Browse files
committed
Reduce overhead of JavaScript calls
In the web our layout state calculation is actually massively dominated by the calls to `performance.now()`. However before we can even call `.now()` on the `performance` object, we first need to even get that. So you first have to get the `window` object and access its `performance` field. So overall that's three calls into JavaScript. If we however cache the `performance` object, we can reduce it to just a single call. That seems to improve the layout state calculation performance by over 2x. Optimally we would only take a single time stamp per layout state calculation, as otherwise there might be slight disagreements between the individual values shown in the layout state. That's however a much bigger refactoring, so that's something we can look into in the future.
1 parent ea17c6a commit 73cc3d1

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/platform/wasm/web/time.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ use web_sys::window;
66
#[derive(Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Debug)]
77
pub struct Instant(OrderedFloat<f64>);
88

9+
thread_local! {
10+
static PERFORMANCE: web_sys::Performance = window()
11+
.expect("No window object available")
12+
.performance()
13+
.expect("Can't measure time without a performance object");
14+
}
15+
916
impl Instant {
1017
pub fn now() -> Self {
11-
let seconds = window()
12-
.and_then(|w| w.performance())
13-
.expect("Can't measure time without a performance object")
14-
.now()
15-
/ 1000.0;
18+
let seconds = PERFORMANCE.with(|p| p.now()) / 1000.0;
1619
Instant(OrderedFloat(seconds))
1720
}
1821
}

0 commit comments

Comments
 (0)