summaryrefslogtreecommitdiff
path: root/core/ops_metrics.rs
diff options
context:
space:
mode:
authorNugine <nugine@foxmail.com>2022-07-01 06:43:25 +0800
committerGitHub <noreply@github.com>2022-07-01 00:43:25 +0200
commita27acbc2ec63dd684cf57990b30d757cd0477d9b (patch)
tree1fa71c31cfebba14a9a7d74eca18dada3b57686b /core/ops_metrics.rs
parent3d8ba30ea0a4dfedcddcf11ecf2bf476cd1af4c3 (diff)
fix(core): remove unsafe in OpsTracker (#15025)
Diffstat (limited to 'core/ops_metrics.rs')
-rw-r--r--core/ops_metrics.rs37
1 files changed, 16 insertions, 21 deletions
diff --git a/core/ops_metrics.rs b/core/ops_metrics.rs
index aa3ff503b..8c6ef3a7c 100644
--- a/core/ops_metrics.rs
+++ b/core/ops_metrics.rs
@@ -1,7 +1,8 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+
use crate::serde::Serialize;
use crate::OpId;
-use std::cell::UnsafeCell;
+use std::cell::{RefCell, RefMut};
// TODO(@AaronO): split into AggregateMetrics & PerOpMetrics
#[derive(Clone, Default, Debug, Serialize)]
@@ -25,18 +26,24 @@ pub struct OpMetrics {
// TODO(@AaronO): track errors
#[derive(Default, Debug)]
pub struct OpsTracker {
- pub ops: UnsafeCell<Vec<OpMetrics>>,
+ ops: RefCell<Vec<OpMetrics>>,
}
impl OpsTracker {
+ pub fn new(ops_count: usize) -> Self {
+ Self {
+ ops: RefCell::new(vec![Default::default(); ops_count]),
+ }
+ }
+
pub fn per_op(&self) -> Vec<OpMetrics> {
- self.ops_mut().clone()
+ self.ops.borrow().clone()
}
pub fn aggregate(&self) -> OpMetrics {
let mut sum = OpMetrics::default();
- for metrics in self.ops_mut().iter() {
+ for metrics in self.ops.borrow().iter() {
sum.ops_dispatched += metrics.ops_dispatched;
sum.ops_dispatched_sync += metrics.ops_dispatched_sync;
sum.ops_dispatched_async += metrics.ops_dispatched_async;
@@ -53,26 +60,14 @@ impl OpsTracker {
sum
}
- #[allow(clippy::mut_from_ref)]
- #[inline]
- fn ops_mut(&self) -> &mut Vec<OpMetrics> {
- // SAFETY: `OpsTracker` is created after registering ops so it is guaranteed
- // that that `ops` will be initialized.
- unsafe { &mut *self.ops.get() }
- }
-
- #[allow(clippy::mut_from_ref)]
#[inline]
- fn metrics_mut(&self, id: OpId) -> &mut OpMetrics {
- // SAFETY: `OpsTracker` is created after registering ops, and ops
- // cannot be unregistered during runtime, so it is guaranteed that `id`
- // is not causing out-of-bound access.
- unsafe { self.ops_mut().get_unchecked_mut(id) }
+ fn metrics_mut(&self, id: OpId) -> RefMut<OpMetrics> {
+ RefMut::map(self.ops.borrow_mut(), |ops| &mut ops[id])
}
#[inline]
pub fn track_sync(&self, id: OpId) {
- let metrics = self.metrics_mut(id);
+ let mut metrics = self.metrics_mut(id);
metrics.ops_dispatched += 1;
metrics.ops_completed += 1;
metrics.ops_dispatched_sync += 1;
@@ -81,14 +76,14 @@ impl OpsTracker {
#[inline]
pub fn track_async(&self, id: OpId) {
- let metrics = self.metrics_mut(id);
+ let mut metrics = self.metrics_mut(id);
metrics.ops_dispatched += 1;
metrics.ops_dispatched_async += 1;
}
#[inline]
pub fn track_async_completed(&self, id: OpId) {
- let metrics = self.metrics_mut(id);
+ let mut metrics = self.metrics_mut(id);
metrics.ops_completed += 1;
metrics.ops_completed_async += 1;
}