Skip to content

Commit 87e1071

Browse files
misc micro optimizations
1 parent 0d82700 commit 87e1071

5 files changed

Lines changed: 35 additions & 30 deletions

File tree

.cargo/config.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[build]
2+
rustflags = ["-C", "target-cpu=native"]
3+
4+

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ nohash-hasher = "0.2"
1515

1616
[profile.release]
1717
opt-level = 3
18-
lto = "thin"
18+
lto = "fat"
1919
codegen-units = 1
2020
panic = "abort"
2121
overflow-checks = false

src/bin/wagmi_inspect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
159159
println!(" {} : memory [pages: {}, max: {}]", name, m.size(), m.max());
160160
}
161161
ExportValue::Global(global) => {
162-
let g = global.borrow();
162+
let g = global.as_ref();
163163
let mutability = if g.mutable { "mut " } else { "" };
164164
println!(" {} : global {}{}", name, mutability, format_type(&g.ty));
165165
}

src/instance.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ pub enum ExportValue {
297297
Function(RuntimeFunction),
298298
Table(Rc<RefCell<WasmTable>>),
299299
Memory(Rc<RefCell<WasmMemory>>),
300-
Global(Rc<RefCell<WasmGlobal>>),
300+
Global(Rc<WasmGlobal>),
301301
}
302302

303303
pub type Exports = HashMap<String, ExportValue>;
@@ -317,7 +317,7 @@ pub struct Instance {
317317
pub module: Rc<Module>,
318318
pub memory: Option<Rc<RefCell<WasmMemory>>>,
319319
pub table: Option<Rc<RefCell<WasmTable>>>,
320-
pub globals: Vec<Rc<RefCell<WasmGlobal>>>,
320+
pub globals: Vec<Rc<WasmGlobal>>,
321321
pub functions: Vec<RuntimeFunction>,
322322
pub exports: Exports,
323323
}
@@ -409,9 +409,8 @@ impl Instance {
409409
let imported = imports.get(&import_ref.module).and_then(|m| m.get(&import_ref.field)).ok_or(Error::Link(UNKNOWN_IMPORT))?;
410410
match imported {
411411
ExportValue::Global(gl) => {
412-
let gb = gl.borrow();
412+
let gb = gl.as_ref();
413413
if gb.ty != g.ty || gb.mutable != g.is_mutable { return Err(Error::Link(INCOMPATIBLE_IMPORT)); }
414-
drop(gb);
415414
inst.globals.push(gl.clone());
416415
}
417416
_ => return Err(Error::Link(INCOMPATIBLE_IMPORT)),
@@ -420,7 +419,7 @@ impl Instance {
420419
// evaluate constant initializer
421420
let mut cpc = g.initializer_offset;
422421
let val = Instance::eval_const(&module, &mut cpc, &inst.globals)?;
423-
inst.globals.push(Rc::new(RefCell::new(WasmGlobal { ty: g.ty, mutable: g.is_mutable, value: Cell::new(val) })));
422+
inst.globals.push(Rc::new(WasmGlobal { ty: g.ty, mutable: g.is_mutable, value: Cell::new(val) }));
424423
}
425424
}
426425

@@ -558,7 +557,7 @@ impl Instance {
558557
fn eval_const(
559558
module: &Module,
560559
pc: &mut usize,
561-
globals: &[Rc<RefCell<WasmGlobal>>]
560+
globals: &[Rc<WasmGlobal>]
562561
) -> Result<WasmValue, Error> {
563562
let bytes = &module.bytes;
564563
let mut stack: Vec<WasmValue> = Vec::new();
@@ -569,7 +568,7 @@ impl Instance {
569568
0x42 => { let v: i64 = read_sleb128(bytes, pc)?; stack.push(WasmValue::from_i64(v)); }
570569
0x43 => { let bits = u32::from_le_bytes(bytes[*pc..*pc+4].try_into().unwrap()); *pc += 4; stack.push(WasmValue::from_f32_bits(bits)); }
571570
0x44 => { let bits = u64::from_le_bytes(bytes[*pc..*pc+8].try_into().unwrap()); *pc += 8; stack.push(WasmValue::from_f64_bits(bits)); }
572-
0x23 => { let gi: u32 = read_leb128(bytes, pc)?; let g = gi as usize; if g >= globals.len() { return Err(Error::Validation(UNKNOWN_GLOBAL)); } stack.push(globals[g].borrow().value.get()); }
571+
0x23 => { let gi: u32 = read_leb128(bytes, pc)?; let g = gi as usize; if g >= globals.len() { return Err(Error::Validation(UNKNOWN_GLOBAL)); } stack.push(globals[g].value.get()); }
573572
0x6a => { let b = stack.pop().unwrap().as_u32(); let a = stack.pop().unwrap().as_u32(); stack.push(WasmValue::from_u32(a.wrapping_add(b))); }
574573
0x6b => { let b = stack.pop().unwrap().as_u32(); let a = stack.pop().unwrap().as_u32(); stack.push(WasmValue::from_u32(a.wrapping_sub(b))); }
575574
0x6c => { let b = stack.pop().unwrap().as_u32(); let a = stack.pop().unwrap().as_u32(); stack.push(WasmValue::from_u32(a.wrapping_mul(b))); }
@@ -599,9 +598,7 @@ impl Instance {
599598
let locals_start = stack.len() - n_params;
600599

601600
// Allocate space for local variables
602-
for _ in 0..locals_count {
603-
stack.push(WasmValue::default());
604-
}
601+
stack.resize(stack.len() + locals_count, WasmValue::default());
605602

606603
// Push return target
607604
control.push(ControlFrame {
@@ -625,6 +622,7 @@ impl Instance {
625622
}
626623

627624

625+
#[inline(always)]
628626
fn call_function_idx(
629627
&self,
630628
idx: usize,
@@ -674,7 +672,8 @@ impl Instance {
674672
ctrl_bases: &mut Vec<usize>
675673
) -> Result<(), Error> {
676674
let bytes = &self.module.bytes;
677-
let mem_opt = self.memory.as_ref();
675+
let mem = self.memory.as_ref();
676+
let tab = self.table.as_ref();
678677

679678
macro_rules! next_op { () => {{ let byte = bytes[pc]; pc += 1; byte }} }
680679
macro_rules! pop_val { () => {{
@@ -881,7 +880,7 @@ impl Instance {
881880
let _align: u32 = read_leb128(bytes, &mut pc)?;
882881
let offset: u32 = read_leb128(bytes, &mut pc)?;
883882
let addr = pop_val!().as_u32();
884-
let mem = mem_opt.ok_or_else(|| Error::Validation(UNKNOWN_MEMORY))?;
883+
let mem = mem.ok_or_else(|| Error::Validation(UNKNOWN_MEMORY))?;
885884
let v = mem.borrow().$method(addr, offset).map_err(Error::Trap)?;
886885
let val = ($push)(v);
887886
stack.push(val);
@@ -892,7 +891,7 @@ impl Instance {
892891
let raw = pop_val!();
893892
let addr = pop_val!().as_u32();
894893
let val = ($from)(raw);
895-
let mem = mem_opt.ok_or_else(|| Error::Validation(UNKNOWN_MEMORY))?;
894+
let mem = mem.ok_or_else(|| Error::Validation(UNKNOWN_MEMORY))?;
896895
mem.borrow_mut().$method(addr, offset, val).map_err(Error::Trap)?;
897896
}}}
898897

@@ -1047,8 +1046,8 @@ impl Instance {
10471046
Some(v) => v.as_u32(),
10481047
None => return Err(Error::Trap(STACK_UNDERFLOW))
10491048
};
1050-
let table_rc = match self.table.as_ref() {
1051-
Some(t) => t.clone(),
1049+
let table_rc = match tab {
1050+
Some(t) => t,
10521051
None => return Err(Error::Trap(UNDEF_ELEM))
10531052
};
10541053
let func_ref = {
@@ -1199,7 +1198,7 @@ impl Instance {
11991198
if gi as usize >= self.globals.len() {
12001199
return Err(Error::Trap(UNKNOWN_GLOBAL));
12011200
}
1202-
stack.push(self.globals[gi as usize].borrow().value.get());
1201+
stack.push(self.globals[gi as usize].value.get());
12031202
}
12041203
0x24 => { // global.set
12051204
let gi: u32 = read_leb128(bytes, &mut pc)?;
@@ -1210,7 +1209,7 @@ impl Instance {
12101209
Some(v) => v,
12111210
None => return Err(Error::Trap(STACK_UNDERFLOW))
12121211
};
1213-
self.globals[gi as usize].borrow().value.set(val);
1212+
self.globals[gi as usize].value.set(val);
12141213
}
12151214
// Memory instructions - loads
12161215
0x28 => { load!(load_u32, |v: u32| WasmValue::from_u32(v)); }
@@ -1240,7 +1239,8 @@ impl Instance {
12401239
// Memory instructions - size/grow
12411240
0x3f => { // memory.size
12421241
pc += 1; // Skip zero flag
1243-
let mem = self.memory.as_ref().ok_or(Error::Validation(UNKNOWN_MEMORY))?;
1242+
let mem = mem
1243+
.ok_or(Error::Validation(UNKNOWN_MEMORY))?;
12441244
stack.push(WasmValue::from_u32(mem.borrow().size()));
12451245
}
12461246
0x40 => { // memory.grow
@@ -1249,7 +1249,8 @@ impl Instance {
12491249
Some(v) => v.as_u32(),
12501250
None => return Err(Error::Trap(STACK_UNDERFLOW))
12511251
};
1252-
let mem = self.memory.as_ref().ok_or(Error::Validation(UNKNOWN_MEMORY))?;
1252+
let mem = mem
1253+
.ok_or(Error::Validation(UNKNOWN_MEMORY))?;
12531254
let old = mem.borrow_mut().grow(delta);
12541255
stack.push(WasmValue::from_u32(old));
12551256
}

tests/spec_tests.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,18 @@ fn spectest_exports() -> HashMap<String, ExportValue> {
104104

105105
let mut exports = HashMap::new();
106106

107-
exports.insert("global_i32".into(), ExportValue::Global(Rc::new(RefCell::new(
107+
exports.insert("global_i32".into(), ExportValue::Global(Rc::new(
108108
WasmGlobal { ty: ValType::I32, mutable: false, value: Cell::new(WasmValue::from_u32(666)) }
109-
))));
110-
exports.insert("global_i64".into(), ExportValue::Global(Rc::new(RefCell::new(
109+
)));
110+
exports.insert("global_i64".into(), ExportValue::Global(Rc::new(
111111
WasmGlobal { ty: ValType::I64, mutable: false, value: Cell::new(WasmValue::from_u64(666)) }
112-
))));
113-
exports.insert("global_f32".into(), ExportValue::Global(Rc::new(RefCell::new(
112+
)));
113+
exports.insert("global_f32".into(), ExportValue::Global(Rc::new(
114114
WasmGlobal { ty: ValType::F32, mutable: false, value: Cell::new(WasmValue::from_f32(666.6)) }
115-
))));
116-
exports.insert("global_f64".into(), ExportValue::Global(Rc::new(RefCell::new(
115+
)));
116+
exports.insert("global_f64".into(), ExportValue::Global(Rc::new(
117117
WasmGlobal { ty: ValType::F64, mutable: false, value: Cell::new(WasmValue::from_f64(666.6)) }
118-
))));
118+
)));
119119

120120
exports.insert("table".into(), ExportValue::Table(Rc::new(RefCell::new(
121121
WasmTable::new(10, 20)
@@ -155,7 +155,7 @@ fn exec_action(instances: &HashMap<String, Rc<Instance>>, action: &Act) -> Resul
155155

156156
match action {
157157
Act::Get { .. } => match export {
158-
ExportValue::Global(g) => Ok(vec![g.borrow().value.get()]),
158+
ExportValue::Global(g) => Ok(vec![g.value.get()]),
159159
_ => Err(Error::Trap("not a global"))
160160
},
161161
Act::Invoke { .. } => match export {

0 commit comments

Comments
 (0)