| 1 |
open Core |
| 2 |
module Hash = Base.Hash |
| 3 |
|
| 4 |
module Bench (Hash : Hash.S) = struct |
| 5 |
let%bench_module ("" [@name_suffix Hash.description]) = |
| 6 |
(module struct |
| 7 |
module Ppx_hash_lib = struct |
| 8 |
module Std = struct |
| 9 |
module Hash = Base.Hash.F (Hash) |
| 10 |
end |
| 11 |
end |
| 12 |
|
| 13 |
module Hash = Ppx_hash_lib.Std.Hash |
| 14 |
open Hash.Builtin |
| 15 |
|
| 16 |
type a = int [@@deriving hash] |
| 17 |
type b = a * float [@@deriving hash] |
| 18 |
|
| 19 |
type c = |
| 20 |
| Foo |
| 21 |
| Bar |
| 22 |
| Baz of a * b * c |
| 23 |
[@@deriving hash] |
| 24 |
|
| 25 |
type d = (int * string) list [@@deriving hash] |
| 26 |
|
| 27 |
let hash_fold_d : Ppx_hash_lib.Std.Hash.state -> d -> Ppx_hash_lib.Std.Hash.state = |
| 28 |
|
| 29 |
|
| 30 |
hash_fold_list (fun hsv arg -> |
| 31 |
let e0, e1 = arg in |
| 32 |
hash_fold_string (hash_fold_int hsv e0) e1) |
| 33 |
;; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
let _ = Foo, Bar |
| 39 |
let a = 32 |
| 40 |
let b = 32, 42.0 |
| 41 |
let c1 = Foo |
| 42 |
let c2 = Baz (a, b, c1) |
| 43 |
let rec cn n = if n <= 1 then c1 else Baz (n, (n, float_of_int n), cn (n - 1)) |
| 44 |
let c10 = cn 10 |
| 45 |
let c100 = cn 100 |
| 46 |
|
| 47 |
let rec dn n = |
| 48 |
if n <= 0 |
| 49 |
then [] |
| 50 |
else |
| 51 |
(n, String.init n ~f:(fun i -> if i mod 2 = 0 then 'j' else 's')) :: dn (n - 1) |
| 52 |
;; |
| 53 |
|
| 54 |
let d10 = dn 10 |
| 55 |
let d100 = dn 100 |
| 56 |
let%bench "hash_init" = Hash.alloc () |
| 57 |
let state = Hash.alloc () |
| 58 |
let run folder x = ignore (Hash.get_hash_value (folder (Hash.reset state) x)) |
| 59 |
let%bench "hash a" = run hash_fold_a a |
| 60 |
let%bench "hash b" = run hash_fold_b b |
| 61 |
let%bench "hash c__1" = run hash_fold_c c1 |
| 62 |
let%bench "hash c__2" = run hash_fold_c c2 |
| 63 |
let%bench "hash c_10" = run hash_fold_c c10 |
| 64 |
let%bench "hash c100" = run hash_fold_c c100 |
| 65 |
let%bench "hash d_10" = run hash_fold_d d10 |
| 66 |
let%bench "hash d100" = run hash_fold_d d100 |
| 67 |
let _ = c2, c10, c100, d10, d100 |
| 68 |
|
| 69 |
type enum = |
| 70 |
| A |
| 71 |
| B |
| 72 |
| C |
| 73 |
| D |
| 74 |
| E |
| 75 |
| F |
| 76 |
| G |
| 77 |
| H |
| 78 |
[@@deriving hash] |
| 79 |
|
| 80 |
let enum_list = [ A; B; C; D; E; F; G; H ] |
| 81 |
let%bench "hash enum" = run (hash_fold_list hash_fold_enum) enum_list |
| 82 |
end) |
| 83 |
;; |
| 84 |
end |
| 85 |
|
| 86 |
module Bench_hashtbl_hash = struct |
| 87 |
|
| 88 |
|
| 89 |
let%bench_module "Hashtbl.hash" = |
| 90 |
(module struct |
| 91 |
type a = int |
| 92 |
type b = a * float |
| 93 |
|
| 94 |
type c = |
| 95 |
| Foo |
| 96 |
| Bar |
| 97 |
| Baz of a * b * c |
| 98 |
|
| 99 |
type d = (int * string) list |
| 100 |
|
| 101 |
let hash_a = Hashtbl.hash |
| 102 |
let hash_b = Hashtbl.hash |
| 103 |
let hash_c = Hashtbl.hash |
| 104 |
let hash_d = Hashtbl.hash |
| 105 |
let _ = Foo, Bar |
| 106 |
let a = 32 |
| 107 |
let b = 32, 42.0 |
| 108 |
let c1 = Foo |
| 109 |
let c2 = Baz (a, b, c1) |
| 110 |
let rec cn n = if n <= 1 then c1 else Baz (n, (n, float_of_int n), cn (n - 1)) |
| 111 |
let c10 = cn 10 |
| 112 |
let c100 = cn 100 |
| 113 |
|
| 114 |
let rec dn n : d = |
| 115 |
if n <= 0 |
| 116 |
then [] |
| 117 |
else |
| 118 |
(n, String.init n ~f:(fun i -> if i mod 2 = 0 then 'j' else 's')) :: dn (n - 1) |
| 119 |
;; |
| 120 |
|
| 121 |
let d10 = dn 10 |
| 122 |
let d100 = dn 100 |
| 123 |
let run f x = ignore (f x) |
| 124 |
let%bench "hash a" = run hash_a a |
| 125 |
let%bench "hash b" = run hash_b b |
| 126 |
let%bench "hash c__1" = run hash_c c1 |
| 127 |
let%bench "hash c__2" = run hash_c c2 |
| 128 |
let%bench "hash c_10" = run hash_c c10 |
| 129 |
let%bench "hash c100" = run hash_c c100 |
| 130 |
let%bench "hash d_10" = run hash_d d10 |
| 131 |
let%bench "hash d100" = run hash_d d100 |
| 132 |
let _ = c2, c10, c100, d10, d100 |
| 133 |
end) |
| 134 |
;; |
| 135 |
end |
| 136 |
|
| 137 |
module Traverse_only : Hash.S = struct |
| 138 |
let description = "Traverse_only" |
| 139 |
|
| 140 |
type hash_value = int |
| 141 |
type state = unit |
| 142 |
type seed = unit |
| 143 |
|
| 144 |
let alloc () = () |
| 145 |
let reset ?seed:_ () = () |
| 146 |
let get_hash_value () = 0 |
| 147 |
let fold_int () _ = () |
| 148 |
let fold_int64 () _ = () |
| 149 |
let fold_float () _ = () |
| 150 |
let fold_string () _ = () |
| 151 |
|
| 152 |
module For_tests = struct |
| 153 |
let compare_state _ _ = 0 |
| 154 |
let state_to_string () = "()" |
| 155 |
end |
| 156 |
end |
| 157 |
|
| 158 |
|
| 159 |
module Check_initialized_correctly : Hash.S = struct |
| 160 |
let description = "Check_initialized_correctly" |
| 161 |
|
| 162 |
type hash_value = int |
| 163 |
|
| 164 |
type state = |
| 165 |
{ me : int |
| 166 |
; valid : int ref |
| 167 |
} |
| 168 |
|
| 169 |
type seed = unit |
| 170 |
|
| 171 |
let next_id = |
| 172 |
let x = ref 0 in |
| 173 |
fun () -> |
| 174 |
incr x; |
| 175 |
!x |
| 176 |
;; |
| 177 |
|
| 178 |
let alloc () = { me = next_id (); valid = ref (next_id ()) } |
| 179 |
|
| 180 |
let reset ?seed:_ t = |
| 181 |
let me = next_id () in |
| 182 |
t.valid := me; |
| 183 |
{ me; valid = t.valid } |
| 184 |
;; |
| 185 |
|
| 186 |
let assert_valid t = assert (t.me = !(t.valid)) |
| 187 |
|
| 188 |
let change t = |
| 189 |
assert_valid t; |
| 190 |
let me = next_id () in |
| 191 |
t.valid := me; |
| 192 |
{ me; valid = t.valid } |
| 193 |
;; |
| 194 |
|
| 195 |
let get_hash_value t = |
| 196 |
let _ = change t in |
| 197 |
0 |
| 198 |
;; |
| 199 |
|
| 200 |
let fold_int t _ = change t |
| 201 |
let fold_int64 t _ = change t |
| 202 |
let fold_float t _ = change t |
| 203 |
let fold_string t _ = change t |
| 204 |
|
| 205 |
module For_tests = struct |
| 206 |
let compare_state a b = |
| 207 |
assert_valid a; |
| 208 |
assert_valid b; |
| 209 |
0 |
| 210 |
;; |
| 211 |
|
| 212 |
let state_to_string _ = "<state>" |
| 213 |
end |
| 214 |
|
| 215 |
let should_fail f = |
| 216 |
match f () with |
| 217 |
| exception _e -> () |
| 218 |
| _ -> failwith "should have failed" |
| 219 |
;; |
| 220 |
|
| 221 |
let%test_unit _ = |
| 222 |
should_fail (fun () -> |
| 223 |
let x = alloc () in |
| 224 |
let y = reset x in |
| 225 |
ignore (fold_int y 1); |
| 226 |
fold_int y 2) |
| 227 |
;; |
| 228 |
|
| 229 |
let%test_unit _ = |
| 230 |
should_fail (fun () -> |
| 231 |
let x = alloc () in |
| 232 |
let y = reset x in |
| 233 |
let y2 = reset x in |
| 234 |
ignore (fold_int y 1); |
| 235 |
ignore (fold_int y2 1)) |
| 236 |
;; |
| 237 |
|
| 238 |
let%test_unit _ = |
| 239 |
should_fail (fun () -> |
| 240 |
let x = alloc () in |
| 241 |
ignore (fold_int x 1)) |
| 242 |
;; |
| 243 |
|
| 244 |
let%test_unit _ = |
| 245 |
should_fail (fun () -> |
| 246 |
let x = alloc () in |
| 247 |
let x = reset x in |
| 248 |
ignore (get_hash_value x); |
| 249 |
ignore (fold_int x 1)) |
| 250 |
;; |
| 251 |
end |
| 252 |
|
| 253 |
let%bench_module "" = (module Bench (Traverse_only)) |
| 254 |
let%bench_module "" = (module Bench (Check_initialized_correctly)) |
| 255 |
let%bench_module "" = (module Bench (Base.Hash)) |
| 256 |
let%bench_module "" = (module Bench (Siphash_lib.Siphash)) |
| 257 |
let%bench_module "" = (module Bench (Ppx_hash_runtime_test.Perfect_hash)) |