Skip to content

Commit

Permalink
Address CR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
serban300 committed Sep 12, 2024
1 parent adff127 commit 00a6f4f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub trait Input {
///
/// The aim is to get a reasonable approximation of memory usage, especially with variably
/// sized types like `Vec`s. Depending on the structure, it is acceptable to be off by a bit.
/// For example for structures like `BTreeMap` we don't track the memory used by the internal
/// For example for structures like `BTreeMap` we don't track the memory used by the internal
/// tree nodes etc. Also we don't take alignment or memory layouts into account.
/// But we should always track the memory used by the decoded data inside the type.
fn on_before_alloc_mem(&mut self, _size: usize) -> Result<(), Error> {
Expand Down Expand Up @@ -1281,7 +1281,7 @@ macro_rules! impl_codec_through_iterator {
<Compact<u32>>::decode(input).and_then(move |Compact(len)| {
input.descend_ref()?;
let result = Result::from_iter((0..len).map(|_| {
input.on_before_alloc_mem(0 $( + mem::size_of::<$generics>() )*)?;
input.on_before_alloc_mem(0usize$(.saturating_add(mem::size_of::<$generics>()))*)?;
Decode::decode(input)
}));
input.ascend_ref();
Expand Down
2 changes: 1 addition & 1 deletion src/mem_tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use crate::{Decode, Error, Input};
use impl_trait_for_tuples::impl_for_tuples;

/// Marker trait used for identifying types that call the mem tracking hooks exposed by `Input`
/// Marker trait used for identifying types that call the [`Input::on_before_alloc_mem`] hook
/// while decoding.
pub trait DecodeWithMemTracking: Decode {}

Expand Down
5 changes: 5 additions & 0 deletions tests/mem_tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ where
let decoded_obj = T::decode(&mut input)?;
assert_eq!(&decoded_obj, &obj);
assert_eq!(input.used_mem(), expected_used_mem);

if expected_used_mem > 0 {
let mut input = MemTrackingInput::new(raw_input, expected_used_mem);
assert!(T::decode(&mut input).is_err());
}
Ok(decoded_obj)
}

Expand Down

0 comments on commit 00a6f4f

Please sign in to comment.