Skip to content
This repository was archived by the owner on Jan 4, 2026. It is now read-only.

Commit 447e286

Browse files
feat: Refactored instruction execution for increased performance (#11)
1 parent 6b4ebdd commit 447e286

15 files changed

Lines changed: 332 additions & 228 deletions

File tree

benches/fib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn fibonacci(input: i64) -> i64 {
6363

6464
let mut vm = Vm::default();
6565
vm.load_program(program.build());
66-
vm.run(None).unwrap().result.unwrap().into()
66+
vm.run(None, None).unwrap().result.unwrap().into()
6767
}
6868

6969
fn bench_fibonacci(c: &mut Criterion) {

benches/functioncall.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn test_function_call() {
3737

3838
let mut vm = Vm::default();
3939
vm.load_program(program.build());
40-
let _ = vm.run(None);
40+
let _ = vm.run(None, None);
4141
}
4242

4343
fn bench_fibonacci(c: &mut Criterion) {

benches/loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn loopy(input: i64) -> i64 {
3737

3838
let mut vm = Vm::default();
3939
vm.load_program(program.build());
40-
vm.run(None).unwrap().result.unwrap().into()
40+
vm.run(None, None).unwrap().result.unwrap().into()
4141
}
4242

4343
fn bench_fibonacci(c: &mut Criterion) {

examples/fib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
use log::LevelFilter;
2+
use simplelog::{ColorChoice, Config, TermLogger, TerminalMode};
13
use bytevm::prelude::{BlockEncoder, FunctionBuilder, ProgramBuilder, Vm};
24

35
fn main() {
46

7+
TermLogger::init(LevelFilter::Info, Config::default(), TerminalMode::Mixed, ColorChoice::Auto).expect("Logger error");
8+
59
let input = 35;
610

711
let mut program = ProgramBuilder::default();
@@ -61,9 +65,11 @@ fn main() {
6165
.build()
6266
);
6367

68+
program.optimize();
69+
6470
let mut vm = Vm::default();
6571
vm.load_program(program.build());
66-
let result = vm.run(None).unwrap();
72+
let result = vm.run(None, None).unwrap();
6773

6874
println!("Time taken: {:?}", result.run_time.as_secs_f64());
6975

examples/math.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use log::LevelFilter;
2+
use simplelog::{ColorChoice, Config, TermLogger, TerminalMode};
3+
use bytevm::prelude::{BlockEncoder, FunctionBuilder, ProgramBuilder, Vm};
4+
5+
fn main() {
6+
7+
TermLogger::init(LevelFilter::Trace, Config::default(), TerminalMode::Mixed, ColorChoice::Auto).expect("Logger error");
8+
9+
10+
let mut program = ProgramBuilder::default();
11+
12+
program.add_function(FunctionBuilder::default()
13+
.name("main")
14+
.arity(0)
15+
.body(
16+
BlockEncoder::default()
17+
// Call the add function with 1 and 2
18+
.push_integer(1)
19+
.push_integer(2)
20+
.call_function_by_name("add")
21+
// Return the result
22+
.return_value()
23+
)
24+
.build()
25+
);
26+
27+
program.add_function(FunctionBuilder::default()
28+
.name("add")
29+
.arity(2)
30+
.body(
31+
BlockEncoder::default()
32+
.declare_local("a")
33+
.declare_local("b")
34+
.declare_local("result")
35+
.get_local("a")
36+
.get_local("b")
37+
.add()
38+
.set_local("result")
39+
.get_local("result")
40+
.return_value()
41+
)
42+
.build()
43+
);
44+
45+
program.optimize();
46+
47+
let mut vm = Vm::default();
48+
vm.load_program(program.build());
49+
let result = vm.run(None, None).unwrap();
50+
51+
println!("Time taken: {:?}", result.run_time.as_secs_f64());
52+
53+
}

src/builder.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,25 @@ impl ProgramBuilder {
2828
pub fn add_symbol(&mut self, name: String, entry: SymbolEntry) {
2929
self.program.symbol_table.insert(name, entry);
3030
}
31-
32-
pub fn build(mut self) -> Program {
31+
32+
pub fn optimize(&mut self) {
3333

3434
// Resolve function references with function index
3535
for function in &mut self.program.functions {
3636
for instruction in &mut function.instructions {
3737
if let Instruction::FunctionCall(CallTarget::Name(name)) = instruction {
3838
if let Some(SymbolEntry::UserDefinedFunction { index }) = self.program.symbol_table.get(name) {
3939
*instruction = Instruction::FunctionCall(CallTarget::Index(*index));
40+
} else {
41+
panic!("Function {} not found", name);
4042
}
4143
}
4244
}
4345
}
46+
47+
}
4448

49+
pub fn build(self) -> Program {
4550
self.program
4651
}
4752
}
@@ -321,7 +326,7 @@ impl BlockEncoder {
321326
pub fn return_value(&mut self) -> &mut Self {
322327
self.push(Instruction::Return)
323328
}
324-
329+
325330
/// End function execution.
326331
pub fn end_function(&mut self) -> &mut Self {
327332
self.push(Instruction::EndFunction)

0 commit comments

Comments
 (0)