Skip to content

Commit

Permalink
avm2: Try to speed up domain memory ops
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-McSweeney authored and Lord-McSweeney committed May 9, 2024
1 parent 8ab1905 commit 8eaede1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
34 changes: 12 additions & 22 deletions core/src/avm2/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2858,7 +2858,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
let dm = self.domain_memory();
let mut dm = dm
.as_bytearray_mut(self.context.gc_context)
.ok_or_else(|| "Unable to get bytearray storage".to_string())?;
.expect("Bytearray storage should exist");

let Ok(address) = usize::try_from(address) else {
return Err(make_error_1506(self));
Expand All @@ -2868,8 +2868,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
return Err(make_error_1506(self));
}

dm.write_at_nongrowing(&val.to_le_bytes(), address)
.map_err(|e| e.to_avm(self))?;
dm.set_nongrowing(address, val as u8);

Ok(FrameControl::Continue)
}
Expand All @@ -2882,7 +2881,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
let dm = self.domain_memory();
let mut dm = dm
.as_bytearray_mut(self.context.gc_context)
.ok_or_else(|| "Unable to get bytearray storage".to_string())?;
.expect("Bytearray storage should exist");

let Ok(address) = usize::try_from(address) else {
return Err(make_error_1506(self));
Expand All @@ -2904,7 +2903,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
let dm = self.domain_memory();
let mut dm = dm
.as_bytearray_mut(self.context.gc_context)
.ok_or_else(|| "Unable to get bytearray storage".to_string())?;
.expect("Bytearray storage should exist");

let Ok(address) = usize::try_from(address) else {
return Err(make_error_1506(self));
Expand All @@ -2926,7 +2925,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
let dm = self.domain_memory();
let mut dm = dm
.as_bytearray_mut(self.context.gc_context)
.ok_or_else(|| "Unable to get bytearray storage".to_string())?;
.expect("Bytearray storage should exist");

let Ok(address) = usize::try_from(address) else {
return Err(make_error_1506(self));
Expand All @@ -2948,7 +2947,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
let dm = self.domain_memory();
let mut dm = dm
.as_bytearray_mut(self.context.gc_context)
.ok_or_else(|| "Unable to get bytearray storage".to_string())?;
.expect("Bytearray storage should exist");

let Ok(address) = usize::try_from(address) else {
return Err(make_error_1506(self));
Expand All @@ -2967,9 +2966,8 @@ impl<'a, 'gc> Activation<'a, 'gc> {
let address = self.pop_stack().coerce_to_u32(self)? as usize;

let dm = self.domain_memory();
let dm = dm
.as_bytearray()
.ok_or_else(|| "Unable to get bytearray storage".to_string())?;
let dm = dm.as_bytearray().expect("Bytearray storage should exist");

let val = dm.get(address);

if let Some(val) = val {
Expand All @@ -2986,9 +2984,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
let address = self.pop_stack().coerce_to_u32(self)? as usize;

let dm = self.domain_memory();
let dm = dm
.as_bytearray()
.ok_or_else(|| "Unable to get bytearray storage".to_string())?;
let dm = dm.as_bytearray().expect("Bytearray storage should exist");

if address > dm.len() - 2 {
return Err(make_error_1506(self));
Expand All @@ -3005,9 +3001,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
let address = self.pop_stack().coerce_to_u32(self)? as usize;

let dm = self.domain_memory();
let dm = dm
.as_bytearray()
.ok_or_else(|| "Unable to get bytearray storage".to_string())?;
let dm = dm.as_bytearray().expect("Bytearray storage should exist");

if address > dm.len() - 4 {
return Err(make_error_1506(self));
Expand All @@ -3023,9 +3017,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
let address = self.pop_stack().coerce_to_u32(self)? as usize;

let dm = self.domain_memory();
let dm = dm
.as_bytearray()
.ok_or_else(|| "Unable to get bytearray storage".to_string())?;
let dm = dm.as_bytearray().expect("Bytearray storage should exist");

if address > dm.len() - 4 {
return Err(make_error_1506(self));
Expand All @@ -3042,9 +3034,7 @@ impl<'a, 'gc> Activation<'a, 'gc> {
let address = self.pop_stack().coerce_to_u32(self)? as usize;

let dm = self.domain_memory();
let dm = dm
.as_bytearray()
.ok_or_else(|| "Unable to get bytearray storage".to_string())?;
let dm = dm.as_bytearray().expect("Bytearray storage should exist");

if address > dm.len() - 8 {
return Err(make_error_1506(self));
Expand Down
5 changes: 5 additions & 0 deletions core/src/avm2/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ impl ByteArrayStorage {
*self.bytes.get_mut(item).unwrap() = value;
}

/// Write a single byte at any offset in the bytearray, panicking if out of bounds.
pub fn set_nongrowing(&mut self, item: usize, value: u8) {
self.bytes[item] = value;
}

pub fn delete(&mut self, item: usize) {
if let Some(i) = self.bytes.get_mut(item) {
*i = 0;
Expand Down

0 comments on commit 8eaede1

Please sign in to comment.