summaryrefslogtreecommitdiff
path: root/ext/web/timers.rs
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-08-28 12:21:49 +0530
committerGitHub <noreply@github.com>2022-08-28 12:21:49 +0530
commitd8396225c4287c53dd42226266e9f66983125e51 (patch)
tree27516437c40b80596f4d0fc9b821882a0f2a0ca1 /ext/web/timers.rs
parent7c4f57e8b091bc386242f83b5373e4bb33382012 (diff)
perf: use fast api for op_now (#15643)
Diffstat (limited to 'ext/web/timers.rs')
-rw-r--r--ext/web/timers.rs32
1 files changed, 27 insertions, 5 deletions
diff --git a/ext/web/timers.rs b/ext/web/timers.rs
index 0f781a579..d9ceef875 100644
--- a/ext/web/timers.rs
+++ b/ext/web/timers.rs
@@ -4,6 +4,7 @@
use deno_core::error::AnyError;
use deno_core::op;
+use deno_core::ZeroCopyBuf;
use deno_core::CancelFuture;
use deno_core::CancelHandle;
@@ -23,29 +24,50 @@ pub trait TimersPermission {
pub type StartTime = Instant;
+static mut NOW_BUF: *mut u32 = std::ptr::null_mut();
+
+#[op]
+pub fn op_now_set_buf(buf: ZeroCopyBuf) {
+ assert_eq!(buf.len(), 8);
+ // SAFETY: This is safe because this is the only place where we initialize
+ // NOW_BUF.
+ unsafe {
+ NOW_BUF = buf.as_ptr() as *mut u32;
+ }
+}
+
// Returns a milliseconds and nanoseconds subsec
// since the start time of the deno runtime.
// If the High precision flag is not set, the
// nanoseconds are rounded on 2ms.
-#[op]
-pub fn op_now<TP>(state: &mut OpState) -> f64
+#[op(fast)]
+pub fn op_now<TP>(state: &mut OpState)
where
TP: TimersPermission + 'static,
{
let start_time = state.borrow::<StartTime>();
let elapsed = start_time.elapsed();
let seconds = elapsed.as_secs();
- let mut subsec_nanos = elapsed.subsec_nanos() as f64;
- let reduced_time_precision = 2_000_000.0; // 2ms in nanoseconds
+ let mut subsec_nanos = elapsed.subsec_nanos();
// If the permission is not enabled
// Round the nano result on 2 milliseconds
// see: https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp#Reduced_time_precision
if !state.borrow_mut::<TP>().allow_hrtime() {
+ let reduced_time_precision = 2_000_000; // 2ms in nanoseconds
subsec_nanos -= subsec_nanos % reduced_time_precision;
}
- (seconds * 1_000) as f64 + (subsec_nanos / 1_000_000.0)
+ // SAFETY: This is safe because we initialize NOW_BUF in op_now_set_buf, its a null pointer
+ // otherwise.
+ // op_now_set_buf guarantees that the buffer is 8 bytes long.
+ unsafe {
+ if !NOW_BUF.is_null() {
+ let buf = std::slice::from_raw_parts_mut(NOW_BUF, 2);
+ buf[0] = seconds as u32;
+ buf[1] = subsec_nanos as u32;
+ }
+ }
}
pub struct TimerHandle(Rc<CancelHandle>);