File: bench_fields.ml

package info (click to toggle)
ppx-fields-conv 0.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 192 kB
  • sloc: ml: 972; sh: 50; makefile: 24
file content (145 lines) | stat: -rw-r--r-- 4,282 bytes parent folder | download
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
(* Current results:
   ┌───────────────────────────────────────────────────────────────────────────────────────┬──────────┬────────────┐
   │ Name                                                                                  │ Time/Run │ Percentage │
   ├───────────────────────────────────────────────────────────────────────────────────────┼──────────┼────────────┤
   │ [bench_fields.ml:field_setting] manual field setting                                  │   2.93ns │     92.92% │
   │ [bench_fields.ml:field_setting] Fields.Direct inlined                                 │   2.71ns │     86.01% │
   │ [bench_fields.ml:field_setting] Fields.Direct NOT inlined                             │   3.15ns │    100.00% │
   │ [bench_fields.ml:shorter_record_field_setting] manual field setting                   │   2.68ns │     84.91% │
   │ [bench_fields.ml:shorter_record_field_setting] [Fields.Direct.set_all_mutable_fields] │   2.53ns │     80.10% │
   └───────────────────────────────────────────────────────────────────────────────────────┴──────────┴────────────┘
*)


type a_or_b = | A | B

let%bench_module "field_setting" =
  (module struct

    type t =
      { mutable a : int
      ; b : int
      ; mutable c : a_or_b
      ; mutable d : int
      ; mutable e : int
      ; mutable f : int
      ; mutable g : int
      } [@@deriving fields]

    let set_manual t ~a ~c ~d ~e ~f ~g =
      t.a <- a;
      t.c <- c;
      t.d <- d;
      t.e <- e;
      t.f <- f;
      t.g <- g;
    ;;

    let [@inline] set_via_fields t ~a ~c ~d ~e ~f ~g =
      Fields.Direct.set_all_mutable_fields t ~a ~c ~d ~e ~f ~g
    ;;

    let [@cold] set_via_fields_not_inlined t ~a ~c ~d ~e ~f ~g =
      Fields.Direct.set_all_mutable_fields t ~a ~c ~d ~e ~f ~g
    ;;

    let init () =
      { a = 0
      ; b = 0
      ; c = A
      ; d = 0
      ; e = 0
      ; f = 0
      ; g = 0
      }

    let%bench_fun "manual field setting" =
      let t = init () in
      (fun () ->
         set_manual t
           ~a:1234567
           ~c:B
           ~d:1000
           ~e:99999
           ~f:42
           ~g:987)
    ;;

    let%bench_fun "Fields.Direct inlined" =
      let t = init () in
      (fun () ->
         set_via_fields t
           ~a:1234567
           ~c:B
           ~d:1000
           ~e:99999
           ~f:42
           ~g:987)
    ;;

    let%bench_fun "Fields.Direct NOT inlined" =
      let t = init () in
      (fun () ->
         set_via_fields_not_inlined t
           ~a:1234567
           ~c:B
           ~d:1000
           ~e:99999
           ~f:42
           ~g:987)
    ;;
  end)

let%bench_module "shorter_record_field_setting" =
  (module struct

    type t =
      { mutable a : int
      ; b : int
      ; mutable c : a_or_b
      ; mutable d : int
      ; e : int
      ; f : int
      ; g : int
      } [@@deriving fields]

    let set_manual t ~a ~c ~d =
      t.a <- a;
      t.c <- c;
      t.d <- d;
    ;;

    let set_via_fields t ~a ~c ~d =
      Fields.Direct.set_all_mutable_fields t ~a ~c ~d
    ;;

    let init () =
      { a = 0
      ; b = 0
      ; c = B
      ; d = 0
      ; e = 0
      ; f = 0
      ; g = 0
      }

    let%bench_fun "manual field setting" =
      let t = init () in
      (fun () ->
         set_manual t
           ~a:1234567
           ~c:B
           ~d:1000
      )
    ;;

    let%bench_fun "[Fields.Direct.set_all_mutable_fields]" =
      let t = init () in
      (fun () ->
         set_via_fields t
           ~a:1234567
           ~c:B
           ~d:1000
      )
    ;;
  end)