Skip to content

Commit

Permalink
chore(compiler): Avoid round trip between string & bytes when reading…
Browse files Browse the repository at this point in the history
… cmi (#1926)

Co-authored-by: Hugo Heuzard <hugo.heuzard@nomadic-labs.com>
  • Loading branch information
phated and Hugo Heuzard committed Feb 22, 2024
1 parent 85d8768 commit aead774
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
16 changes: 12 additions & 4 deletions compiler/src/typed/cmi_format.re
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,21 @@ let input_cmi = ic =>
| Result.Error(e) => raise(Invalid_argument(e))
};

let deserialize_cmi = bytes =>
switch (
cmi_infos_of_yojson @@ Yojson.Safe.from_string(Bytes.to_string(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_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 aead774

Please sign in to comment.