Skip to content

Commit

Permalink
chore(compiler): Avoid round trip between string and bytes when readi…
Browse files Browse the repository at this point in the history
…ng cmi
  • Loading branch information
Hugo Heuzard authored and phated committed Oct 30, 2023
1 parent a4016f1 commit 35bdd06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
13 changes: 11 additions & 2 deletions compiler/src/typed/cmi_format.re
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,21 @@ let input_cmi = ic =>
| Result.Error(e) => raise(Invalid_argument(e))
};

let deserialize_cmi = bytes =>
let deserialize_cmi = ic => size => {
let size = ref(size);
let lexbuf = Lexing.from_function (buf => n => {
let n = min (n, size^);
let read = input(ic, buf, 0, n);
size := size^ - read;
read
});
let state = Yojson.init_lexer ();
switch (
cmi_infos_of_yojson @@ Yojson.Safe.from_string(Bytes.to_string(bytes))
cmi_infos_of_yojson @@ Yojson.Safe.from_lexbuf(state, lexbuf)
) {
| Result.Ok(x) => x
| Result.Error(e) => raise(Invalid_argument(e))
}
};

let serialize_cmi =
Expand Down
6 changes: 2 additions & 4 deletions compiler/src/utils/wasm_utils.re
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ module type BinarySectionSpec = {
type t;

let name: string;
let deserialize: bytes => t;
let deserialize: in_channel => int => t;
let accepts_version: abi_version => bool;
let serialize: t => bytes;
};
Expand Down Expand Up @@ -454,9 +454,7 @@ module BinarySection =
when name == Spec.name && Spec.accepts_version(abi_version) =>
/* Now we're at the start of the section. Time to read */
let realsize = size - (pos_in(inchan) - offset);
let bytes = Bytes.create(realsize);
really_input(inchan, bytes, 0, realsize);
Some(Spec.deserialize(bytes));
Some(Spec.deserialize(inchan, realsize));
| _ => process(tl)
};
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/utils/wasm_utils.rei
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module type BinarySectionSpec = {
type t;

let name: string;
let deserialize: bytes => t;
let deserialize: in_channel => int => t;
let accepts_version: abi_version => bool;
let serialize: t => bytes;
};
Expand Down

0 comments on commit 35bdd06

Please sign in to comment.