Skip to content

Commit

Permalink
Remove StringRef in favor of DataOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
plafer committed Jul 12, 2024
1 parent f45d0c5 commit 43cadeb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 57 deletions.
30 changes: 12 additions & 18 deletions core/src/mast/serialization/basic_block_data_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ================================================================================================
Expand Down Expand Up @@ -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<u8>, Vec<StringRef>) {
pub fn into_parts(mut self) -> (Vec<u8>, Vec<DataOffset>) {
let string_table = self.string_table_builder.into_table(&mut self.data);
(self.data, string_table)
}
Expand Down Expand Up @@ -140,7 +140,7 @@ impl BasicBlockDataBuilder {

#[derive(Debug, Default)]
struct StringTableBuilder {
table: Vec<StringRef>,
table: Vec<DataOffset>,
str_to_index: BTreeMap<Blake3Digest<32>, StringIndex>,
strings_data: Vec<u8>,
}
Expand All @@ -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<u8>) -> Vec<StringRef> {
pub fn into_table(self, data: &mut Vec<u8>) -> Vec<DataOffset> {
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()
}
}
18 changes: 6 additions & 12 deletions core/src/mast/serialization/basic_block_data_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
Expand Down Expand Up @@ -189,15 +189,9 @@ impl<'a> BasicBlockDataDecoder<'a> {
}

fn read_string(&self, str_idx: StringIndex) -> Result<String, DeserializationError> {
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()
Expand Down
28 changes: 1 addition & 27 deletions core/src/mast/serialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: ByteWriter>(&self, target: &mut W) {
self.offset.write_into(target);
}
}

impl Deserializable for StringRef {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let offset = DataOffset::read_from(source)?;

Ok(Self { offset })
}
}

// MAST FOREST SERIALIZATION/DESERIALIZATION
// ================================================================================================

Expand Down Expand Up @@ -131,7 +105,7 @@ impl Deserializable for MastForest {

let roots: Vec<MastNodeId> = Deserializable::read_from(source)?;

let strings: Vec<StringRef> = Deserializable::read_from(source)?;
let strings: Vec<DataOffset> = Deserializable::read_from(source)?;

let data: Vec<u8> = Deserializable::read_from(source)?;

Expand Down

0 comments on commit 43cadeb

Please sign in to comment.