refactor: rm usage like Arc<LoroValue>

because LoroValue itself is cheap to clone now
This commit is contained in:
Zixuan Chen 2023-07-10 18:28:44 +08:00
parent a90668abca
commit de9a8cb9ad
3 changed files with 9 additions and 9 deletions

View file

@ -1,4 +1,4 @@
use std::{hash::Hash, sync::Arc}; use std::hash::Hash;
use fxhash::FxHashMap; use fxhash::FxHashMap;
use serde::{ser::SerializeStruct, Serialize}; use serde::{ser::SerializeStruct, Serialize};
@ -34,7 +34,7 @@ impl MapDelta {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MapValue { pub struct MapValue {
pub counter: Counter, pub counter: Counter,
pub value: Option<Arc<LoroValue>>, pub value: Option<LoroValue>,
pub lamport: (Lamport, PeerID), pub lamport: (Lamport, PeerID),
} }
@ -84,14 +84,14 @@ impl Serialize for MapValue {
S: serde::Serializer, S: serde::Serializer,
{ {
let mut s = serializer.serialize_struct("MapValue", 2)?; let mut s = serializer.serialize_struct("MapValue", 2)?;
s.serialize_field("value", &self.value.as_deref())?; s.serialize_field("value", &self.value)?;
s.serialize_field("lamport", &self.lamport)?; s.serialize_field("lamport", &self.lamport)?;
s.end() s.end()
} }
} }
impl MapValue { impl MapValue {
pub fn new(id: ID, lamport: Lamport, value: Option<Arc<LoroValue>>) -> Self { pub fn new(id: ID, lamport: Lamport, value: Option<LoroValue>) -> Self {
MapValue { MapValue {
counter: id.counter, counter: id.counter,
value, value,

View file

@ -29,7 +29,7 @@ pub(super) struct SharedArena {
parents: Arc<Mutex<FxHashMap<ContainerIdx, Option<ContainerIdx>>>>, parents: Arc<Mutex<FxHashMap<ContainerIdx, Option<ContainerIdx>>>>,
text: Arc<Mutex<AppendOnlyBytes>>, text: Arc<Mutex<AppendOnlyBytes>>,
text_utf16_len: Arc<AtomicUsize>, text_utf16_len: Arc<AtomicUsize>,
values: Arc<Mutex<Vec<Arc<LoroValue>>>>, values: Arc<Mutex<Vec<LoroValue>>>,
} }
pub(crate) struct StrAllocResult { pub(crate) struct StrAllocResult {
@ -89,7 +89,7 @@ impl SharedArena {
pub fn alloc_value(&self, value: LoroValue) -> usize { pub fn alloc_value(&self, value: LoroValue) -> usize {
let mut values_lock = self.values.lock().unwrap(); let mut values_lock = self.values.lock().unwrap();
values_lock.push(Arc::new(value)); values_lock.push(value);
values_lock.len() - 1 values_lock.len() - 1
} }
@ -97,7 +97,7 @@ impl SharedArena {
let mut values_lock = self.values.lock().unwrap(); let mut values_lock = self.values.lock().unwrap();
let start = values_lock.len(); let start = values_lock.len();
for value in values { for value in values {
values_lock.push(Arc::new(value)); values_lock.push(value);
} }
start..values_lock.len() start..values_lock.len()
@ -115,7 +115,7 @@ impl SharedArena {
self.text.lock().unwrap().slice(range) self.text.lock().unwrap().slice(range)
} }
pub fn get_value(&self, idx: usize) -> Option<Arc<LoroValue>> { pub fn get_value(&self, idx: usize) -> Option<LoroValue> {
self.values.lock().unwrap().get(idx).cloned() self.values.lock().unwrap().get(idx).cloned()
} }

View file

@ -56,7 +56,7 @@ impl ContainerState for MapState {
MapValue { MapValue {
lamport: (op.lamport, op.id.peer), lamport: (op.lamport, op.id.peer),
counter: op.id.counter, counter: op.id.counter,
value: Some(Arc::new(map.value)), value: Some(map.value),
}, },
), ),
RawOpContent::List(_) => unreachable!(), RawOpContent::List(_) => unreachable!(),