Skip to content

Commit 3991c07

Browse files
committed
Bytecode parsing: improved performance
1 parent 3405b8c commit 3991c07

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

compiler/parse_bytecode.ml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,16 @@ end
260260
(* Detect each block *)
261261
module Blocks : sig
262262
type t
263-
val add : t -> int -> t
264-
val next : t -> int -> int
265263
val analyse : Debug.data -> code -> t
264+
val add : t -> int -> t
265+
type u
266+
val finish_analysis : t -> u
267+
val next : u -> int -> int
266268
end = struct
267-
type t = AddrSet.t * int
269+
type t = AddrSet.t
270+
type u = int array
268271

269-
let add (blocks,len) pc = AddrSet.add pc blocks,len
272+
let add blocks pc = AddrSet.add pc blocks
270273
let rec scan debug blocks code pc len =
271274
if pc < len then begin
272275
match (get_instr code pc).kind with
@@ -319,14 +322,23 @@ end = struct
319322
end
320323
else blocks
321324

322-
let rec next ((blocks,len) as info) pc =
323-
let pc = pc + 1 in
324-
if pc = len || AddrSet.mem pc blocks then pc else next info pc
325+
let finish_analysis blocks = Array.of_list (AddrSet.elements blocks)
326+
327+
(* invariant: a.(i) <= x < a.(j) *)
328+
let rec find a i j x =
329+
if i + 1 = j then a.(j) else
330+
let k = (i + j) / 2 in
331+
if a.(k) <= x then
332+
find a k j x
333+
else
334+
find a i k x
335+
336+
let next blocks pc = find blocks 0 (Array.length blocks - 1) pc
325337

326338
let analyse debug_data code =
327339
let blocks = AddrSet.empty in
328340
let len = String.length code / 4 in
329-
(scan debug_data blocks code 0 len,len)
341+
add (scan debug_data blocks code 0 len) len
330342

331343
end
332344

@@ -630,7 +642,7 @@ let compiled_blocks = ref AddrMap.empty
630642
let method_cache_id = ref 1
631643

632644
type compile_info =
633-
{ blocks : Blocks.t;
645+
{ blocks : Blocks.u;
634646
code : string;
635647
limit : int;
636648
debug : Debug.data }
@@ -1728,6 +1740,7 @@ let parse_bytecode ~debug code globals debug_data =
17281740
if debug = `Full
17291741
then Debug.fold debug_data (fun pc _ blocks -> Blocks.add blocks pc) blocks
17301742
else blocks in
1743+
let blocks = Blocks.finish_analysis blocks in
17311744
compile_block blocks debug_data code 0 state;
17321745
let blocks =
17331746
AddrMap.mapi

0 commit comments

Comments
 (0)