File: bench_fields.ml

package info (click to toggle)
ppx-fields-conv 0.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: ml: 2,177; sh: 37; makefile: 14
file content (98 lines) | stat: -rw-r--r-- 3,920 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
(* 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 ~direct_iterators:set_all_mutable_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 ~direct_iterators:set_all_mutable_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)
;;