From aead774b312bf3ddf08058800fd818c8d591a35d Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 21 Feb 2024 21:38:49 -0700 Subject: [PATCH] chore(compiler): Avoid round trip between string & bytes when reading cmi (#1926) Co-authored-by: Hugo Heuzard --- compiler/src/typed/cmi_format.re | 16 ++++++++++++---- compiler/src/utils/wasm_utils.re | 6 ++---- compiler/src/utils/wasm_utils.rei | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/compiler/src/typed/cmi_format.re b/compiler/src/typed/cmi_format.re index 47f1048b70..253cfe88e6 100644 --- a/compiler/src/typed/cmi_format.re +++ b/compiler/src/typed/cmi_format.re @@ -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 = ( diff --git a/compiler/src/utils/wasm_utils.re b/compiler/src/utils/wasm_utils.re index 4a3693c5fc..47bcef0b89 100644 --- a/compiler/src/utils/wasm_utils.re +++ b/compiler/src/utils/wasm_utils.re @@ -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; }; @@ -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) }; }; diff --git a/compiler/src/utils/wasm_utils.rei b/compiler/src/utils/wasm_utils.rei index b1a27d29ee..a5f3765df8 100644 --- a/compiler/src/utils/wasm_utils.rei +++ b/compiler/src/utils/wasm_utils.rei @@ -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; };