Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(compiler): Avoid round trip between string & bytes when reading cmi #1926

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading