Skip to content

Commit 090bc99

Browse files
authored
Merge pull request #1521 from N1ark/item-name-maps
Improve Charon-ML performance
2 parents 9e95cf3 + 4b007bd commit 090bc99

9 files changed

Lines changed: 122 additions & 80 deletions

File tree

‎charon-ml/src/CharonVersion.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(* This is an automatically generated file, generated from `charon/Cargo.toml`. *)
22
(* To re-generate this file, rune `make` in the root directory *)
3-
let supported_charon_version = "0.1.291"
3+
let supported_charon_version = "0.1.292"

‎charon-ml/src/GAst.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type crate = {
3434
name : string;
3535
options : cli_options;
3636
target_information : (string * target_info) list;
37+
runtime_checks : runtime_checks;
3738
item_names : (item_id * name) list;
3839
assoc_item_names : assoc_item_names TraitDeclId.Map.t;
3940
short_names : (item_id * name) list;

‎charon-ml/src/LlbcAstUtils.ml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class ['self] map_crate =
147147
name;
148148
options;
149149
target_information;
150+
runtime_checks;
150151
item_names;
151152
declarations;
152153
type_decls;
@@ -186,6 +187,7 @@ class ['self] map_crate =
186187
name;
187188
options;
188189
target_information;
190+
runtime_checks;
189191
item_names;
190192
declarations;
191193
type_decls;
@@ -277,6 +279,7 @@ class ['self] iter_crate =
277279
name;
278280
options;
279281
target_information;
282+
runtime_checks = _;
280283
item_names = _;
281284
declarations;
282285
type_decls;

‎charon-ml/src/OfJson.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ let crate_of_json (js : json) : (crate, string) result =
4040
name = crate.crate_name;
4141
options = crate.options;
4242
target_information = crate.target_information;
43+
runtime_checks = crate.runtime_checks;
4344
item_names = crate.item_names;
4445
assoc_item_names = crate.assoc_item_names;
4546
short_names = crate.short_names;

‎charon-ml/src/OfPostcard.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ let crate_of_postcard_file (file : string) : (GAst.crate, string) result =
3232
name = crate.crate_name;
3333
options = crate.options;
3434
target_information = crate.target_information;
35+
runtime_checks = crate.runtime_checks;
3536
item_names = crate.item_names;
3637
assoc_item_names = crate.assoc_item_names;
3738
short_names = crate.short_names;

‎charon-ml/src/OfPostcardBasic.ml‎

Lines changed: 111 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
(** Basic utilities for postcard deserialization. *)
22

3-
type postcard_state = { ic : in_channel }
3+
(** We use our own buffer rather than moving the [in_channel], which is more
4+
performant. *)
5+
type postcard_state = {
6+
ic : in_channel;
7+
buf : Bytes.t;
8+
mutable buf_pos : int; (** The next byte to read in [buf]. *)
9+
mutable buf_len : int; (** The number of bytes read into [buf]. *)
10+
mutable buf_ofs : int; (** The position in the input of [buf]'s start. *)
11+
}
12+
13+
let state_of_channel (ic : in_channel) : postcard_state =
14+
{
15+
ic;
16+
buf = Bytes.create 65536;
17+
buf_pos = 0;
18+
buf_len = 0;
19+
buf_ofs = pos_in ic;
20+
}
21+
22+
(** The position of the next byte to read in the input. *)
23+
let[@inline] pos (st : postcard_state) : int = st.buf_ofs + st.buf_pos
24+
25+
(** Moves to the given position of the input. *)
26+
let[@inline] seek (st : postcard_state) (pos : int) : unit =
27+
seek_in st.ic pos;
28+
st.buf_pos <- 0;
29+
st.buf_len <- 0;
30+
st.buf_ofs <- pos
31+
32+
(** Reads more input into the buffer; [false] if the end of the input is
33+
reached. *)
34+
let[@inline] refill (st : postcard_state) : bool =
35+
st.buf_ofs <- st.buf_ofs + st.buf_len;
36+
st.buf_pos <- 0;
37+
st.buf_len <- input st.ic st.buf 0 (Bytes.length st.buf);
38+
st.buf_len > 0
39+
440
type input_format_hint = Json | Postcard | Empty
541

642
(** Attempts detecting the format of a string, which corresponds to the first N
@@ -30,12 +66,12 @@ let format_hint_of_file (file : string) : (input_format_hint, string) result =
3066
(** Same as [format_hint_of_file], but operates on an already-opened in_channel
3167
and restores the position after reading the sample. *)
3268
let format_hint_of_postcard (st : postcard_state) : input_format_hint =
33-
let pos = pos_in st.ic in
34-
seek_in st.ic 0;
69+
let pos = pos st in
70+
seek st 0;
3571
let sample_len = min 64 (in_channel_length st.ic) in
3672
let sample = really_input_string st.ic sample_len in
3773
let hint = format_hint_of_string sample in
38-
seek_in st.ic pos;
74+
seek st pos;
3975
hint
4076

4177
let ( let* ) o f =
@@ -45,71 +81,85 @@ let ( let* ) o f =
4581

4682
let with_state_of_file (file : string)
4783
(f : postcard_state -> ('a, string) result) : ('a, string) result =
48-
try In_channel.with_open_bin file (fun ic -> f { ic })
84+
try In_channel.with_open_bin file (fun ic -> f (state_of_channel ic))
4985
with Sys_error e -> Error e
5086

5187
let combine_error_msgs (st : postcard_state) (msg : string)
5288
(res : ('a, string) result) : ('a, string) result =
5389
match res with
5490
| Ok x -> Ok x
5591
| Error e ->
56-
let rem = in_channel_length st.ic - pos_in st.ic in
92+
let pos = pos st in
93+
let rem = in_channel_length st.ic - pos in
5794
let n = if rem < 8 then rem else 8 in
58-
let pos = pos_in st.ic in
95+
seek st pos;
5996
let preview =
60-
List.init n (fun i -> try input_byte st.ic with End_of_file -> 0)
97+
List.init n (fun _ -> try input_byte st.ic with End_of_file -> 0)
6198
|> List.map (fun b -> Printf.sprintf "%02x" b)
6299
|> String.concat " "
63100
in
101+
seek st pos;
64102
Error
65103
(msg ^ " failed at byte " ^ string_of_int pos ^ " (next: " ^ preview
66104
^ "): " ^ e)
67105

68106
let take_u8 (st : postcard_state) : (int, string) result =
69-
try Ok (input_byte st.ic)
70-
with End_of_file ->
71-
Error "unexpected end of postcard input, expected 1 more byte"
107+
if st.buf_pos < st.buf_len || refill st then (
108+
let b = Bytes.unsafe_get st.buf st.buf_pos in
109+
st.buf_pos <- st.buf_pos + 1;
110+
Ok (Char.code b))
111+
else Error "unexpected end of postcard input, expected 1 more byte"
72112

73113
let take_bytes (st : postcard_state) (len : int) : (bytes, string) result =
74-
try
75-
let bytes = Bytes.create len in
76-
really_input st.ic bytes 0 len;
77-
Ok bytes
78-
with End_of_file ->
79-
Error
80-
("unexpected end of postcard input, expected " ^ string_of_int len
81-
^ " more bytes")
82-
83-
let read_varint_z ~(max_bytes : int) ~(max_last : int) (st : postcard_state) :
84-
(Z.t, string) result =
85-
let rec loop (i : int) (shift : int) (acc : Z.t) : (Z.t, string) result =
114+
let bytes = Bytes.create len in
115+
let rec loop ofs =
116+
if ofs >= len then Ok bytes
117+
else if st.buf_pos < st.buf_len || refill st then (
118+
let n = min (len - ofs) (st.buf_len - st.buf_pos) in
119+
Bytes.blit st.buf st.buf_pos bytes ofs n;
120+
st.buf_pos <- st.buf_pos + n;
121+
loop (ofs + n))
122+
else
123+
Error
124+
("unexpected end of postcard input, expected " ^ string_of_int len
125+
^ " more bytes")
126+
in
127+
loop 0
128+
129+
(** Reads a varint of at most [max_bytes] bytes, whose last byte is at most
130+
[max_last], as an OCaml int of at most [bits] bits (taken as unsigned). *)
131+
let read_varint ~(max_bytes : int) ~(max_last : int) ~(bits : int)
132+
(st : postcard_state) : (int, string) result =
133+
let rec loop (i : int) (shift : int) (acc : int) : (int, string) result =
86134
if i >= max_bytes then Error "invalid postcard varint"
87135
else
88136
let* raw = take_u8 st in
89137
let payload = raw land 0x7F in
90-
let acc = Z.logor acc (Z.shift_left (Z.of_int payload) shift) in
91-
let has_more = raw land 0x80 <> 0 in
92-
if not has_more then
93-
if i = max_bytes - 1 && raw > max_last then
94-
Error "invalid postcard varint terminal byte"
95-
else Ok acc
96-
else loop (i + 1) (shift + 7) acc
138+
if payload <> 0 && (shift >= bits || payload lsr (bits - shift) <> 0) then
139+
Error "integer out of OCaml int range"
140+
else
141+
let acc = acc lor (payload lsl shift) in
142+
let has_more = raw land 0x80 <> 0 in
143+
if not has_more then
144+
if i = max_bytes - 1 && raw > max_last then
145+
Error "invalid postcard varint terminal byte"
146+
else Ok acc
147+
else loop (i + 1) (shift + 7) acc
97148
in
98-
loop 0 0 Z.zero
149+
loop 0 0 0
99150

100-
let z_to_int (z : Z.t) : (int, string) result =
101-
if Z.geq z Z.zero && Z.leq z (Z.of_int max_int) then Ok (Z.to_int z)
102-
else Error "integer out of OCaml int range"
151+
(** Unsigned integers, of at most 62 bits. *)
152+
let read_unsigned ~max_bytes ~max_last st =
153+
read_varint ~max_bytes ~max_last ~bits:62 st
103154

104-
let z_to_i (z : Z.t) : (Z.t, string) result =
105-
let one = Z.one in
106-
if Z.equal (Z.logand z one) Z.zero then Ok (Z.shift_right z 1)
107-
else Ok Z.(neg (add (shift_right z 1) one))
155+
(** Zigzag-encoded signed integers, of at most 63 bits. *)
156+
let read_signed ~max_bytes ~max_last st =
157+
let* z = read_varint ~max_bytes ~max_last ~bits:63 st in
158+
Ok ((z lsr 1) lxor -(z land 1))
108159

109160
let usize_of_postcard (_ctx : 'ctx) (st : postcard_state) : (int, string) result
110161
=
111-
let* z = read_varint_z ~max_bytes:10 ~max_last:1 st in
112-
z_to_int z
162+
read_unsigned ~max_bytes:10 ~max_last:1 st
113163

114164
let bool_of_postcard (_ctx : 'ctx) (st : postcard_state) : (bool, string) result
115165
=
@@ -120,41 +170,29 @@ let bool_of_postcard (_ctx : 'ctx) (st : postcard_state) : (bool, string) result
120170
| _ -> Error "invalid postcard bool"
121171

122172
let int_of_postcard (_ctx : 'ctx) (st : postcard_state) : (int, string) result =
123-
let* z = read_varint_z ~max_bytes:5 ~max_last:15 st in
124-
z_to_int z
173+
read_unsigned ~max_bytes:5 ~max_last:15 st
125174

126175
let u16_of_postcard (_ctx : 'ctx) (st : postcard_state) : (int, string) result =
127-
let* z = read_varint_z ~max_bytes:3 ~max_last:3 st in
128-
z_to_int z
176+
read_unsigned ~max_bytes:3 ~max_last:3 st
129177

130178
let u32_of_postcard (_ctx : 'ctx) (st : postcard_state) : (int, string) result =
131-
let* z = read_varint_z ~max_bytes:5 ~max_last:15 st in
132-
z_to_int z
179+
read_unsigned ~max_bytes:5 ~max_last:15 st
133180

134181
let u64_of_postcard (_ctx : 'ctx) (st : postcard_state) : (int, string) result =
135-
let* z = read_varint_z ~max_bytes:10 ~max_last:1 st in
136-
z_to_int z
182+
read_unsigned ~max_bytes:10 ~max_last:1 st
137183

138184
let i16_of_postcard (_ctx : 'ctx) (st : postcard_state) : (int, string) result =
139-
let* z = read_varint_z ~max_bytes:3 ~max_last:3 st in
140-
let* s = z_to_i z in
141-
z_to_int s
185+
read_signed ~max_bytes:3 ~max_last:3 st
142186

143187
let i32_of_postcard (_ctx : 'ctx) (st : postcard_state) : (int, string) result =
144-
let* z = read_varint_z ~max_bytes:5 ~max_last:15 st in
145-
let* s = z_to_i z in
146-
z_to_int s
188+
read_signed ~max_bytes:5 ~max_last:15 st
147189

148190
let i64_of_postcard (_ctx : 'ctx) (st : postcard_state) : (int, string) result =
149-
let* z = read_varint_z ~max_bytes:10 ~max_last:1 st in
150-
let* s = z_to_i z in
151-
z_to_int s
191+
read_signed ~max_bytes:10 ~max_last:1 st
152192

153193
let isize_of_postcard (_ctx : 'ctx) (st : postcard_state) : (int, string) result
154194
=
155-
let* z = read_varint_z ~max_bytes:10 ~max_last:1 st in
156-
let* s = z_to_i z in
157-
z_to_int s
195+
read_signed ~max_bytes:10 ~max_last:1 st
158196

159197
let u8_of_postcard (_ctx : 'ctx) (st : postcard_state) : (int, string) result =
160198
take_u8 st
@@ -235,20 +273,18 @@ let list_of_postcard
235273
(a_of_postcard : 'ctx -> postcard_state -> ('a, string) result) (ctx : 'ctx)
236274
(st : postcard_state) : ('a list, string) result =
237275
let* len = usize_of_postcard ctx st in
238-
if len > 1_000_000 then
239-
Error
240-
("suspicious postcard sequence length " ^ string_of_int len
241-
^ " (varint read at byte "
242-
^ string_of_int (pos_in st.ic)
243-
^ ")")
244-
else
245-
let rec loop i acc =
246-
if i <= 0 then Ok (List.rev acc)
247-
else
248-
let* x = a_of_postcard ctx st in
249-
loop (i - 1) (x :: acc)
250-
in
251-
loop len []
276+
(* this is a little scary, but we never access the array unless we have
277+
computed all indices, so it's fine. going through an array is more memory
278+
efficient than a list, which we would have to reverse. *)
279+
let arr = Array.make len (Obj.magic 0) in
280+
let rec loop i =
281+
if i == len then Ok (Array.to_list arr)
282+
else
283+
let* x = a_of_postcard ctx st in
284+
Array.set arr i x;
285+
loop (i + 1)
286+
in
287+
loop 0
252288

253289
let option_of_postcard
254290
(a_of_postcard : 'ctx -> postcard_state -> ('a, string) result) (ctx : 'ctx)
@@ -301,7 +337,7 @@ let key_value_pair_of_postcard
301337

302338
let ensure_eof (st : postcard_state) : (unit, string) result =
303339
let len = in_channel_length st.ic in
304-
let pos = pos_in st.ic in
340+
let pos = pos st in
305341
if pos >= len then Ok ()
306342
else
307343
Format.kasprintf Result.error

‎charon/Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎charon/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ tracing = { version = "0.1", features = ["max_level_trace"] }
2727

2828
[package]
2929
name = "charon"
30-
version = "0.1.291"
30+
version = "0.1.292"
3131
authors.workspace = true
3232
edition.workspace = true
3333
license.workspace = true

‎charon/src/ast/visitor.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<K: BodyVisitable + Hash + Eq, T: BodyVisitable> BodyVisitable for SeqHashMa
199199
visitor(drive_body_mut(&mut VisitBodyMut)),
200200
// Types that are ignored when encountered.
201201
skip(
202-
AbortKind, BinOp, BorrowKind, BranchId, AsmOperandId, AsmTemplatePiece, BuiltinAssertKind, ConstantExpr, FieldId,
202+
AbortKind, BinOp, BorrowKind, BranchId, AsmOperandId, AsmTemplatePiece, ConstantExpr, FieldId,
203203
TypeDeclRef, FunDeclId, FunDeclRef, FnPtrKind, GenericArgs, RegionArgs, GlobalDeclRef, IntegerTy, IntTy, UIntTy,
204204
Name, NullOp, RefKind, IntegerValue, Span, Ty, TypeDeclId, UnOp, VariantId,
205205
TraitRef, PolyTraitRef, ScalarTy, Region, RegionId, (), String, PathBuf, bool, u32, usize,
@@ -208,7 +208,7 @@ impl<K: BodyVisitable + Hash + Eq, T: BodyVisitable> BodyVisitable for SeqHashMa
208208
),
209209
// Types that we unconditionally explore.
210210
drive(
211-
Assert, InlineAsm, BorrowckStatement, PlaceKind,
211+
Assert, BuiltinAssertKind, InlineAsm, BorrowckStatement, PlaceKind,
212212
llbc_ast::StatementKind, ullbc_ast::StatementKind,
213213
ullbc_ast::TerminatorKind, SwitchData, SwitchScrutinee,
214214
for<T: BodyVisitable> GExprBody<T>,

0 commit comments

Comments
 (0)