<commit_before>(** Generic benchs *)

(* composition *)
let (%%) f g x = f (g x)

(* FIXME: find out why -tree takes so long *)

module L = struct
  (* FLAT MAP *)

  let f_ x =
    if x mod 10 = 0 then []
    else if x mod 5 = 1 then [x;x+1]
    else [x;x+1;x+2;x+3]

  let bench_flat_map ?(time=2) n =
    let l = lazy CCList.(1 -- n) in
    let flatten_map_ l = List.flatten (CCList.map f_ l)
    and flatten_ccmap_ l = List.flatten (List.map f_ l) in
    CCBench.throughputN time
      [ "flat_map", CCList.flat_map f_ %% Lazy.force, l
      ; "flatten o CCList.map", flatten_ccmap_ %% Lazy.force, l
      ; "flatten o map", flatten_map_ %% Lazy.force, l
      ]

  (* APPEND *)

  let append_ f (lazy l1, lazy l2, lazy l3) =
    ignore (f (f l1 l2) l3)

  let bench_append ?(time=2) n =
    let l1 = lazy CCList.(1 -- n) in
    let l2 = lazy CCList.(n+1 -- 2*n) in
    let l3 = lazy CCList.(2*n+1 -- 3*n) in
    let arg = l1, l2, l3 in
    CCBench.throughputN time
      [ "CCList.append", append_ CCList.append, arg
      ; "List.append", append_ List.append, arg
      ]

  (* FLATTEN *)

  let bench_flatten ?(time=2) n =
    let fold_right_append_ l =
      List.fold_right List.append l []
    and cc_fold_right_append_ l =
      CCList.fold_right CCList.append l []
    in
    let l = lazy (
      CCList.Idx.mapi
        (fun i x -> CCList.(x -- (x+ min i 100)))
        CCList.(1 -- n))
    in
    CCBench.throughputN time
      [ "CCList.flatten", CCList.flatten %% Lazy.force, l
      ; "List.flatten", List.flatten %% Lazy.force, l
      ; "fold_right append", fold_right_append_ %% Lazy.force, l
      ; "CCList.(fold_right append)", cc_fold_right_append_ %% Lazy.force, l
      ]

  (* MAIN *)

  let () = CCBench.register CCBench.(
    "list" >:::
      [ "flat_map" >::
        map_int
          [ bench_flat_map ~time:2, 100
          ; bench_flat_map ~time:2, 10_000
          ; bench_flat_map ~time:4, 100_000]
      ; "flatten" >::
        map_int
          [ bench_flatten ~time:2, 100
          ; bench_flatten ~time:2, 10_000
          ; bench_flatten ~time:4, 100_000]
      ; "append" >::
        map_int
          [ bench_append ~time:2, 100
          ; bench_append ~time:2, 10_000
          ; bench_append ~time:4, 100_000]
      ]
    )
end

module Vec = struct
  let f x = x+1

  let map_push_ f v =
    let v' = CCVector.create () in
    CCVector.iter (fun x -> CCVector.push v' (f x)) v;
    v'

  let map_push_size_ f v =
    let v' = CCVector.create_with ~capacity:(CCVector.length v) 0 in
    CCVector.iter (fun x -> CCVector.push v' (f x)) v;
    v'

  let bench_map n =
    let v = lazy (CCVector.init n (fun x->x)) in
    CCBench.throughputN 2
      [ "map", CCVector.map f %% Lazy.force, v
      ; "map_push", map_push_ f %% Lazy.force, v
      ; "map_push_cap", map_push_size_ f %% Lazy.force, v
      ]

  let try_append_ app n v2 () =
    let v1 = CCVector.init n (fun x->x) in
    app v1 (Lazy.force v2);
    assert (CCVector.length v1 = 2*n);
    ()

  let append_naive_ v1 v2 =
    CCVector.iter (fun x -> CCVector.push v1 x) v2

  let bench_append n =
    let v2 = lazy (CCVector.init n (fun x->n+x)) in
    CCBench.throughputN 2
      [ "append", try_append_ CCVector.append n v2, ()
      ; "append_naive", try_append_ append_naive_ n v2, ()
      ]

  let () = CCBench.register CCBench.(
    "vector" >:::
      [ "map" >:: with_int bench_map [100; 10_000; 100_000]
      ; "append" >:: with_int bench_append [100; 10_000; 50_000]
      ]
  )
end

module Cache = struct
  let make_fib c =
    let f = Cache.with_cache_rec c
      (fun fib n -> match n with
        | 0 -> 0
        | 1 -> 1
        | 2 -> 1
        | n -> fib (n-1) + fib (n-2)
      )
    in
    fun x ->
      Cache.clear c;
      f x

  let bench_fib n =
    let l =
      [ "replacing_fib", make_fib (Cache.replacing 256), n
      ; "LRU_fib", make_fib (Cache.lru 256), n
      ]
    in
    let l = if n <= 20
      then  [ "linear_fib (5)", make_fib (Cache.linear 5), n
            ; "linear_fib (32)", make_fib (Cache.linear 32), n
            ; "dummy_fib", make_fib Cache.dummy, n
            ] @ l
      else l
    in
    CCBench.throughputN 3 l

  let () = CCBench.register CCBench.(
    "cache" >:::
      [ "fib" >:: with_int bench_fib [10; 20; 100; 200; 1_000;]
      ]
  )
end

module Tbl = struct
  module IHashtbl = Hashtbl.Make(struct
    type t = int
    let equal i j = i = j
    let hash i = i
  end)

  module IFlatHashtbl = FlatHashtbl.Make(struct
    type t = int
    let equal i j = i = j
    let hash i = i<commit_msg>more precise benchmarks for caches<commit_after>(** Generic benchs *)

(* composition *)
let (%%) f g x = f (g x)

(* FIXME: find out why -tree takes so long *)

module L = struct
  (* FLAT MAP *)

  let f_ x =
    if x mod 10 = 0 then []
    else if x mod 5 = 1 then [x;x+1]
    else [x;x+1;x+2;x+3]

  let bench_flat_map ?(time=2) n =
    let l = lazy CCList.(1 -- n) in
    let flatten_map_ l = List.flatten (CCList.map f_ l)
    and flatten_ccmap_ l = List.flatten (List.map f_ l) in
    CCBench.throughputN time
      [ "flat_map", CCList.flat_map f_ %% Lazy.force, l
      ; "flatten o CCList.map", flatten_ccmap_ %% Lazy.force, l
      ; "flatten o map", flatten_map_ %% Lazy.force, l
      ]

  (* APPEND *)

  let append_ f (lazy l1, lazy l2, lazy l3) =
    ignore (f (f l1 l2) l3)

  let bench_append ?(time=2) n =
    let l1 = lazy CCList.(1 -- n) in
    let l2 = lazy CCList.(n+1 -- 2*n) in
    let l3 = lazy CCList.(2*n+1 -- 3*n) in
    let arg = l1, l2, l3 in
    CCBench.throughputN time
      [ "CCList.append", append_ CCList.append, arg
      ; "List.append", append_ List.append, arg
      ]

  (* FLATTEN *)

  let bench_flatten ?(time=2) n =
    let fold_right_append_ l =
      List.fold_right List.append l []
    and cc_fold_right_append_ l =
      CCList.fold_right CCList.append l []
    in
    let l = lazy (
      CCList.Idx.mapi
        (fun i x -> CCList.(x -- (x+ min i 100)))
        CCList.(1 -- n))
    in
    CCBench.throughputN time
      [ "CCList.flatten", CCList.flatten %% Lazy.force, l
      ; "List.flatten", List.flatten %% Lazy.force, l
      ; "fold_right append", fold_right_append_ %% Lazy.force, l
      ; "CCList.(fold_right append)", cc_fold_right_append_ %% Lazy.force, l
      ]

  (* MAIN *)

  let () = CCBench.register CCBench.(
    "list" >:::
      [ "flat_map" >::
        map_int
          [ bench_flat_map ~time:2, 100
          ; bench_flat_map ~time:2, 10_000
          ; bench_flat_map ~time:4, 100_000]
      ; "flatten" >::
        map_int
          [ bench_flatten ~time:2, 100
          ; bench_flatten ~time:2, 10_000
          ; bench_flatten ~time:4, 100_000]
      ; "append" >::
        map_int
          [ bench_append ~time:2, 100
          ; bench_append ~time:2, 10_000
          ; bench_append ~time:4, 100_000]
      ]
    )
end

module Vec = struct
  let f x = x+1

  let map_push_ f v =
    let v' = CCVector.create () in
    CCVector.iter (fun x -> CCVector.push v' (f x)) v;
    v'

  let map_push_size_ f v =
    let v' = CCVector.create_with ~capacity:(CCVector.length v) 0 in
    CCVector.iter (fun x -> CCVector.push v' (f x)) v;
    v'

  let bench_map n =
    let v = lazy (CCVector.init n (fun x->x)) in
    CCBench.throughputN 2
      [ "map", CCVector.map f %% Lazy.force, v
      ; "map_push", map_push_ f %% Lazy.force, v
      ; "map_push_cap", map_push_size_ f %% Lazy.force, v
      ]

  let try_append_ app n v2 () =
    let v1 = CCVector.init n (fun x->x) in
    app v1 (Lazy.force v2);
    assert (CCVector.length v1 = 2*n);
    ()

  let append_naive_ v1 v2 =
    CCVector.iter (fun x -> CCVector.push v1 x) v2

  let bench_append n =
    let v2 = lazy (CCVector.init n (fun x->n+x)) in
    CCBench.throughputN 2
      [ "append", try_append_ CCVector.append n v2, ()
      ; "append_naive", try_append_ append_naive_ n v2, ()
      ]

  let () = CCBench.register CCBench.(
    "vector" >:::
      [ "map" >:: with_int bench_map [100; 10_000; 100_000]
      ; "append" >:: with_int bench_append [100; 10_000; 50_000]
      ]
  )
end

module Cache = struct
  let make_fib c =
    let f = Cache.with_cache_rec c
      (fun fib n -> match n with
        | 0 -> 0
        | 1 -> 1
        | 2 -> 1
        | n -> fib (n-1) + fib (n-2)
      )
    in
    fun x ->
      Cache.clear c;
      f x

  let bench_fib n =
    let l =
      [ "replacing_fib (128)", make_fib (Cache.replacing 128), n
      ; "LRU_fib (128)", make_fib (Cache.lru 128), n
      ; "replacing_fib (16)", make_fib (Cache.replacing 16), n
      ; "LRU_fib (16)", make_fib (Cache.lru 16), n
      ; "unbounded", make_fib (Cache.unbounded 32), n
      ]
    in
    let l = if n <= 20
      then  [ "linear_fib (5)", make_fib (Cache.linear 5), n
            ; "linear_fib (32)", make_fib (Cache.linear 32), n
            ; "dummy_fib", make_fib Cache.dummy, n
            ] @ l
      else l
    in
    CCBench.throughputN 3 l

  let () = CCBench.register CCBench.(
    "cache" >:::
      [ "fib" >:: with_int bench_fib [10; 20; 100; 200; 1_000;]
      ]
  )
end

module Tbl = struct
  module IHashtbl = Hashtbl.Make(struct
    type t = int
    let equal i j = i = j
    let hash i = i
  end)

  module IFlatHashtbl = FlatHashtbl.Make(struct
    type t = int
    let equal i j = i = j
    let hash i = i
{'fraction_non_alphanumeric': 0.1327329259140131, 'fraction_numerical': 0.03752546370751581, 'mean_word_length': 2.064388961892247, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '767982', 'n_tokens_mistral': 3931, 'n_tokens_neox': 3565, 'n_words': 1030}