Skip to content

Commit

Permalink
mn vec item refactor: removed PAYLOAD_SIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed May 8, 2024
1 parent 319c25a commit 0d950c2
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 101 deletions.
1 change: 0 additions & 1 deletion framework/base/src/types/flags/esdt_local_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ impl<'a> From<&'a [u8]> for EsdtLocalRole {

impl ManagedVecItem for EsdtLocalRole {
type PAYLOAD = ManagedVecItemPayloadBuffer<1>;
const PAYLOAD_SIZE: usize = 1;
const SKIPS_RESERIALIZATION: bool = false; // TODO: might be ok to be true, but needs testing
type Ref<'a> = Self;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl<M: ManagedTypeApi> EsdtTokenPaymentMultiValue<M> {

impl<M: ManagedTypeApi> ManagedVecItem for EsdtTokenPaymentMultiValue<M> {
type PAYLOAD = <EsdtTokenPayment<M> as ManagedVecItem>::PAYLOAD;
const PAYLOAD_SIZE: usize = EsdtTokenPayment::<M>::PAYLOAD_SIZE;
const SKIPS_RESERIALIZATION: bool = EsdtTokenPayment::<M>::SKIPS_RESERIALIZATION;
type Ref<'a> = Self;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,40 @@
use super::ManagedVecItem;
use super::{ManagedVecItem, ManagedVecItemPayload};
use core::{cmp::Ordering, marker::PhantomData};

pub struct EncodedManagedVecItem<T: ManagedVecItem>
where
T: ManagedVecItem,
[(); <T as ManagedVecItem>::PAYLOAD_SIZE]:,
{
pub encoded: [u8; <T as ManagedVecItem>::PAYLOAD_SIZE],
pub encoded: T::PAYLOAD,
_phantom: PhantomData<T>,
}

impl<T> EncodedManagedVecItem<T>
where
T: ManagedVecItem,
[(); <T as ManagedVecItem>::PAYLOAD_SIZE]:,
{
pub(crate) fn decode(&self) -> T {
T::from_byte_reader(|item_bytes| {
item_bytes.copy_from_slice(&self.encoded);
item_bytes.copy_from_slice(self.encoded.payload_slice());
})
}
}

impl<T> PartialEq for EncodedManagedVecItem<T>
where
T: PartialEq + ManagedVecItem,
[(); <T as ManagedVecItem>::PAYLOAD_SIZE]:,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
self.decode().eq(&other.decode())
}
}

impl<T> Eq for EncodedManagedVecItem<T>
where
T: Eq + ManagedVecItem,
[(); <T as ManagedVecItem>::PAYLOAD_SIZE]:,
{
}
impl<T> Eq for EncodedManagedVecItem<T> where T: Eq + ManagedVecItem {}

impl<T> PartialOrd for EncodedManagedVecItem<T>
where
T: PartialOrd + ManagedVecItem,
[(); <T as ManagedVecItem>::PAYLOAD_SIZE]:,
{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Expand All @@ -54,7 +45,6 @@ where
impl<T> Ord for EncodedManagedVecItem<T>
where
T: Ord + ManagedVecItem,
[(); <T as ManagedVecItem>::PAYLOAD_SIZE]:,
{
fn cmp(&self, other: &Self) -> Ordering {
self.decode().cmp(&other.decode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ where
T: ManagedVecItem,
{
ManagedVecItem::from_byte_reader(|bytes| {
let size = T::PAYLOAD_SIZE;
let size = T::payload_size();
bytes.copy_from_slice(&arr[*index..*index + size]);
*index += size;
})
Expand All @@ -169,7 +169,7 @@ where
T: ManagedVecItem,
{
ManagedVecItem::to_byte_writer(item, |bytes| {
let size = T::PAYLOAD_SIZE;
let size = T::payload_size();
arr[*index..*index + size].copy_from_slice(bytes);
*index += size;
});
Expand All @@ -186,7 +186,6 @@ impl<M: ManagedTypeApi> IntoMultiValue for EsdtTokenPayment<M> {

impl<M: ManagedTypeApi> ManagedVecItem for EsdtTokenPayment<M> {
type PAYLOAD = ManagedVecItemPayloadBuffer<16>;
const PAYLOAD_SIZE: usize = 16;
const SKIPS_RESERIALIZATION: bool = false;
type Ref<'a> = Self;

Expand Down
1 change: 0 additions & 1 deletion framework/base/src/types/managed/wrapped/managed_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ where
T: ManagedType<M> + 'static,
{
type PAYLOAD = ManagedVecItemPayloadBuffer<4>;
const PAYLOAD_SIZE: usize = 4;
const SKIPS_RESERIALIZATION: bool = false;
type Ref<'a> = Self;

Expand Down
35 changes: 12 additions & 23 deletions framework/base/src/types/managed/wrapped/managed_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ where
/// Number of items.
#[inline]
pub fn len(&self) -> usize {
self.byte_len() / T::PAYLOAD_SIZE
self.byte_len() / T::payload_size()
}

#[inline]
Expand All @@ -132,7 +132,7 @@ where
}

pub fn try_get(&self, index: usize) -> Option<T::Ref<'_>> {
let byte_index = index * T::PAYLOAD_SIZE;
let byte_index = index * T::payload_size();
let mut load_result = Ok(());
let result = unsafe {
T::from_byte_reader_as_borrow(|dest_slice| {
Expand Down Expand Up @@ -178,7 +178,7 @@ where
}

pub(super) unsafe fn get_unsafe(&self, index: usize) -> T {
let byte_index = index * T::PAYLOAD_SIZE;
let byte_index = index * T::payload_size();
let mut load_result = Ok(());
let result = T::from_byte_reader(|dest_slice| {
load_result = self.buffer.load_slice(byte_index, dest_slice);
Expand All @@ -191,15 +191,15 @@ where
}

pub fn set(&mut self, index: usize, item: &T) -> Result<(), InvalidSliceError> {
let byte_index = index * T::PAYLOAD_SIZE;
let byte_index = index * T::payload_size();
item.to_byte_writer(|slice| self.buffer.set_slice(byte_index, slice))
}

/// Returns a new `ManagedVec`, containing the [start_index, end_index) range of elements.
/// Returns `None` if any index is out of range
pub fn slice(&self, start_index: usize, end_index: usize) -> Option<Self> {
let byte_start = start_index * T::PAYLOAD_SIZE;
let byte_end = end_index * T::PAYLOAD_SIZE;
let byte_start = start_index * T::payload_size();
let byte_end = end_index * T::payload_size();
let opt_buffer = self.buffer.copy_slice(byte_start, byte_end - byte_start);
opt_buffer.map(ManagedVec::new_from_raw_buffer)
}
Expand Down Expand Up @@ -314,10 +314,9 @@ where
fn with_self_as_slice<R, F>(&self, f: F) -> R
where
F: FnOnce(&[EncodedManagedVecItem<T>]) -> R,
[(); T::PAYLOAD_SIZE]:,
{
self.buffer.with_buffer_contents(|bytes| {
let item_len = bytes.len() / T::PAYLOAD_SIZE;
let item_len = bytes.len() / T::payload_size();
let values = Self::transmute_slice(bytes, item_len);
f(values)
})
Expand All @@ -326,14 +325,13 @@ where
fn with_self_as_slice_mut<F>(&mut self, f: F)
where
F: FnOnce(&mut [EncodedManagedVecItem<T>]) -> &[EncodedManagedVecItem<T>],
[(); T::PAYLOAD_SIZE]:,
{
self.buffer.with_buffer_contents_mut(|bytes| {
let item_len = bytes.len() / T::PAYLOAD_SIZE;
let item_len = bytes.len() / T::payload_size();
let values = Self::transmute_slice_mut(bytes, item_len);

let result = f(values);
let result_len = result.len() * T::PAYLOAD_SIZE;
let result_len = result.len() * T::payload_size();
Self::transmute_slice(result, result_len)
});
}
Expand All @@ -357,7 +355,6 @@ impl<M, T> ManagedVec<M, T>
where
M: ManagedTypeApi,
T: ManagedVecItem + Ord + Debug,
[(); T::PAYLOAD_SIZE]:,
{
pub fn sort(&mut self) {
self.with_self_as_slice_mut(|slice| {
Expand Down Expand Up @@ -398,10 +395,7 @@ where
});
}

pub fn sort_unstable(&mut self)
where
[(); T::PAYLOAD_SIZE]:,
{
pub fn sort_unstable(&mut self) {
self.with_self_as_slice_mut(|slice| {
slice.sort_unstable();
slice
Expand All @@ -411,7 +405,6 @@ where
pub fn sort_unstable_by<F>(&mut self, mut compare: F)
where
F: FnMut(&T, &T) -> Ordering,
[(); T::PAYLOAD_SIZE]:,
{
self.with_self_as_slice_mut(|slice| {
slice.sort_unstable_by(|a, b| compare(&a.decode(), &b.decode()));
Expand All @@ -423,7 +416,6 @@ where
where
F: FnMut(&T) -> K,
K: Ord,
[(); T::PAYLOAD_SIZE]:,
{
self.with_self_as_slice_mut(|slice| {
slice.sort_unstable_by_key(|a| f(&a.decode()));
Expand Down Expand Up @@ -463,10 +455,7 @@ where
M: ManagedTypeApi,
T: ManagedVecItem + PartialEq + Debug,
{
pub fn dedup(&mut self)
where
[(); T::PAYLOAD_SIZE]:,
{
pub fn dedup(&mut self) {
self.with_self_as_slice_mut(|slice| {
let same_bucket = |a, b| a == b;
let len = slice.len();
Expand Down Expand Up @@ -539,7 +528,7 @@ where
if self_item != other_item {
return false;
}
byte_index += T::PAYLOAD_SIZE;
byte_index += T::payload_size();
}
true
}
Expand Down
16 changes: 3 additions & 13 deletions framework/base/src/types/managed/wrapped/managed_vec_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ use super::{ManagedVecItemNestedTuple, ManagedVecItemPayload, ManagedVecItemPayl
/// Not all data needs to be stored as payload, for instance for most managed types
/// the payload is just the handle, whereas the mai ndata is kept by the VM.
pub trait ManagedVecItem: 'static {
/// Type managing the underlying binary representation in a ManagedVec..
type PAYLOAD: ManagedVecItemPayload;

/// Size of the data stored in the underlying `ManagedBuffer`.
const PAYLOAD_SIZE: usize;

/// If true, then the encoding of the item is identical to the payload,
/// and no further conversion is necessary
/// (the underlying buffer can be used as-is during serialization).
Expand Down Expand Up @@ -59,7 +57,6 @@ macro_rules! impl_int {
($ty:ident, $payload_size:expr) => {
impl ManagedVecItem for $ty {
type PAYLOAD = ManagedVecItemPayloadBuffer<$payload_size>;
const PAYLOAD_SIZE: usize = $payload_size;
const SKIPS_RESERIALIZATION: bool = true;
type Ref<'a> = Self;
fn from_byte_reader<Reader: FnMut(&mut [u8])>(mut reader: Reader) -> Self {
Expand Down Expand Up @@ -89,7 +86,6 @@ impl_int! {i64, 8}

impl ManagedVecItem for usize {
type PAYLOAD = ManagedVecItemPayloadBuffer<4>;
const PAYLOAD_SIZE: usize = 4;
const SKIPS_RESERIALIZATION: bool = true;
type Ref<'a> = Self;

Expand All @@ -113,7 +109,6 @@ impl ManagedVecItem for usize {

impl ManagedVecItem for bool {
type PAYLOAD = ManagedVecItemPayloadBuffer<1>;
const PAYLOAD_SIZE: usize = 1;
const SKIPS_RESERIALIZATION: bool = true;
type Ref<'a> = Self;

Expand All @@ -138,17 +133,15 @@ impl ManagedVecItem for bool {
impl<T> ManagedVecItem for Option<T>
where
(u8, (T, ())): ManagedVecItemNestedTuple,
[(); 1 + T::PAYLOAD_SIZE]:,
T: ManagedVecItem,
{
type PAYLOAD = <(u8, (T, ())) as ManagedVecItemNestedTuple>::PAYLOAD;
const PAYLOAD_SIZE: usize = 1 + T::PAYLOAD_SIZE;
const SKIPS_RESERIALIZATION: bool = false;
type Ref<'a> = Self;

fn from_byte_reader<Reader: FnMut(&mut [u8])>(mut reader: Reader) -> Self {
let mut payload = Self::PAYLOAD::new_buffer();
let payload_slice = payload.payload_slice();
let payload_slice = payload.payload_slice_mut();
reader(payload_slice);
if payload_slice[0] == 0 {
None
Expand All @@ -167,7 +160,7 @@ where

fn to_byte_writer<R, Writer: FnMut(&[u8]) -> R>(&self, mut writer: Writer) -> R {
let mut payload = Self::PAYLOAD::new_buffer();
let slice = payload.payload_slice();
let slice = payload.payload_slice_mut();
if let Some(t) = self {
slice[0] = 1;
T::to_byte_writer(t, |bytes| {
Expand All @@ -182,7 +175,6 @@ macro_rules! impl_managed_type {
($ty:ident) => {
impl<M: ManagedTypeApi> ManagedVecItem for $ty<M> {
type PAYLOAD = ManagedVecItemPayloadBuffer<4>;
const PAYLOAD_SIZE: usize = 4;
const SKIPS_RESERIALIZATION: bool = false;
type Ref<'a> = ManagedRef<'a, M, Self>;

Expand Down Expand Up @@ -217,7 +209,6 @@ where
M: ManagedTypeApi,
{
type PAYLOAD = ManagedVecItemPayloadBuffer<4>;
const PAYLOAD_SIZE: usize = 4;
const SKIPS_RESERIALIZATION: bool = false;
type Ref<'a> = ManagedRef<'a, M, Self>;

Expand Down Expand Up @@ -247,7 +238,6 @@ where
T: ManagedVecItem,
{
type PAYLOAD = ManagedVecItemPayloadBuffer<4>;
const PAYLOAD_SIZE: usize = 4;
const SKIPS_RESERIALIZATION: bool = false;
type Ref<'a> = ManagedRef<'a, M, Self>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ pub trait ManagedVecItemPayload {

fn payload_size() -> usize;

fn payload_slice(&mut self) -> &mut [u8];
fn payload_slice(&self) -> &[u8];

fn payload_slice_mut(&mut self) -> &mut [u8];
}

pub struct ManagedVecItemEmptyPayload;
Expand All @@ -17,7 +19,11 @@ impl ManagedVecItemPayload for ManagedVecItemEmptyPayload {
0
}

fn payload_slice(&mut self) -> &mut [u8] {
fn payload_slice(&self) -> &[u8] {
&[]
}

fn payload_slice_mut(&mut self) -> &mut [u8] {
&mut []
}
}
Expand All @@ -35,7 +41,11 @@ impl<const N: usize> ManagedVecItemPayload for ManagedVecItemPayloadBuffer<N> {
N
}

fn payload_slice(&mut self) -> &mut [u8] {
fn payload_slice(&self) -> &[u8] {
&self.buffer[..]
}

fn payload_slice_mut(&mut self) -> &mut [u8] {
&mut self.buffer[..]
}
}
Expand Down
Loading

0 comments on commit 0d950c2

Please sign in to comment.