Skip to content

Commit 335354a

Browse files
committed
Optimize VM
1 parent 37ddd86 commit 335354a

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

lib/code.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ let num_opcodes = 30
110110
Uses Obj.magic since OCaml represents simple variants as integers.
111111
INVARIANT: The opcode variant order MUST match to_int mapping.
112112
Only use after bounds checking: 0 <= n < num_opcodes *)
113-
let unsafe_of_int (n : int) : opcode = Obj.magic n
113+
let[@inline] unsafe_of_int (n : int) : opcode = Obj.magic n
114114

115115
let to_definition = function
116116
| OpConstant -> { name = "OpConstant"; operand_widths = [ 2 ] }
@@ -176,13 +176,13 @@ let concat (instrs : bytes list) : instructions =
176176
Buffer.to_bytes buf
177177

178178
(* Read a big-endian uint16 from instructions at offset *)
179-
let read_uint16 (ins : instructions) (offset : int) : int =
180-
let b1 = Char.code (Bytes.get ins offset) in
181-
let b2 = Char.code (Bytes.get ins (offset + 1)) in
179+
let[@inline] read_uint16 (ins : instructions) (offset : int) : int =
180+
let b1 = Char.code (Bytes.unsafe_get ins offset) in
181+
let b2 = Char.code (Bytes.unsafe_get ins (offset + 1)) in
182182
(b1 lsl 8) lor b2
183183

184184
(* Read a uint8 from instructions at offset *)
185-
let read_uint8 (ins : instructions) (offset : int) : int = Char.code (Bytes.get ins offset)
185+
let[@inline] read_uint8 (ins : instructions) (offset : int) : int = Char.code (Bytes.unsafe_get ins offset)
186186

187187
module Test = struct
188188
type test = {

lib/value.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let true_value = Boolean true
3939
let false_value = Boolean false
4040
let null_value = Null
4141

42-
let is_truthy = function
42+
let[@inline] is_truthy = function
4343
| Null -> false
4444
| Boolean b -> b
4545
| _ -> true

lib/vm.ml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ type vm = {
3030
}
3131

3232
(* Get the current frame *)
33-
let current_frame (vm : vm) : frame = vm.frames.(vm.frame_index)
33+
let[@inline] current_frame (vm : vm) : frame = vm.frames.(vm.frame_index)
3434

3535
(* Push a new frame *)
36-
let push_frame (vm : vm) (f : frame) : unit =
36+
let[@inline] push_frame (vm : vm) (f : frame) : unit =
3737
vm.frame_index <- vm.frame_index + 1;
3838
vm.frames.(vm.frame_index) <- f
3939

4040
(* Pop the current frame *)
41-
let pop_frame (vm : vm) : frame =
41+
let[@inline] pop_frame (vm : vm) : frame =
4242
let f = vm.frames.(vm.frame_index) in
4343
vm.frame_index <- vm.frame_index - 1;
4444
f
4545

4646
(* Get current instructions from current frame *)
47-
let current_instructions (vm : vm) : bytes = (current_frame vm).cl.fn.instructions
47+
let[@inline] current_instructions (vm : vm) : bytes = (current_frame vm).cl.fn.instructions
4848

4949
let create (bytecode : Compiler.bytecode) : vm =
5050
(* Wrap the main program in a "main" closure *)
@@ -80,39 +80,39 @@ let create_with_globals (bytecode : Compiler.bytecode) (globals : Value.value ar
8080
sp = 0;
8181
}
8282

83-
let push (vm : vm) (value : Value.value) : (unit, string) result =
83+
let[@inline] push (vm : vm) (value : Value.value) : (unit, string) result =
8484
if vm.sp >= stack_size then
8585
Error "stack overflow"
8686
else (
87-
vm.stack.(vm.sp) <- value;
87+
Array.unsafe_set vm.stack vm.sp value;
8888
vm.sp <- vm.sp + 1;
8989
Ok ())
9090

91-
let pop (vm : vm) : Value.value =
92-
let value = vm.stack.(vm.sp - 1) in
91+
let[@inline] pop (vm : vm) : Value.value =
92+
let value = Array.unsafe_get vm.stack (vm.sp - 1) in
9393
vm.sp <- vm.sp - 1;
9494
value
9595

9696
(* For testing: get the last popped element (still on stack, just below sp) *)
97-
let last_popped_stack_elem (vm : vm) : Value.value = vm.stack.(vm.sp)
97+
let last_popped_stack_elem (vm : vm) : Value.value = Array.unsafe_get vm.stack vm.sp
9898

99-
let execute_binary_integer_op (op : Code.opcode) (l : int64) (r : int64) : int64 =
99+
let[@inline] execute_binary_integer_op (op : Code.opcode) (l : int64) (r : int64) : int64 =
100100
match op with
101101
| Code.OpAdd -> Int64.add l r
102102
| Code.OpSub -> Int64.sub l r
103103
| Code.OpMul -> Int64.mul l r
104104
| Code.OpDiv -> Int64.div l r
105105
| _ -> 0L
106106

107-
let execute_binary_float_op (op : Code.opcode) (l : float) (r : float) : float =
107+
let[@inline] execute_binary_float_op (op : Code.opcode) (l : float) (r : float) : float =
108108
match op with
109109
| Code.OpAdd -> l +. r
110110
| Code.OpSub -> l -. r
111111
| Code.OpMul -> l *. r
112112
| Code.OpDiv -> l /. r
113113
| _ -> 0.0
114114

115-
let execute_binary_op (vm : vm) (op : Code.opcode) : unit =
115+
let[@inline] execute_binary_op (vm : vm) (op : Code.opcode) : unit =
116116
let right = pop vm in
117117
let left = pop vm in
118118
match (left, right) with
@@ -138,7 +138,7 @@ let execute_binary_op (vm : vm) (op : Code.opcode) : unit =
138138
| _ -> ()
139139

140140
(* Execute comparison operations - always returns a boolean *)
141-
let execute_comparison (vm : vm) (op : Code.opcode) : unit =
141+
let[@inline] execute_comparison (vm : vm) (op : Code.opcode) : unit =
142142
let right = pop vm in
143143
let left = pop vm in
144144
let result =
@@ -184,7 +184,7 @@ let run (vm : vm) : (unit, string) result =
184184
let frame = current_frame vm in
185185
let ins = frame.cl.fn.instructions in
186186
let ip = frame.ip in
187-
let op_byte = Char.code (Bytes.get ins ip) in
187+
let op_byte = Char.code (Bytes.unsafe_get ins ip) in
188188

189189
(* Use unsafe_of_int for fast opcode dispatch - we trust the compiler emitted valid opcodes *)
190190
(match Code.unsafe_of_int op_byte with

0 commit comments

Comments
 (0)