Debian Codegraph @

src/write.ml

raw symbols SCIP
1 (* Write_ml: writing values to the binary protocol using (mostly) OCaml. *)
2
3 (* Note: the code is this file is carefully written to avoid unnecessary allocations. When
4 touching this code, be sure to run the benchmarks to check for regressions. *)
5
6 open Common
7 include Write_intf.Definitions
8
9 external unsafe_set : buf -> int -> char -> unit = "%caml_ba_unsafe_set_1"
10 external unsafe_set8 : buf -> int -> int -> unit = "%caml_ba_unsafe_set_1"
11 external unsafe_set16 : buf -> int -> int -> unit = "%caml_bigstring_set16u"
12 external unsafe_set32 : buf -> int -> int32 -> unit = "%caml_bigstring_set32u"
13 external unsafe_set64 : buf -> int -> int64 -> unit = "%caml_bigstring_set64u"
14 external bswap16 : (int[@local_opt]) -> (int[@local_opt]) = "%bswap16"
15 external bswap32 : (int32[@local_opt]) -> (int32[@local_opt]) = "%bswap_int32"
16 external bswap64 : (int64[@local_opt]) -> (int64[@local_opt]) = "%bswap_int64"
17
18 (*$ open Bin_prot_cinaps $*)
19
20 let code_NEG_INT8 = (*$ Code.char NEG_INT8 *) '\xff' (*$*)
21 let code_INT16 = (*$ Code.char INT16 *) '\xfe' (*$*)
22 let code_INT32 = (*$ Code.char INT32 *) '\xfd' (*$*)
23 let code_INT64 = (*$ Code.char INT64 *) '\xfc' (*$*)
24 let arch_sixtyfour = Sys.word_size = 64
25 let arch_big_endian = Sys.big_endian
26
27 let unsafe_set16be =
28 if arch_big_endian
29 then unsafe_set16
30 else fun buf pos x -> unsafe_set16 buf pos (bswap16 x)
31 ;;
32
33 let unsafe_set32be =
34 if arch_big_endian
35 then unsafe_set32
36 else fun buf pos x -> unsafe_set32 buf pos (bswap32 x)
37 ;;
38
39 let unsafe_set64be =
40 if arch_big_endian
41 then unsafe_set64
42 else fun buf pos x -> unsafe_set64 buf pos (bswap64 x)
43 ;;
44
45 let unsafe_set16le =
46 if arch_big_endian
47 then fun buf pos x -> unsafe_set16 buf pos (bswap16 x)
48 else unsafe_set16
49 ;;
50
51 let unsafe_set32le =
52 if arch_big_endian
53 then fun buf pos x -> unsafe_set32 buf pos (bswap32 x)
54 else unsafe_set32
55 ;;
56
57 let unsafe_set64le =
58 if arch_big_endian
59 then fun buf pos x -> unsafe_set64 buf pos (bswap64 x)
60 else unsafe_set64
61 ;;
62
63 let bin_write_unit buf ~pos () =
64 assert_pos pos;
65 check_pos buf pos;
66 unsafe_set buf pos '\000';
67 pos + 1
68 ;;
69
70 let bin_write_bool buf ~pos b =
71 assert_pos pos;
72 check_pos buf pos;
73 unsafe_set buf pos (if b then '\001' else '\000');
74 pos + 1
75 ;;
76
77 let all_bin_write_small_int buf pos n =
78 check_pos buf pos;
79 unsafe_set8 buf pos n;
80 pos + 1
81 ;;
82
83 let all_bin_write_neg_int8 buf pos n =
84 let next = pos + 2 in
85 check_next buf next;
86 unsafe_set buf pos code_NEG_INT8;
87 unsafe_set8 buf (pos + 1) n;
88 next
89 ;;
90
91 let all_bin_write_int16 buf pos n =
92 let next = pos + 3 in
93 check_next buf next;
94 unsafe_set buf pos code_INT16;
95 unsafe_set16le buf (pos + 1) n;
96 next
97 ;;
98
99 let all_bin_write_int32 buf pos n =
100 let next = pos + 5 in
101 check_next buf next;
102 unsafe_set buf pos code_INT32;
103 unsafe_set32le buf (pos + 1) n;
104 next
105 [@@inline]
106 ;;
107
108 let all_bin_write_int64 buf pos n =
109 let next = pos + 9 in
110 check_next buf next;
111 unsafe_set buf pos code_INT64;
112 unsafe_set64le buf (pos + 1) n;
113 next
114 [@@inline]
115 ;;
116
117 let bin_write_char buf ~pos c =
118 assert_pos pos;
119 check_pos buf pos;
120 unsafe_set buf pos c;
121 pos + 1
122 ;;
123
124 let bin_write_int buf ~pos n =
125 assert_pos pos;
126 if n >= 0
127 then
128 if n < 0x00000080
129 then all_bin_write_small_int buf pos n
130 else if n < 0x00008000
131 then all_bin_write_int16 buf pos n
132 else if arch_sixtyfour && n >= 1 lsl 31
133 then all_bin_write_int64 buf pos (Int64.of_int n)
134 else all_bin_write_int32 buf pos (Int32.of_int n)
135 else if n >= -0x00000080
136 then all_bin_write_neg_int8 buf pos n
137 else if n >= -0x00008000
138 then all_bin_write_int16 buf pos n
139 else if arch_sixtyfour && n < -(1 lsl 31)
140 then all_bin_write_int64 buf pos (Int64.of_int n)
141 else all_bin_write_int32 buf pos (Int32.of_int n)
142 ;;
143
144 let bin_write_nat0 buf ~pos nat0 =
145 assert_pos pos;
146 let n = (nat0 : Nat0.t :> int) in
147 if n < 0x00000080
148 then all_bin_write_small_int buf pos n
149 else if n < 0x00010000
150 then all_bin_write_int16 buf pos n
151 else if arch_sixtyfour && n >= 1 lsl 32
152 then all_bin_write_int64 buf pos (Int64.of_int n)
153 else all_bin_write_int32 buf pos (Int32.of_int n)
154 ;;
155
156 let bin_write_string buf ~pos str =
157 let len = Base.String.length str in
158 let plen = Nat0.unsafe_of_int len in
159 let new_pos = bin_write_nat0 buf ~pos plen in
160 let next = new_pos + len in
161 check_next buf next;
162 (* TODO: optimize for small strings *)
163 unsafe_blit_string_buf ~src_pos:0 str ~dst_pos:new_pos buf ~len;
164 next
165 ;;
166
167 let bin_write_bytes buf ~pos str =
168 let len = Base.Bytes.length str in
169 let plen = Nat0.unsafe_of_int len in
170 let new_pos = bin_write_nat0 buf ~pos plen in
171 let next = new_pos + len in
172 check_next buf next;
173 (* TODO: optimize for small bytes *)
174 unsafe_blit_bytes_buf ~src_pos:0 str ~dst_pos:new_pos buf ~len;
175 next
176 ;;
177
178 let bin_write_float buf ~pos x =
179 assert_pos pos;
180 let next = pos + 8 in
181 check_next buf next;
182 unsafe_set64le buf pos (Base.Int64.bits_of_float x);
183 next
184 [@@inline]
185 ;;
186
187 let bin_write_int32 =
188 if arch_sixtyfour
189 then fun [@inline] buf ~pos n -> bin_write_int buf ~pos (Int32.to_int n)
190 else
191 fun [@inline] buf ~pos n ->
192 if n >= 0x00008000l || n < -0x00008000l
193 then (
194 assert_pos pos;
195 all_bin_write_int32 buf pos n)
196 else bin_write_int buf ~pos (Int32.to_int n)
197 ;;
198
199 let bin_write_int64 buf ~pos n =
200 if n >= 0x80000000L || n < -0x80000000L
201 then (
202 assert_pos pos;
203 all_bin_write_int64 buf pos n)
204 else if arch_sixtyfour
205 then bin_write_int buf ~pos (Int64.to_int n)
206 else if n >= 0x00008000L || n < -0x00008000L
207 then (
208 assert_pos pos;
209 all_bin_write_int32 buf pos (Base.Int64.to_int32_trunc n) [@nontail])
210 else bin_write_int buf ~pos (Int64.to_int n)
211 [@@inline]
212 ;;
213
214 let bin_write_nativeint buf ~pos n =
215 if arch_sixtyfour
216 && (n >= (* 0x80000000n *) Nativeint.shift_left 1n 31
217 || n < (* -0x80000000n *) Nativeint.neg (Nativeint.shift_left 1n 31))
218 then (
219 assert_pos pos;
220 all_bin_write_int64 buf pos (Base.Int64.of_nativeint n) [@nontail])
221 else if ((not arch_sixtyfour) && n >= 0x8000n) || n < -0x8000n
222 then (
223 assert_pos pos;
224 all_bin_write_int32 buf pos (Base.Nativeint.to_int32_trunc n) [@nontail])
225 else bin_write_int buf ~pos (Nativeint.to_int n)
226 [@@inline]
227 ;;
228
229 let bin_write_ref bin_write_el buf ~pos r = bin_write_el buf ~pos !r
230
231 let bin_write_lazy bin_write_el buf ~pos lv =
232 let v = Base.Lazy.force lv in
233 bin_write_el buf ~pos v
234 ;;
235
236 let bin_write_option bin_write_el buf ~pos = function
237 | None -> bin_write_bool buf ~pos false
238 | Some v ->
239 let next = bin_write_bool buf ~pos true in
240 bin_write_el buf ~pos:next v
241 ;;
242
243 let bin_write_pair bin_write_a bin_write_b buf ~pos (a, b) =
244 let next = bin_write_a buf ~pos a in
245 bin_write_b buf ~pos:next b
246 ;;
247
248 let bin_write_triple bin_write_a bin_write_b bin_write_c buf ~pos (a, b, c) =
249 let next1 = bin_write_a buf ~pos a in
250 let next2 = bin_write_b buf ~pos:next1 b in
251 bin_write_c buf ~pos:next2 c
252 ;;
253
254 let bin_write_list =
255 let rec loop ~bin_write_el ~buf ~els_pos lst =
256 match lst with
257 | [] -> els_pos
258 | hd :: tl ->
259 let new_els_pos = bin_write_el buf ~pos:els_pos hd in
260 loop ~bin_write_el ~buf ~els_pos:new_els_pos tl
261 in
262 fun bin_write_el buf ~pos lst ->
263 let len = Nat0.unsafe_of_int (List.length lst) in
264 let els_pos = bin_write_nat0 buf ~pos len in
265 loop ~bin_write_el ~buf ~els_pos lst
266 ;;
267
268 let[@inline always] bin_write_float_array_gen ~length ~blit buf ~pos a =
269 let len = length a in
270 let plen = Nat0.unsafe_of_int len in
271 let pos = bin_write_nat0 buf ~pos plen in
272 let size = len * 8 in
273 let next = pos + size in
274 check_next buf next;
275 blit ~src_pos:0 a ~dst_pos:pos buf ~len;
276 next
277 ;;
278
279 external float_array_length : Float.Array.t -> int = "%floatarray_length"
280
281 let bin_write_floatarray buf ~pos a =
282 bin_write_float_array_gen
283 ~length:float_array_length
284 ~blit:unsafe_blit_floatarray_buf
285 buf
286 ~pos
287 a
288 ;;
289
290 let bin_write_float_array buf ~pos a =
291 bin_write_float_array_gen
292 ~length:Base.Array.length
293 ~blit:unsafe_blit_float_array_buf
294 buf
295 ~pos
296 a
297 ;;
298
299 let bin_write_array_loop bin_write_el buf ~els_pos ~n ar =
300 let els_pos_ref = ref els_pos in
301 for i = 0 to n - 1 do
302 els_pos_ref := bin_write_el buf ~pos:!els_pos_ref (Base.Array.unsafe_get ar i)
303 done;
304 !els_pos_ref
305 ;;
306
307 let bin_write_array (type a) bin_write_el buf ~pos ar =
308 let module Obj = Base.Exported_for_specific_uses.Obj_local in
309 if (Obj.magic (bin_write_el : a writer) : float writer)
310 == (bin_write_float :> float writer)
311 then bin_write_float_array buf ~pos (Obj.magic (ar : a array) : float array) [@nontail]
312 else (
313 let n = Base.Array.length ar in
314 let pn = Nat0.unsafe_of_int n in
315 let els_pos = bin_write_nat0 buf ~pos pn in
316 bin_write_array_loop bin_write_el buf ~els_pos ~n ar)
317 ;;
318
319 let bin_write_hashtbl bin_write_key bin_write_val buf ~pos htbl =
320 let len = Hashtbl.length htbl in
321 let plen = Nat0.unsafe_of_int len in
322 let els_pos = bin_write_nat0 buf ~pos plen in
323 let cnt_ref = ref 0 in
324 let coll_htbl k v els_pos =
325 incr cnt_ref;
326 let new_els_pos = bin_write_key buf ~pos:els_pos k in
327 bin_write_val buf ~pos:new_els_pos v
328 in
329 let res_pos = Hashtbl.fold coll_htbl htbl els_pos in
330 if !cnt_ref <> len then raise_concurrent_modification "bin_write_hashtbl";
331 res_pos
332 ;;
333
334 external buf_of_vec32 : (vec32[@local_opt]) -> (buf[@local_opt]) = "%identity"
335 external buf_of_vec64 : (vec64[@local_opt]) -> (buf[@local_opt]) = "%identity"
336 external buf_of_mat32 : (mat32[@local_opt]) -> (buf[@local_opt]) = "%identity"
337 external buf_of_mat64 : (mat64[@local_opt]) -> (buf[@local_opt]) = "%identity"
338 external array1_dim : ('a, 'b, 'c) Stdlib.Bigarray.Array1.t -> int = "%caml_ba_dim_1"
339 external array2_dim1 : ('a, 'b, 'c) Stdlib.Bigarray.Array2.t -> int = "%caml_ba_dim_1"
340 external array2_dim2 : ('a, 'b, 'c) Stdlib.Bigarray.Array2.t -> int = "%caml_ba_dim_2"
341
342 let bin_write_float32_vec buf ~pos v =
343 let len = array1_dim v in
344 let plen = Nat0.unsafe_of_int len in
345 let pos = bin_write_nat0 buf ~pos plen in
346 let size = len * 4 in
347 let next = pos + size in
348 check_next buf next;
349 unsafe_blit_buf ~src:(buf_of_vec32 v) ~src_pos:0 ~dst:buf ~dst_pos:pos ~len:size;
350 next
351 ;;
352
353 let bin_write_float64_vec buf ~pos v =
354 let len = array1_dim v in
355 let plen = Nat0.unsafe_of_int len in
356 let pos = bin_write_nat0 buf ~pos plen in
357 let size = len * 8 in
358 let next = pos + size in
359 check_next buf next;
360 unsafe_blit_buf ~src:(buf_of_vec64 v) ~src_pos:0 ~dst:buf ~dst_pos:pos ~len:size;
361 next
362 ;;
363
364 let bin_write_vec = bin_write_float64_vec
365
366 let bin_write_float32_mat buf ~pos m =
367 let len1 = array2_dim1 m in
368 let len2 = array2_dim2 m in
369 let pos = bin_write_nat0 buf ~pos (Nat0.unsafe_of_int len1) in
370 let pos = bin_write_nat0 buf ~pos (Nat0.unsafe_of_int len2) in
371 let size = len1 * len2 * 4 in
372 let next = pos + size in
373 check_next buf next;
374 unsafe_blit_buf ~src:(buf_of_mat32 m) ~src_pos:0 ~dst:buf ~dst_pos:pos ~len:size;
375 next
376 ;;
377
378 let bin_write_float64_mat buf ~pos m =
379 let len1 = array2_dim1 m in
380 let len2 = array2_dim2 m in
381 let pos = bin_write_nat0 buf ~pos (Nat0.unsafe_of_int len1) in
382 let pos = bin_write_nat0 buf ~pos (Nat0.unsafe_of_int len2) in
383 let size = len1 * len2 * 8 in
384 let next = pos + size in
385 check_next buf next;
386 unsafe_blit_buf ~src:(buf_of_mat64 m) ~src_pos:0 ~dst:buf ~dst_pos:pos ~len:size;
387 next
388 ;;
389
390 let bin_write_mat = bin_write_float64_mat
391
392 let bin_write_bigstring buf ~pos s =
393 let len = array1_dim s in
394 let plen = Nat0.unsafe_of_int len in
395 let pos = bin_write_nat0 buf ~pos plen in
396 let next = pos + len in
397 check_next buf next;
398 unsafe_blit_buf ~src:s ~src_pos:0 ~dst:buf ~dst_pos:pos ~len;
399 next
400 ;;
401
402 let bin_write_variant_int buf ~pos x =
403 assert_pos pos;
404 let next = pos + 4 in
405 check_next buf next;
406 unsafe_set32le buf pos (Int32.logor (Int32.shift_left (Int32.of_int x) 1) 1l);
407 next
408 ;;
409
410 let bin_write_int_8bit buf ~pos n =
411 assert_pos pos;
412 check_pos buf pos;
413 unsafe_set8 buf pos n;
414 pos + 1
415 ;;
416
417 let bin_write_int_16bit buf ~pos n =
418 assert_pos pos;
419 let next = pos + 2 in
420 check_next buf next;
421 unsafe_set16le buf pos n;
422 next
423 ;;
424
425 let bin_write_int_32bit buf ~pos n =
426 assert_pos pos;
427 let next = pos + 4 in
428 check_next buf next;
429 unsafe_set32le buf pos (Int32.of_int n);
430 next
431 ;;
432
433 let bin_write_int_64bit buf ~pos n =
434 assert_pos pos;
435 let next = pos + 8 in
436 check_next buf next;
437 unsafe_set64le buf pos (Int64.of_int n);
438 next
439 ;;
440
441 let bin_write_int64_bits buf ~pos n =
442 assert_pos pos;
443 let next = pos + 8 in
444 check_next buf next;
445 unsafe_set64le buf pos n;
446 next
447 ;;
448
449 let bin_write_network16_int buf ~pos n =
450 assert_pos pos;
451 let next = pos + 2 in
452 check_next buf next;
453 unsafe_set16be buf pos n;
454 next
455 ;;
456
457 let bin_write_network32_int buf ~pos n =
458 assert_pos pos;
459 let next = pos + 4 in
460 check_next buf next;
461 unsafe_set32be buf pos (Int32.of_int n);
462 next
463 ;;
464
465 let bin_write_network32_int32 buf ~pos n =
466 assert_pos pos;
467 let next = pos + 4 in
468 check_next buf next;
469 unsafe_set32be buf pos n;
470 next
471 ;;
472
473 let bin_write_network64_int buf ~pos n =
474 assert_pos pos;
475 let next = pos + 8 in
476 check_next buf next;
477 unsafe_set64be buf pos (Int64.of_int n);
478 next
479 ;;
480
481 let bin_write_network64_int64 buf ~pos n =
482 assert_pos pos;
483 let next = pos + 8 in
484 check_next buf next;
485 unsafe_set64be buf pos n;
486 next
487 ;;
488
489 external unsafe_string_get32
490 : (string[@local_opt])
491 -> int
492 -> (int32[@local_opt])
493 = "%caml_string_get32u"
494
495 external unsafe_string_get64
496 : (string[@local_opt])
497 -> int
498 -> (int64[@local_opt])
499 = "%caml_string_get64u"
500
501 let bin_write_md5 buf ~pos x =
502 let x = Md5_lib.to_binary_local x in
503 assert (Base.String.length x = 16);
504 assert_pos pos;
505 let next = pos + 16 in
506 check_next buf next;
507 if arch_sixtyfour
508 then (
509 let a = unsafe_string_get64 x 0 in
510 let b = unsafe_string_get64 x 8 in
511 unsafe_set64 buf pos a;
512 unsafe_set64 buf (pos + 8) b)
513 else (
514 let a = unsafe_string_get32 x 0 in
515 let b = unsafe_string_get32 x 4 in
516 let c = unsafe_string_get32 x 8 in
517 let d = unsafe_string_get32 x 12 in
518 unsafe_set32 buf pos a;
519 unsafe_set32 buf (pos + 4) b;
520 unsafe_set32 buf (pos + 8) c;
521 unsafe_set32 buf (pos + 12) d);
522 next
523 ;;
524
525 (* Local versions *)
526
527 let bin_write_unit__local = bin_write_unit
528 let bin_write_bool__local = bin_write_bool
529 let bin_write_string__local = bin_write_string
530 let bin_write_bytes__local = bin_write_bytes
531 let bin_write_char__local = bin_write_char
532 let bin_write_int__local = bin_write_int
533 let bin_write_nat0__local = bin_write_nat0
534 let bin_write_float__local = bin_write_float
535 let bin_write_int32__local = bin_write_int32
536 let bin_write_int64__local = bin_write_int64
537 let bin_write_nativeint__local = bin_write_nativeint
538 let bin_write_ref__local = bin_write_ref
539 let bin_write_lazy__local = bin_write_lazy
540
541 let bin_write_option__local bin_write_el buf ~pos = function
542 | None -> bin_write_bool buf ~pos false
543 | Some v ->
544 let next = bin_write_bool buf ~pos true in
545 bin_write_el buf ~pos:next v
546 ;;
547
548 let bin_write_pair__local bin_write_a bin_write_b buf ~pos (a, b) =
549 let next = bin_write_a buf ~pos a in
550 bin_write_b buf ~pos:next b
551 ;;
552
553 let bin_write_triple__local bin_write_a bin_write_b bin_write_c buf ~pos (a, b, c) =
554 let next1 = bin_write_a buf ~pos a in
555 let next2 = bin_write_b buf ~pos:next1 b in
556 bin_write_c buf ~pos:next2 c
557 ;;
558
559 let bin_write_list__local =
560 let rec loop ~bin_write_el ~buf ~els_pos lst =
561 match lst with
562 | [] -> els_pos
563 | hd :: tl ->
564 let new_els_pos = bin_write_el buf ~pos:els_pos hd in
565 loop ~bin_write_el ~buf ~els_pos:new_els_pos tl
566 in
567 fun bin_write_el buf ~pos lst ->
568 let len = Nat0.unsafe_of_int (Base.List.length lst) in
569 let els_pos = bin_write_nat0 buf ~pos len in
570 loop ~bin_write_el ~buf ~els_pos lst
571 ;;
572
573 let bin_write_array__local = bin_write_array
574 let bin_write_float32_vec__local = bin_write_float32_vec
575 let bin_write_float64_vec__local = bin_write_float64_vec
576 let bin_write_vec__local = bin_write_vec
577 let bin_write_float32_mat__local = bin_write_float32_mat
578 let bin_write_float64_mat__local = bin_write_float64_mat
579 let bin_write_mat__local = bin_write_mat
580 let bin_write_bigstring__local = bin_write_bigstring
581 let bin_write_floatarray__local = bin_write_floatarray
582 let bin_write_md5__local = bin_write_md5
583 let bin_write_variant_int__local = bin_write_variant_int
584 let bin_write_int_8bit__local = bin_write_int_8bit
585 let bin_write_int_16bit__local = bin_write_int_16bit
586 let bin_write_int_32bit__local = bin_write_int_32bit
587 let bin_write_int_64bit__local = bin_write_int_64bit
588 let bin_write_int64_bits__local = bin_write_int64_bits
589 let bin_write_network16_int__local = bin_write_network16_int
590 let bin_write_network32_int__local = bin_write_network32_int
591 let bin_write_network32_int32__local = bin_write_network32_int32
592 let bin_write_network64_int__local = bin_write_network64_int
593 let bin_write_network64_int64__local = bin_write_network64_int64