Skip to content

Commit

Permalink
Eliminate size redundancy in the Object header
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-blaeser committed Dec 20, 2023
1 parent c77355e commit 0fe3926
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions rts/motoko-rts/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub(crate) unsafe fn print_boxed_object(buf: &mut WriteBuf, p: usize) {
let _ = write!(
buf,
"<Object size={:#x} hash_ptr={:#x} field=[",
(*object).size,
object.size(),
(*object).hash_blob.get_raw()
);
for i in 0..object.size() {
Expand All @@ -155,7 +155,7 @@ pub(crate) unsafe fn print_boxed_object(buf: &mut WriteBuf, p: usize) {
let _ = write!(buf, ")");
}

if i != (*object).size - 1 {
if i != object.size() - 1 {
let _ = write!(buf, ",");
}
}
Expand Down
4 changes: 2 additions & 2 deletions rts/motoko-rts/src/stabilization/layout/stable_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::{Serializer, StableToSpace, StableValue, StaticScanner};

#[repr(C)]
pub struct StableObject {
// The size information is redundant to the `hash_blob` word size but storing it simplifies and optimizes deserialization.
size: u32, // Number of fields.
hash_blob: StableValue, // Pointer to a blob containing the `u32` hashes of the field labels.
// Dynamically sized body with `size` fields, each of `StableValue`, ordered according to the hashes in the blob.
Expand All @@ -30,7 +31,7 @@ impl StaticScanner<StableValue> for StableObject {
impl Serializer<Object> for StableObject {
unsafe fn serialize_static_part(main_object: *mut Object) -> Self {
StableObject {
size: (*main_object).size,
size: main_object.size(),
hash_blob: StableValue::serialize((*main_object).hash_blob),
}
}
Expand Down Expand Up @@ -70,7 +71,6 @@ impl Serializer<Object> for StableObject {
(*target_object)
.header
.init_forward(Value::from_ptr(target_object as usize));
(*target_object).size = self.size;
(*target_object).hash_blob = self.hash_blob.deserialize();
}

Expand Down
6 changes: 4 additions & 2 deletions rts/motoko-rts/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,6 @@ impl Region {
#[repr(C)] // See the note at the beginning of this module
pub struct Object {
pub header: Obj,
pub size: u32, // Number of elements
pub hash_blob: Value, // Pointer to a blob containing the hashes of the object field labels.
}

Expand All @@ -625,8 +624,11 @@ impl Object {
self.add(1) as *mut Value // skip object header
}

/// Number of fields in the object.
pub(crate) unsafe fn size(self: *mut Self) -> u32 {
(*self).size
let hash_blob_length = (*self).hash_blob.as_blob().len().as_u32();
debug_assert_eq!(hash_blob_length % WORD_SIZE, 0);
hash_blob_length / WORD_SIZE
}

pub(crate) unsafe fn get(self: *mut Self, idx: u32) -> Value {
Expand Down

0 comments on commit 0fe3926

Please sign in to comment.