| 1 | open! Core |
| 2 | open! Import |
| 3 | |
| 4 | let require_no_allocation here f = |
| 5 | let ( result |
| 6 | , ({ major_words_allocated; minor_words_allocated } : |
| 7 | Gc.For_testing.Allocation_report.t) ) |
| 8 | = |
| 9 | Gc.For_testing.measure_allocation f |
| 10 | in |
| 11 | if major_words_allocated > 0 || minor_words_allocated > 0 |
| 12 | then |
| 13 | raise_s |
| 14 | [%sexp |
| 15 | "Unexpected allocations" |
| 16 | , (here : Source_code_position.t) |
| 17 | , { major_words_allocated : int; minor_words_allocated : int }]; |
| 18 | result |
| 19 | ;; |
| 20 | |
| 21 | let%expect_test "" = |
| 22 | let buf = Bin_prot.Common.create_buf 10_000 in |
| 23 | let run_test here (bin_t : _ Bin_prot.Type_class.t) l = |
| 24 | let size = |
| 25 | require_no_allocation here (fun () -> |
| 26 | Bin_prot.Size.bin_size_list bin_t.writer.size l) |
| 27 | in |
| 28 | printf "size: %d\n" size; |
| 29 | let pos = |
| 30 | require_no_allocation here (fun () -> |
| 31 | Bin_prot.Write.bin_write_list bin_t.writer.write buf ~pos:0 l) |
| 32 | in |
| 33 | printf "pos after writing: %d\n" pos; |
| 34 | () |
| 35 | in |
| 36 | run_test [%here] Int.Stable.V1.bin_t [ 1; 2; 3; 4; 5 ]; |
| 37 | [%expect {| |
| 38 | size: 6 |
| 39 | pos after writing: 6 |
| 40 | |}]; |
| 41 | run_test [%here] String.Stable.V1.bin_t [ "one"; "two"; "three"; "four" ]; |
| 42 | [%expect {| |
| 43 | size: 20 |
| 44 | pos after writing: 20 |
| 45 | |}] |
| 46 | ;; |