Skip to content

Commit

Permalink
replace BTreeMap with vec
Browse files Browse the repository at this point in the history
  • Loading branch information
afinch7 committed Aug 23, 2019
1 parent a21bf51 commit a56c5c8
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions core/op_dispatchers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::libdeno::OpId;
use crate::libdeno::PinnedBuf;
use futures::future::Future;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -60,7 +59,7 @@ pub struct OpDisReg {
// Quick lookups by unique "op id"/"resource id"
// The main goal of op_dis_registry is to perform lookups as fast
// as possible at all times.
op_dis_registry: Mutex<BTreeMap<OpId, Arc<Box<dyn OpDispatcher>>>>,
op_dis_registry: Mutex<Vec<Option<Arc<Box<dyn OpDispatcher>>>>>,
next_op_dis_id: AtomicU32,
// Serves as "phone book" for op_dis_registry
// This should only be referenced for initial lookups. It isn't
Expand All @@ -73,12 +72,19 @@ pub struct OpDisReg {
impl OpDisReg {
pub fn new() -> Self {
Self {
op_dis_registry: Mutex::new(BTreeMap::new()),
op_dis_registry: Mutex::new(Vec::new()),
next_op_dis_id: AtomicU32::new(0),
op_dis_id_registry: Mutex::new(HashMap::new()),
}
}

fn add_op_dis<D: Named + OpDispatcher + 'static>(&self, op_id: OpId, d: D) {
let mut holder = self.op_dis_registry.lock().unwrap();
let new_len = holder.len().max(op_id as usize) + 1;
holder.resize(new_len, None);
holder.insert(op_id as usize, Some(Arc::new(Box::new(d))));
}

pub fn register_op<D: Named + OpDispatcher + 'static>(
&self,
namespace: &str,
Expand All @@ -98,13 +104,7 @@ impl OpDisReg {
.or_insert(op_id);
// If we can successfully add the rid to the "phone book" then add this
// op to the primary registry.
self
.op_dis_registry
.lock()
.unwrap()
.entry(op_id)
.and_modify(|_| unreachable!("Op id already registered"))
.or_insert(Arc::new(Box::new(d)));
self.add_op_dis(op_id, d);
(op_id, namespace_string, D::NAME.to_string())
}

Expand All @@ -115,8 +115,8 @@ impl OpDisReg {
buf: Option<PinnedBuf>,
) -> CoreOp {
let lock = self.op_dis_registry.lock().unwrap();
if let Some(op) = lock.get(&op_id) {
let op_ = Arc::clone(op);
if let Some(op) = &lock[op_id as usize] {
let op_ = Arc::clone(&op);
drop(lock);
op_.dispatch(args, buf)
} else {
Expand All @@ -132,6 +132,9 @@ impl OpDisReg {
}
}

unsafe impl Send for OpDisReg {}
unsafe impl Sync for OpDisReg {}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit a56c5c8

Please sign in to comment.