Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbabcock committed Dec 19, 2024
1 parent d77dc00 commit 240ccb0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
48 changes: 40 additions & 8 deletions src/network/frame_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use crate::network::{CacheInfo, VersionTriplet};
use crate::parser::ReplayBody;

#[derive(Debug)]
pub(crate) struct SegmentedArray<T> {
pub(crate) struct RawSegmentedArray<T> {
array: Vec<Option<T>>,
map: FnvHashMap<usize, T>,
}

impl<T> SegmentedArray<T> {
impl<T> RawSegmentedArray<T> {
pub(crate) fn new(size: usize) -> Self {
let mut array = Vec::with_capacity(size);
array.resize_with(size, || None);
Expand Down Expand Up @@ -54,6 +54,36 @@ impl<T> SegmentedArray<T> {
}
}

#[derive(Debug)]
pub(crate) struct SegmentedArray<K, V> {
raw: RawSegmentedArray<V>,
marker: std::marker::PhantomData<K>,
}

impl<K, V> SegmentedArray<K, V>
where
K: Into<usize>,
{
pub(crate) fn new(size: usize) -> Self {
Self {
raw: RawSegmentedArray::new(size),
marker: std::marker::PhantomData,
}
}

pub(crate) fn insert(&mut self, key: K, value: V) {
self.raw.insert(key.into(), value);
}

pub(crate) fn get(&self, key: K) -> Option<&V> {
self.raw.get(key.into())
}

pub(crate) fn delete(&mut self, key: K) {
self.raw.delete(key.into());
}
}

pub(crate) struct FrameDecoder<'a, 'b: 'a> {
pub frames_len: usize,
pub product_decoder: ProductValueDecoder,
Expand Down Expand Up @@ -117,7 +147,7 @@ impl<'a, 'b> FrameDecoder<'a, 'b> {
attr_decoder: &AttributeDecoder,
bits: &mut LittleEndianReader<'_>,
buf: &mut [u8],
actors: &mut SegmentedArray<(ObjectId, &'c CacheInfo)>,
actors: &mut SegmentedArray<ActorId, (ObjectId, &'c CacheInfo)>,
new_actors: &mut Vec<NewActor>,
deleted_actors: &mut Vec<ActorId>,
updated_actors: &mut Vec<UpdatedAttribute>,
Expand Down Expand Up @@ -169,20 +199,20 @@ impl<'a, 'b> FrameDecoder<'a, 'b> {
// overwrite it.
let cache_info = self
.object_ind_attributes
.get(actor.object_id.0 as usize)
.get(usize::from(actor.object_id))
.and_then(|x| x.as_ref())
.ok_or(FrameError::MissingCache {
actor: actor_id,
actor_object: actor.object_id,
})?;

actors.insert(actor.actor_id.0 as usize, (actor.object_id, cache_info));
actors.insert(actor.actor_id, (actor.object_id, cache_info));
new_actors.push(actor);
} else {
// We'll be updating an existing actor with some attributes so we need
// to track down what the actor's type is and what attributes are available
let (object_id, cache_info) = actors
.get(actor_id.0 as usize)
.get(actor_id)
.ok_or(FrameError::MissingActor { actor: actor_id })?;

// While there are more attributes to update for our actor:
Expand All @@ -208,7 +238,7 @@ impl<'a, 'b> FrameDecoder<'a, 'b> {
// decoding function. Experience has told me replays that fail to
// parse, fail to do so here, so a large chunk is dedicated to
// generating an error message with context
let attr = cache_info.attributes.get(stream_id.0 as usize).ok_or(
let attr = cache_info.attributes.get(stream_id).ok_or(
FrameError::MissingAttribute {
actor: actor_id,
actor_object: *object_id,
Expand Down Expand Up @@ -242,7 +272,7 @@ impl<'a, 'b> FrameDecoder<'a, 'b> {
}
} else {
deleted_actors.push(actor_id);
actors.delete(actor_id.0 as usize);
actors.delete(actor_id);
}
}

Expand Down Expand Up @@ -296,6 +326,7 @@ impl<'a, 'b> FrameDecoder<'a, 'b> {
ObjectId(key as i32),
value
.attributes
.raw
.map
.iter()
.enumerate()
Expand All @@ -308,6 +339,7 @@ impl<'a, 'b> FrameDecoder<'a, 'b> {
.collect(),
frames: frames.clone(),
actors: actors
.raw
.map
.iter()
.map(|(k, (o, _))| (ActorId(*k as i32), *o))
Expand Down
4 changes: 2 additions & 2 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::cmp;
pub(crate) struct CacheInfo {
max_prop_id: u32,
prop_id_bits: u32,
attributes: SegmentedArray<ObjectAttribute>,
attributes: SegmentedArray<StreamId, ObjectAttribute>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -148,7 +148,7 @@ pub(crate) fn parse(header: &Header, body: &ReplayBody) -> Result<NetworkFrames,
.saturating_add(1);
let mut attributes = SegmentedArray::new(64);
for (k, v) in attrs {
attributes.insert(k.0 as usize, v);
attributes.insert(k, v);
}

let max_bit_width = crate::bits::bit_width(max as u64);
Expand Down
12 changes: 12 additions & 0 deletions src/network/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ impl From<StreamId> for i32 {
}
}

impl From<StreamId> for usize {
fn from(val: StreamId) -> Self {
val.0 as usize
}
}

impl fmt::Display for StreamId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
Expand All @@ -276,6 +282,12 @@ impl From<ActorId> for i32 {
}
}

impl From<ActorId> for usize {
fn from(val: ActorId) -> Self {
val.0 as usize
}
}

impl fmt::Display for ActorId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
Expand Down

0 comments on commit 240ccb0

Please sign in to comment.