Browse Source

Avoid unneeded bounds checks in bytecode address patching (#2680)

As discussed in this comment https://github.com/boa-dev/boa/pull/2669#discussion_r1137618027,
`rustc` doesn't seem to optimize out the bounds checks.
pull/2684/head
Haled Odat 2 years ago
parent
commit
50a343a3ce
  1. 11
      boa_engine/src/bytecompiler/mod.rs

11
boa_engine/src/bytecompiler/mod.rs

@ -544,15 +544,16 @@ impl<'b, 'host> ByteCompiler<'b, 'host> {
} }
fn patch_jump_with_target(&mut self, label: Label, target: u32) { fn patch_jump_with_target(&mut self, label: Label, target: u32) {
const U32_SIZE: usize = std::mem::size_of::<u32>();
let Label { index } = label; let Label { index } = label;
let index = index as usize; let index = index as usize;
let bytes = target.to_ne_bytes(); let bytes = target.to_ne_bytes();
self.bytecode[index + 1] = bytes[0];
self.bytecode[index + 2] = bytes[1]; // This is done to avoid unneeded bounds checks.
self.bytecode[index + 3] = bytes[2]; assert!(self.bytecode.len() > index + U32_SIZE && usize::MAX - U32_SIZE >= index);
self.bytecode[index + 4] = bytes[3]; self.bytecode[index + 1..=index + U32_SIZE].copy_from_slice(bytes.as_slice());
} }
fn patch_jump(&mut self, label: Label) { fn patch_jump(&mut self, label: Label) {

Loading…
Cancel
Save