Skip to content

Commit

Permalink
remove key/index check
Browse files Browse the repository at this point in the history
  • Loading branch information
ivandevp committed Oct 23, 2024
1 parent a65e74f commit b21e1d1
Showing 1 changed file with 60 additions and 75 deletions.
135 changes: 60 additions & 75 deletions nova_vm/src/heap/element_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1832,75 +1832,6 @@ impl ElementArrays {
elements_vector.elements_index = new_index;
}

fn shallow_clone_with_key(
&mut self,
elements_vector: ElementsVector,
new_key: ElementArrayKey,
new_len: u32,
) -> SealableElementsVector {
let index = elements_vector.elements_index.into_index();
let ElementArrays {
e2pow4,
e2pow6,
e2pow8,
e2pow10,
e2pow12,
e2pow16,
e2pow24,
e2pow32,
} = self;
let new_index = match new_key {
ElementArrayKey::Empty => ElementIndex::from_u32_index(0),
ElementArrayKey::E4 => {
let elements = e2pow4;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E6 => {
let elements = e2pow6;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E8 => {
let elements = e2pow8;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E10 => {
let elements = e2pow10;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E12 => {
let elements = e2pow12;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E16 => {
let elements = e2pow16;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E24 => {
let elements = e2pow24;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E32 => {
let elements = e2pow32;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
};

SealableElementsVector {
cap: new_key,
elements_index: new_index,
len: new_len,
len_writable: true,
}
}

pub fn allocate_elements_with_capacity(&mut self, capacity: usize) -> ElementsVector {
let cap = ElementArrayKey::from(capacity);
ElementsVector {
Expand Down Expand Up @@ -2377,12 +2308,66 @@ impl ElementArrays {
/// This method creates a "shallow clone" of the elements of a trivial/dense array.
/// It does not do anything with descriptors and assumes there is a previous validation in place.
pub fn shallow_clone(&mut self, elements_vector: ElementsVector) -> SealableElementsVector {
let new_len = usize::try_from(elements_vector.len()).unwrap();
let new_key = ElementArrayKey::from(new_len);
if new_key == elements_vector.cap {
self.shallow_clone_with_key(elements_vector, elements_vector.cap, elements_vector.len())
} else {
self.shallow_clone_with_key(elements_vector, new_key, new_len as u32)
let index = elements_vector.elements_index.into_index();
let ElementArrays {
e2pow4,
e2pow6,
e2pow8,
e2pow10,
e2pow12,
e2pow16,
e2pow24,
e2pow32,
} = self;
let new_index = match elements_vector.cap {
ElementArrayKey::Empty => ElementIndex::from_u32_index(0),
ElementArrayKey::E4 => {
let elements = e2pow4;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E6 => {
let elements = e2pow6;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E8 => {
let elements = e2pow8;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E10 => {
let elements = e2pow10;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E12 => {
let elements = e2pow12;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E16 => {
let elements = e2pow16;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E24 => {
let elements = e2pow24;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
ElementArrayKey::E32 => {
let elements = e2pow32;
elements.values.extend_from_within(index..index + 1);
ElementIndex::last_element_index(&elements.values)
}
};

SealableElementsVector {
cap: elements_vector.cap,
elements_index: new_index,
len: elements_vector.len(),
len_writable: true,
}
}
}
Expand Down

0 comments on commit b21e1d1

Please sign in to comment.