diff --git a/core/src/mast/serialization/basic_block_data_builder.rs b/core/src/mast/serialization/basic_block_data_builder.rs index 94f50603ff..78a045d6c4 100644 --- a/core/src/mast/serialization/basic_block_data_builder.rs +++ b/core/src/mast/serialization/basic_block_data_builder.rs @@ -7,7 +7,7 @@ use crate::{ AdviceInjector, DebugOptions, Decorator, SignatureKind, }; -use super::{decorator::EncodedDecoratorVariant, DataOffset, StringIndex, StringRef}; +use super::{decorator::EncodedDecoratorVariant, DataOffset, StringIndex}; // BASIC BLOCK DATA BUILDER // ================================================================================================ @@ -48,7 +48,7 @@ impl BasicBlockDataBuilder { } /// Returns the serialized [`crate::mast::MastForest`] data field, as well as the string table. - pub fn into_parts(mut self) -> (Vec, Vec) { + pub fn into_parts(mut self) -> (Vec, Vec) { let string_table = self.string_table_builder.into_table(&mut self.data); (self.data, string_table) } @@ -140,7 +140,7 @@ impl BasicBlockDataBuilder { #[derive(Debug, Default)] struct StringTableBuilder { - table: Vec, + table: Vec, str_to_index: BTreeMap, StringIndex>, strings_data: Vec, } @@ -153,35 +153,29 @@ impl StringTableBuilder { } else { // add new string to table // NOTE: these string refs' offset will need to be shifted again in `into_table()` - let str_ref = StringRef { - offset: self - .strings_data - .len() - .try_into() - .expect("strings table larger than 2^32 bytes"), - }; + let str_offset = self + .strings_data + .len() + .try_into() + .expect("strings table larger than 2^32 bytes"); + let str_idx = self.table.len(); string.write_into(&mut self.strings_data); - self.table.push(str_ref); + self.table.push(str_offset); self.str_to_index.insert(Blake3_256::hash(string.as_bytes()), str_idx); str_idx } } - pub fn into_table(self, data: &mut Vec) -> Vec { + pub fn into_table(self, data: &mut Vec) -> Vec { let table_offset: u32 = data .len() .try_into() .expect("MAST forest serialization: data field longer than 2^32 bytes"); data.extend(self.strings_data); - self.table - .into_iter() - .map(|str_ref| StringRef { - offset: str_ref.offset + table_offset, - }) - .collect() + self.table.into_iter().map(|str_offset| str_offset + table_offset).collect() } } diff --git a/core/src/mast/serialization/basic_block_data_decoder.rs b/core/src/mast/serialization/basic_block_data_decoder.rs index 8a9d50e902..78dd77215f 100644 --- a/core/src/mast/serialization/basic_block_data_decoder.rs +++ b/core/src/mast/serialization/basic_block_data_decoder.rs @@ -2,19 +2,19 @@ use crate::{ AdviceInjector, AssemblyOp, DebugOptions, Decorator, DecoratorList, Operation, SignatureKind, }; -use super::{decorator::EncodedDecoratorVariant, DataOffset, StringIndex, StringRef}; +use super::{decorator::EncodedDecoratorVariant, DataOffset, StringIndex}; use alloc::{string::String, vec::Vec}; use miden_crypto::Felt; use winter_utils::{ByteReader, Deserializable, DeserializationError, SliceReader}; pub struct BasicBlockDataDecoder<'a> { data: &'a [u8], - strings: &'a [StringRef], + strings: &'a [DataOffset], } /// Constructors impl<'a> BasicBlockDataDecoder<'a> { - pub fn new(data: &'a [u8], strings: &'a [StringRef]) -> Self { + pub fn new(data: &'a [u8], strings: &'a [DataOffset]) -> Self { Self { data, strings } } } @@ -189,15 +189,9 @@ impl<'a> BasicBlockDataDecoder<'a> { } fn read_string(&self, str_idx: StringIndex) -> Result { - let str_offset = { - let str_ref = self.strings.get(str_idx).ok_or_else(|| { - DeserializationError::InvalidValue(format!( - "invalid index in strings table: {str_idx}" - )) - })?; - - str_ref.offset as usize - }; + let str_offset = self.strings.get(str_idx).copied().ok_or_else(|| { + DeserializationError::InvalidValue(format!("invalid index in strings table: {str_idx}")) + })? as usize; let mut reader = SliceReader::new(&self.data[str_offset..]); reader.read() diff --git a/core/src/mast/serialization/mod.rs b/core/src/mast/serialization/mod.rs index d8a6b0f455..a71c79c89a 100644 --- a/core/src/mast/serialization/mod.rs +++ b/core/src/mast/serialization/mod.rs @@ -39,32 +39,6 @@ const MAGIC: &[u8; 5] = b"MAST\0"; /// version field itself, but should be considered invalid for now. const VERSION: [u8; 3] = [0, 0, 0]; -// STRING REF -// ================================================================================================ - -/// An entry in the `strings` table of an encoded [`MastForest`]. -/// -/// Strings are UTF8-encoded. -#[derive(Debug)] -pub struct StringRef { - /// Offset into the `data` section. - offset: DataOffset, -} - -impl Serializable for StringRef { - fn write_into(&self, target: &mut W) { - self.offset.write_into(target); - } -} - -impl Deserializable for StringRef { - fn read_from(source: &mut R) -> Result { - let offset = DataOffset::read_from(source)?; - - Ok(Self { offset }) - } -} - // MAST FOREST SERIALIZATION/DESERIALIZATION // ================================================================================================ @@ -131,7 +105,7 @@ impl Deserializable for MastForest { let roots: Vec = Deserializable::read_from(source)?; - let strings: Vec = Deserializable::read_from(source)?; + let strings: Vec = Deserializable::read_from(source)?; let data: Vec = Deserializable::read_from(source)?;