From 1a21ca91c6d3d3c505351a60ed63f6cd5db89383 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Tue, 17 Oct 2023 00:14:22 +0200 Subject: [PATCH 1/2] chore(compiler): Avoid round trip between string and bytes when reading cmi --- compiler/src/typed/cmi_format.re | 13 +++++++++++-- compiler/src/utils/wasm_utils.re | 6 ++---- compiler/src/utils/wasm_utils.rei | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/compiler/src/typed/cmi_format.re b/compiler/src/typed/cmi_format.re index 47f1048b70..76e88c700c 100644 --- a/compiler/src/typed/cmi_format.re +++ b/compiler/src/typed/cmi_format.re @@ -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 = diff --git a/compiler/src/utils/wasm_utils.re b/compiler/src/utils/wasm_utils.re index 4a3693c5fc..4ace57c18e 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..8b0545c3a4 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; }; From aaa41db01e08b33cacd396f4ab85bafb895c1874 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Wed, 21 Feb 2024 20:46:24 -0700 Subject: [PATCH 2/2] fmt --- compiler/src/typed/cmi_format.re | 23 +++++++++++------------ compiler/src/utils/wasm_utils.re | 2 +- compiler/src/utils/wasm_utils.rei | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/compiler/src/typed/cmi_format.re b/compiler/src/typed/cmi_format.re index 76e88c700c..253cfe88e6 100644 --- a/compiler/src/typed/cmi_format.re +++ b/compiler/src/typed/cmi_format.re @@ -102,22 +102,21 @@ let input_cmi = ic => | Result.Error(e) => raise(Invalid_argument(e)) }; -let deserialize_cmi = ic => size => { +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) - ) { + 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 4ace57c18e..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: in_channel => int => t; + let deserialize: (in_channel, int) => t; let accepts_version: abi_version => bool; let serialize: t => bytes; }; diff --git a/compiler/src/utils/wasm_utils.rei b/compiler/src/utils/wasm_utils.rei index 8b0545c3a4..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: in_channel => int => t; + let deserialize: (in_channel, int) => t; let accepts_version: abi_version => bool; let serialize: t => bytes; };