Debian Codegraph @

runtime-lib/test/allocation.ml

raw symbols SCIP
1 open Core
2
3 let words_of_float = if Sys.word_size_in_bits = 64 then 2 else 3
4
5 let check_allocation : expect:int -> (unit -> 'a) -> 'a =
6 fun ~expect f ->
7 (* It costs [fudge] words of allocation to discover the allocation! *)
8 let fudge = 18 + (3 * words_of_float) in
9 let n0 = Int.of_float (Gc.stat ()).Gc.Stat.minor_words in
10 let n1 = Int.of_float (Gc.stat ()).Gc.Stat.minor_words in
11 [%test_result: Int.t] ~expect:(n0 + fudge) n1;
12 let res = f () in
13 let n2 = Int.of_float (Gc.stat ()).Gc.Stat.minor_words in
14 let n = n2 - n1 - fudge in
15 if n <> expect then failwithf "check_allocation: expect=%d, got=%d" expect n ();
16 res
17 ;;
18
19 let check_no_allocation f = check_allocation ~expect:0 f
20
21 module F (Hash : Base.Hash.S) = struct
22 module Ppx_hash_lib = struct
23 (* override default which is used by generated code *)
24 module Std = struct
25 module Hash = Base.Hash.F (Hash)
26 end
27 end
28
29 include Ppx_hash_lib.Std
30 open Hash.Builtin
31
32 type tree =
33 | Leaf of int
34 | Node of tree * tree
35 [@@deriving sexp_of, hash]
36
37 let create_full ~depth =
38 assert (depth >= 0);
39 let rec build n d =
40 if d = 0
41 then n + 1, Leaf n
42 else (
43 let n, t1 = build n (d - 1) in
44 let n, t2 = build n (d - 1) in
45 n, Node (t1, t2))
46 in
47 let _, t = build 100 depth in
48 t
49 ;;
50
51 let the_tree = create_full ~depth:3
52 end
53
54 module Test_alloc (X : sig
55 module Hash : Base.Hash.S with type hash_value = int
56
57 val size_of_state : int
58 val seed1 : Hash.seed
59 val seed2 : Hash.seed
60 end) =
61 struct
62 include F (X.Hash)
63
64 let%test_unit _ =
65 let state = check_allocation ~expect:X.size_of_state (fun () -> Hash.alloc ()) in
66 let _state = check_no_allocation (fun () -> hash_fold_tree state the_tree) in
67 ()
68 ;;
69
70 let run_seeded seed = Hash.run ~seed hash_fold_tree the_tree
71 let%test_unit _ = assert (not (run_seeded X.seed1 = run_seeded X.seed2))
72
73 let%test_unit _ =
74 ignore (check_allocation ~expect:X.size_of_state (fun () -> hash_tree the_tree))
75 ;;
76
77 let mk_reentrant_folder n =
78 let i = ref n in
79 let rec loop state x =
80 decr i;
81 if !i > 0 then ignore (Hash.run loop x);
82 let state = hash_fold_tree state x in
83 state
84 in
85 Hash.run loop
86 ;;
87
88 let%test_unit _ =
89 let n = 100 in
90 let reentrant_folder = mk_reentrant_folder n in
91 ignore
92 (check_allocation ~expect:(n * X.size_of_state) (fun () ->
93 reentrant_folder the_tree))
94 ;;
95
96 let res1 = hash_tree the_tree
97 let res2 = hash_tree the_tree
98 let%test_unit "hashing is stable2" = [%test_eq: int] res1 res2
99 end
100
101 module Test_alloc_internalhash = Test_alloc (struct
102 module Hash = Base.Hash
103
104 let size_of_state = 0
105 let seed1 = 1
106 let seed2 = 2
107 end)
108
109 module Test_alloc_siphash = Test_alloc (struct
110 module Hash = Siphash_lib.Siphash
111
112 let size_of_state = if Sys.word_size_in_bits = 64 then 5 else 9
113 let seed1 = "1"
114 let seed2 = "2"
115 end)