package bechamel

  1. Overview
  2. Docs
Yet Another Benchmark in OCaml

Install

Dune Dependency

Authors

Maintainers

Sources

bechamel-0.5.0.tbz
sha256=2f9aa544395fa62ea067352782988bf94d977162d4d3ee6e321e49a29c5ec868
sha512=80af7d3015fd3e63514b6241190f7fc96a1329480d98526dc426c8e24fdf2f196917a9bdae72ac7234e21b2835c38a5ac989cfc00360d9530afe52f97f58c79f

doc/src/bechamel/test.ml.html

Source file test.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
type ('a, 't) app

module Uniq : sig
  type t

  external inj : 'a -> ('a, t) app = "%identity"
  external prj : ('a, t) app -> 'a = "%identity"
  val unit : (unit, t) app
end = struct
  type t

  external inj : 'a -> ('a, t) app = "%identity"
  external prj : ('a, t) app -> 'a = "%identity"

  let unit = inj ()
end

module Multiple : sig
  type t

  external inj : 'a array -> ('a, t) app = "%identity"
  external prj : ('a, t) app -> 'a array = "%identity"
end = struct
  type t

  external inj : 'a array -> ('a, t) app = "%identity"
  external prj : ('a, t) app -> 'a array = "%identity"
end

type packed =
  | V :
      { fn : [ `Init ] -> 'a -> 'b
      ; kind : ('a, 'v, 't) kind
      ; allocate : 'v -> ('a, 't) app
      ; free : ('a, 't) app -> unit
      }
      -> packed

and ('a, 'v, 'k) kind =
  | Uniq : ('a, unit, Uniq.t) kind
  | Multiple : ('a, int, Multiple.t) kind

let uniq = Uniq
let multiple = Multiple
let always v _ = v
let ( <.> ) f g x = f (g x)

module Elt = struct
  type t = { key : int; name : string; fn : packed }

  let unsafe_make ~name fn =
    { key = 0
    ; name
    ; fn =
        V
          { fn = (fun `Init -> Staged.unstage fn)
          ; kind = Uniq
          ; allocate = always Uniq.unit
          ; free = always ()
          }
    }

  let key { key; _ } = key
  let name { name; _ } = name
  let fn { fn; _ } = fn
end

type fmt_indexed =
  (string -> int -> string, Format.formatter, unit, string) format4

type fmt_grouped =
  (string -> string -> string, Format.formatter, unit, string) format4

type t = { name : string; set : Elt.t list }

let make ~name fn =
  { name
  ; set =
      [ { Elt.key = 0
        ; Elt.name
        ; Elt.fn =
            V
              { fn = (fun `Init -> Staged.unstage fn)
              ; kind = Uniq
              ; allocate = always Uniq.unit
              ; free = always ()
              }
        }
      ]
  }

open Unsafe

let make_multiple_allocate f = function
  | 0 -> [||]
  | len ->
      let vs = unsafe_array_make len (f ()) in
      for i = 1 to len - 1 do
        unsafe_array_set vs i (f ())
      done;
      vs

let make_multiple_free f arr =
  for i = 0 to Array.length arr - 1 do
    f (unsafe_array_get arr i)
  done

let make_with_resource :
    type a v k.
       name:string
    -> (a, v, k) kind
    -> allocate:(unit -> a)
    -> free:(a -> unit)
    -> (a -> 'b) Staged.t
    -> t =
 fun ~name kind ~allocate ~free fn ->
  match kind with
  | Uniq ->
      { name
      ; set =
          [ { Elt.key = 0
            ; Elt.name
            ; Elt.fn =
                V
                  { fn = (fun `Init -> Staged.unstage fn)
                  ; allocate = Uniq.inj <.> allocate
                  ; free = free <.> Uniq.prj
                  ; kind = Uniq
                  }
            }
          ]
      }
  | Multiple ->
      { name
      ; set =
          [ { Elt.key = 0
            ; Elt.name
            ; Elt.fn =
                V
                  { fn = (fun `Init -> Staged.unstage fn)
                  ; allocate = Multiple.inj <.> make_multiple_allocate allocate
                  ; free = make_multiple_free free <.> Multiple.prj
                  ; kind = Multiple
                  }
            }
          ]
      }

let make_indexed ~name ?(fmt : fmt_indexed = "%s:%d") ~args fn =
  { name
  ; set =
      List.map
        (fun key ->
          { Elt.key
          ; Elt.name = Fmt.str fmt name key
          ; Elt.fn =
              V
                { fn = (fun `Init -> Staged.unstage (fn key))
                ; kind = Uniq
                ; allocate = always Uniq.unit
                ; free = always ()
                }
          })
        args
  }

let make_indexed_with_resource :
    type a f g.
       name:string
    -> ?fmt:fmt_indexed
    -> args:int list
    -> (a, f, g) kind
    -> allocate:(int -> a)
    -> free:(a -> unit)
    -> (int -> (a -> 'b) Staged.t)
    -> t =
 fun ~name ?(fmt : fmt_indexed = "%s:%d") ~args kind ~allocate ~free fn ->
  match kind with
  | Uniq ->
      { name
      ; set =
          List.map
            (fun key ->
              { Elt.key
              ; Elt.name = Fmt.str fmt name key
              ; Elt.fn =
                  V
                    { fn = (fun `Init -> Staged.unstage (fn key))
                    ; kind = Uniq
                    ; allocate = (fun () -> Uniq.inj (allocate key))
                    ; free = free <.> Uniq.prj
                    }
              })
            args
      }
  | Multiple ->
      { name
      ; set =
          List.map
            (fun key ->
              { Elt.key
              ; Elt.name = Fmt.str fmt name key
              ; Elt.fn =
                  V
                    { fn = (fun `Init -> Staged.unstage (fn key))
                    ; kind = Multiple
                    ; allocate =
                        Multiple.inj
                        <.> make_multiple_allocate (fun () -> allocate key)
                    ; free = make_multiple_free free <.> Multiple.prj
                    }
              })
            args
      }

let name { name; _ } = name
let names { set; _ } = List.map Elt.name set
let elements { set; _ } = set
let expand ts = List.concat (List.map (fun t -> t.set) ts)

let make_grouped ~name ?(fmt : fmt_grouped = "%s/%s") ts =
  let ts = expand ts in
  { name
  ; set =
      List.map (fun t -> { t with Elt.name = Fmt.str fmt name t.Elt.name }) ts
  }
OCaml

Innovation. Community. Security.