summaryrefslogtreecommitdiff
path: root/cli/ops/timers.rs
diff options
context:
space:
mode:
authorSteven Guerrero <stephenguerrero43@gmail.com>2020-12-07 08:27:25 -0500
committerGitHub <noreply@github.com>2020-12-07 08:27:25 -0500
commit43a35b005f9f4631dd97a9db0f41ad76eaed941e (patch)
tree50f65c3c5a2260ec3455c79c4ea8185ffad67c07 /cli/ops/timers.rs
parentb77d6cb29e4437f4783368aaa3b1d5c972470ad0 (diff)
perf: use minimal op with performance.now() (#8619)
Diffstat (limited to 'cli/ops/timers.rs')
-rw-r--r--cli/ops/timers.rs42
1 files changed, 29 insertions, 13 deletions
diff --git a/cli/ops/timers.rs b/cli/ops/timers.rs
index 841cdf289..8037fd698 100644
--- a/cli/ops/timers.rs
+++ b/cli/ops/timers.rs
@@ -8,7 +8,11 @@
//! only need to be able to start, cancel and await a single timer (or Delay, as Tokio
//! calls it) for an entire Isolate. This is what is implemented here.
+use super::dispatch_minimal::minimal_op;
+use super::dispatch_minimal::MinimalOp;
+use crate::metrics::metrics_op;
use crate::permissions::Permissions;
+use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::futures;
use deno_core::futures::channel::oneshot;
@@ -77,7 +81,7 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
super::reg_json_sync(rt, "op_global_timer_stop", op_global_timer_stop);
super::reg_json_sync(rt, "op_global_timer_start", op_global_timer_start);
super::reg_json_async(rt, "op_global_timer", op_global_timer);
- super::reg_json_sync(rt, "op_now", op_now);
+ rt.register_op("op_now", metrics_op(minimal_op(op_now)));
super::reg_json_sync(rt, "op_sleep_sync", op_sleep_sync);
}
@@ -138,26 +142,38 @@ async fn op_global_timer(
// If the High precision flag is not set, the
// nanoseconds are rounded on 2ms.
fn op_now(
- state: &mut OpState,
- _args: Value,
- _zero_copy: &mut [ZeroCopyBuf],
-) -> Result<Value, AnyError> {
- let start_time = state.borrow::<StartTime>();
+ state: Rc<RefCell<OpState>>,
+ // Arguments are discarded
+ _sync: bool,
+ _x: i32,
+ mut zero_copy: BufVec,
+) -> MinimalOp {
+ match zero_copy.len() {
+ 0 => return MinimalOp::Sync(Err(type_error("no buffer specified"))),
+ 1 => {}
+ _ => {
+ return MinimalOp::Sync(Err(type_error("Invalid number of arguments")))
+ }
+ }
+
+ let op_state = state.borrow();
+ let start_time = op_state.borrow::<StartTime>();
let seconds = start_time.elapsed().as_secs();
- let mut subsec_nanos = start_time.elapsed().subsec_nanos();
- let reduced_time_precision = 2_000_000; // 2ms in nanoseconds
+ let mut subsec_nanos = start_time.elapsed().subsec_nanos() as f64;
+ let reduced_time_precision = 2_000_000.0; // 2ms in nanoseconds
// 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::<Permissions>().check_hrtime().is_err() {
+ if op_state.borrow::<Permissions>().check_hrtime().is_err() {
subsec_nanos -= subsec_nanos % reduced_time_precision;
}
- Ok(json!({
- "seconds": seconds,
- "subsecNanos": subsec_nanos,
- }))
+ let result = (seconds * 1_000) as f64 + (subsec_nanos / 1_000_000.0);
+
+ (&mut zero_copy[0]).copy_from_slice(&result.to_be_bytes());
+
+ MinimalOp::Sync(Ok(0))
}
#[derive(Deserialize)]