| 1 | open! Base |
| 2 | |
| 3 | let bench_sexp_of_t ~sexp_of_t t = |
| 4 | let t = Sys.opaque_identity t in |
| 5 | fun () -> sexp_of_t t |
| 6 | ;; |
| 7 | |
| 8 | let bench_t_of_sexp ~t_of_sexp string = |
| 9 | let sexp = Sys.opaque_identity (Parsexp.Single.parse_string_exn string) in |
| 10 | fun () -> t_of_sexp sexp |
| 11 | ;; |
| 12 | |
| 13 | let%bench_module "Record" = |
| 14 | (module struct |
| 15 | type t = |
| 16 | { a : int |
| 17 | ; b : int option [@omit_nil] |
| 18 | ; c : bool [@sexp.bool] |
| 19 | ; d : int array [@sexp.array] |
| 20 | ; e : int list [@sexp.list] |
| 21 | ; f : int option [@sexp.option] |
| 22 | ; g : int [@default 0] [@sexp_drop_default ( = )] |
| 23 | ; h : int [@default 0] [@sexp_drop_default.compare] |
| 24 | ; i : int [@default 0] [@sexp_drop_default.equal] |
| 25 | ; j : int [@default 0] [@sexp_drop_default.sexp] |
| 26 | ; k : 'a. 'a list |
| 27 | } |
| 28 | [@@deriving sexp] |
| 29 | |
| 30 | let%bench_fun "sexp_of_t, full" = |
| 31 | bench_sexp_of_t |
| 32 | ~sexp_of_t |
| 33 | { a = 1 |
| 34 | ; b = Some 2 |
| 35 | ; c = true |
| 36 | ; d = [| 3; 4 |] |
| 37 | ; e = [ 5; 6 ] |
| 38 | ; f = Some 7 |
| 39 | ; g = 8 |
| 40 | ; h = 9 |
| 41 | ; i = 10 |
| 42 | ; j = 11 |
| 43 | ; k = [] |
| 44 | } |
| 45 | ;; |
| 46 | |
| 47 | let%bench_fun "sexp_of_t, empty" = |
| 48 | bench_sexp_of_t |
| 49 | ~sexp_of_t |
| 50 | { a = 0 |
| 51 | ; b = None |
| 52 | ; c = false |
| 53 | ; d = [||] |
| 54 | ; e = [] |
| 55 | ; f = None |
| 56 | ; g = 0 |
| 57 | ; h = 0 |
| 58 | ; i = 0 |
| 59 | ; j = 0 |
| 60 | ; k = [] |
| 61 | } |
| 62 | ;; |
| 63 | |
| 64 | let%bench_fun "t_of_sexp, full, in order" = |
| 65 | bench_t_of_sexp |
| 66 | ~t_of_sexp |
| 67 | "((a 1) (b (2)) (c) (d (3 4)) (e (5 6)) (f 7) (g 8) (h 9) (i 10) (j 11) (k ()))" |
| 68 | ;; |
| 69 | |
| 70 | let%bench_fun "t_of_sexp, full, reverse order" = |
| 71 | bench_t_of_sexp |
| 72 | ~t_of_sexp |
| 73 | "((k ()) (j 11) (i 10) (h 9) (g 8) (f 7) (e (5 6)) (d (3 4)) (c) (b (2)) (a 1))" |
| 74 | ;; |
| 75 | |
| 76 | let%bench_fun "t_of_sexp, empty" = bench_t_of_sexp ~t_of_sexp "((a 0) (k ()))" |
| 77 | end) |
| 78 | ;; |
| 79 | |
| 80 | let%bench_module "Variant" = |
| 81 | (module struct |
| 82 | type t = |
| 83 | | Atomic |
| 84 | | Tuple of int * string |
| 85 | | List of int list [@sexp.list] |
| 86 | | Record of |
| 87 | { a : int |
| 88 | ; b : int option [@omit_nil] |
| 89 | ; c : bool [@sexp.bool] |
| 90 | ; d : int array [@sexp.array] |
| 91 | ; e : int list [@sexp.list] |
| 92 | ; f : int option [@sexp.option] |
| 93 | ; g : int [@default 0] [@sexp_drop_default ( = )] |
| 94 | ; h : int [@default 0] [@sexp_drop_default.compare] |
| 95 | ; i : int [@default 0] [@sexp_drop_default.equal] |
| 96 | ; j : int [@default 0] [@sexp_drop_default.sexp] |
| 97 | ; k : 'a. 'a list |
| 98 | } |
| 99 | [@@deriving sexp] |
| 100 | |
| 101 | let%bench_fun "sexp_of_t, atomic" = bench_sexp_of_t ~sexp_of_t Atomic |
| 102 | let%bench_fun "sexp_of_t, tuple" = bench_sexp_of_t ~sexp_of_t (Tuple (1, "hello")) |
| 103 | let%bench_fun "sexp_of_t, list, full" = bench_sexp_of_t ~sexp_of_t (List [ 1; 2 ]) |
| 104 | let%bench_fun "sexp_of_t, list, empty" = bench_sexp_of_t ~sexp_of_t (List []) |
| 105 | |
| 106 | let%bench_fun "sexp_of_t, record, full" = |
| 107 | bench_sexp_of_t |
| 108 | ~sexp_of_t |
| 109 | (Record |
| 110 | { a = 1 |
| 111 | ; b = Some 2 |
| 112 | ; c = true |
| 113 | ; d = [| 3; 4 |] |
| 114 | ; e = [ 5; 6 ] |
| 115 | ; f = Some 7 |
| 116 | ; g = 8 |
| 117 | ; h = 9 |
| 118 | ; i = 10 |
| 119 | ; j = 11 |
| 120 | ; k = [] |
| 121 | }) |
| 122 | ;; |
| 123 | |
| 124 | let%bench_fun "sexp_of_t, record, empty" = |
| 125 | bench_sexp_of_t |
| 126 | ~sexp_of_t |
| 127 | (Record |
| 128 | { a = 0 |
| 129 | ; b = None |
| 130 | ; c = false |
| 131 | ; d = [||] |
| 132 | ; e = [] |
| 133 | ; f = None |
| 134 | ; g = 0 |
| 135 | ; h = 0 |
| 136 | ; i = 0 |
| 137 | ; j = 0 |
| 138 | ; k = [] |
| 139 | }) |
| 140 | ;; |
| 141 | |
| 142 | let%bench_fun "t_of_sexp, atomic" = bench_t_of_sexp ~t_of_sexp "Atomic" |
| 143 | let%bench_fun "t_of_sexp, tuple" = bench_t_of_sexp ~t_of_sexp "(Tuple 1 hello)" |
| 144 | let%bench_fun "t_of_sexp, list, full" = bench_t_of_sexp ~t_of_sexp "(List 1 2)" |
| 145 | let%bench_fun "t_of_sexp, list, empty" = bench_t_of_sexp ~t_of_sexp "(List)" |
| 146 | |
| 147 | let%bench_fun "t_of_sexp, record, full, in order" = |
| 148 | bench_t_of_sexp |
| 149 | ~t_of_sexp |
| 150 | "(Record (a 1) (b (2)) (c) (d (3 4)) (e (5 6)) (f 7) (g 8) (h 9) (i 10) (j 11) \ |
| 151 | (k ()))" |
| 152 | ;; |
| 153 | |
| 154 | let%bench_fun "t_of_sexp, record, full, reverse order" = |
| 155 | bench_t_of_sexp |
| 156 | ~t_of_sexp |
| 157 | "(Record (k ()) (j 11) (i 10) (h 9) (g 8) (f 7) (e (5 6)) (d (3 4)) (c) (b (2)) \ |
| 158 | (a 1))" |
| 159 | ;; |
| 160 | |
| 161 | let%bench_fun "t_of_sexp, record, empty" = |
| 162 | bench_t_of_sexp ~t_of_sexp "(Record (a 0) (k ()))" |
| 163 | ;; |
| 164 | end) |
| 165 | ;; |
| 166 | |
| 167 | let%bench_module "Tag" = |
| 168 | (module struct |
| 169 | type t = |
| 170 | [ `Atomic |
| 171 | | `Tuple of int * string |
| 172 | | `List of int list [@sexp.list] |
| 173 | ] |
| 174 | [@@deriving sexp] |
| 175 | |
| 176 | let%bench_fun "sexp_of_t, atomic" = bench_sexp_of_t ~sexp_of_t `Atomic |
| 177 | let%bench_fun "sexp_of_t, tuple" = bench_sexp_of_t ~sexp_of_t (`Tuple (1, "hello")) |
| 178 | let%bench_fun "sexp_of_t, list, full" = bench_sexp_of_t ~sexp_of_t (`List [ 1; 2 ]) |
| 179 | let%bench_fun "sexp_of_t, list, empty" = bench_sexp_of_t ~sexp_of_t (`List []) |
| 180 | let%bench_fun "t_of_sexp, atomic" = bench_t_of_sexp ~t_of_sexp "Atomic" |
| 181 | let%bench_fun "t_of_sexp, tuple" = bench_t_of_sexp ~t_of_sexp "(Tuple (1 hello))" |
| 182 | let%bench_fun "t_of_sexp, list, full" = bench_t_of_sexp ~t_of_sexp "(List 1 2)" |
| 183 | let%bench_fun "t_of_sexp, list, empty" = bench_t_of_sexp ~t_of_sexp "(List)" |
| 184 | end) |
| 185 | ;; |
| 186 | |
| 187 | let%bench_module "Inherit" = |
| 188 | (module struct |
| 189 | type atomic = [ `Atomic ] [@@deriving sexp] |
| 190 | type tuple = [ `Tuple of int * string ] [@@deriving sexp] |
| 191 | type listed = [ `List of int list [@sexp.list] ] [@@deriving sexp] |
| 192 | |
| 193 | type t = |
| 194 | [ atomic |
| 195 | | tuple |
| 196 | | listed |
| 197 | ] |
| 198 | [@@deriving sexp] |
| 199 | |
| 200 | let%bench_fun "sexp_of_t, atomic" = bench_sexp_of_t ~sexp_of_t `Atomic |
| 201 | let%bench_fun "sexp_of_t, tuple" = bench_sexp_of_t ~sexp_of_t (`Tuple (1, "hello")) |
| 202 | let%bench_fun "sexp_of_t, list, full" = bench_sexp_of_t ~sexp_of_t (`List [ 1; 2 ]) |
| 203 | let%bench_fun "sexp_of_t, list, empty" = bench_sexp_of_t ~sexp_of_t (`List []) |
| 204 | let%bench_fun "t_of_sexp, atomic" = bench_t_of_sexp ~t_of_sexp "Atomic" |
| 205 | let%bench_fun "t_of_sexp, tuple" = bench_t_of_sexp ~t_of_sexp "(Tuple (1 hello))" |
| 206 | let%bench_fun "t_of_sexp, list, full" = bench_t_of_sexp ~t_of_sexp "(List 1 2)" |
| 207 | let%bench_fun "t_of_sexp, list, empty" = bench_t_of_sexp ~t_of_sexp "(List)" |
| 208 | end) |
| 209 | ;; |