Skip to content

Commit ffc61f5

Browse files
authored
Remove Belt.Array.push (#122)
1 parent b8756a5 commit ffc61f5

File tree

5 files changed

+96
-30
lines changed

5 files changed

+96
-30
lines changed

packages/Belt/src/Belt_Array.ml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,4 @@ let joinWithU a sep toString =
518518
let joinWith a sep toString = joinWithU a sep (fun x -> toString x)
519519
let initU n f = Stdlib.Array.init n f
520520
let init n f = initU n (fun i -> f i)
521-
522-
let push arr i =
523-
let len = length arr in
524-
setUnsafe arr (len + 1) i
525-
[@@deprecated
526-
"You should use `concat` instead. Since in JavaScript `Array.prototype.push` \
527-
mutates the array reference, and it is not possible in native OCaml."]
521+
let push _arr _i = `Do_not_use_Array_push_in_native

packages/Belt/src/Belt_Array.mli

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -668,16 +668,10 @@ val truncateToLengthUnsafe : 'a t -> int -> 'a t
668668
let () = truncateToLengthUnsafe arr 3;;
669669
arr = [|"ant";"bee";"cat"|];;
670670
]}
671-
672671
*)
673672

674673
val initU : int -> ((int -> 'a)[@bs]) -> 'a t
675674
val init : int -> (int -> 'a) -> 'a t
676675

677-
(* external push : 'a t -> 'a -> unit = "push"
678-
[@@send] *)
679-
val push : 'a t -> 'a -> unit
680-
(**
681-
[arr->push(item)]
682-
push element `item` into the array
683-
*)
676+
val push : 'a t -> 'a -> [ `Do_not_use_Array_push_in_native ]
677+
(** Using Belt.Array.push in native isn't a good idea, since OCaml's Array are fixed length and can't resize the same way as JavaScript arrays. *)

packages/Belt/test/benchmark.ml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
open Bechamel
2+
3+
(* From our function [make_list], we make an indexed (by [args]) test. It's a list
4+
of tests which are applied with [args] such as:
5+
6+
{[
7+
let test =
8+
[ make_list 0
9+
; make_list 10
10+
; make_list 100
11+
; make_list 400
12+
; make_list 1000 ]
13+
]} *)
14+
let static_array = [| 33 |]
15+
16+
let test =
17+
Test.make_indexed ~name:"Belt.Array.push" ~fmt:"%s %d"
18+
~args:[ 0; 100; 500; 1000; 10000 ] (fun words ->
19+
Staged.stage @@ fun () -> Belt.Array.push static_array words)
20+
21+
(* From our test, we can start to benchmark it!
22+
23+
A benchmark is a /run/ of your test multiple times. From results given by
24+
[Benchmark.all], an analyse is needed to infer measures of one call of your
25+
test.
26+
27+
[Bechamel] asks 3 things:
28+
- what you want to record (see [instances])
29+
- how you want to analyse (see [ols])
30+
- how you want to benchmark your test (see [cfg])
31+
32+
The core of [Bechamel] (see [Bechamel.Toolkit]) has some possible measures
33+
such as the [monotonic-clock] to see time performances.
34+
35+
The analyse can be OLS (Ordinary Least Square) or RANSAC. In this example, we
36+
use only one.
37+
38+
Finally, to launch the benchmark, we need some others details such as:
39+
- should we stabilise the GC?
40+
- how many /run/ you want
41+
- the maximum of time allowed by the benchmark
42+
- etc.
43+
44+
[raw_results] is what the benchmark produced. [results] is what the analyse
45+
can infer. The first one is used to show graphs or to let the user (with
46+
[Measurement_raw]) to infer something else than what [ols] did. The second is
47+
mostly what you want: a synthesis of /samples/. *)
48+
49+
let benchmark () =
50+
let ols =
51+
Analyze.ols ~bootstrap:0 ~r_square:true ~predictors:Measure.[| run |]
52+
in
53+
let instances =
54+
Toolkit.Instance.[ minor_allocated; major_allocated; monotonic_clock ]
55+
in
56+
let cfg =
57+
Benchmark.cfg ~limit:2000 ~quota:(Time.second 0.5) ~kde:(Some 1000) ()
58+
in
59+
let raw_results = Benchmark.all cfg instances test in
60+
let results =
61+
List.map (fun instance -> Analyze.all ols instance raw_results) instances
62+
in
63+
let results = Analyze.merge ols instances results in
64+
(results, raw_results)
65+
66+
let () =
67+
List.iter
68+
(fun v -> Bechamel_notty.Unit.add v (Measure.unit v))
69+
Toolkit.Instance.[ minor_allocated; major_allocated; monotonic_clock ]
70+
71+
let img (window, results) =
72+
Bechamel_notty.Multiple.image_of_ols_results ~rect:window
73+
~predictor:Measure.run results
74+
75+
open Notty_unix
76+
77+
let () =
78+
let window =
79+
match winsize Unix.stdout with
80+
| Some (w, h) -> { Bechamel_notty.w; h }
81+
| None -> { Bechamel_notty.w = 80; h = 1 }
82+
in
83+
let results, _ = benchmark () in
84+
img (window, results) |> eol |> output_image

packages/Belt/test/dune

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
(test
22
(name test)
3-
(modules :standard)
3+
(modules test)
44
(libraries alcotest fmt belt js))
5+
6+
; (executable
7+
; (name benchmark)
8+
; (modules benchmark)
9+
; (public_name belt_benchmark)
10+
; (libraries belt bechamel-notty bechamel unix notty.unix))

packages/melange.ppx/regex.ml

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
open Ppxlib
22
module Builder = Ast_builder.Default
33

4-
let compile_regex ~flags str =
5-
match Quickjs.RegExp.compile str flags with
6-
| regex -> Ok regex
7-
| exception Invalid_argument msg -> Error msg
8-
| exception _ -> Error "unknown error while compiling regex"
9-
104
let parse_re str =
115
try
126
let _ = Str.search_forward (Str.regexp "/\\(.*\\)/\\(.*\\)") str 0 in
137
let first = Str.matched_group 1 str in
148
let second = Str.matched_group 2 str in
159
match String.length second with
16-
| 0 -> (
17-
match compile_regex ~flags:"" first with
18-
| Ok _regex -> Ok (first, None)
19-
| Error msg -> Error msg)
20-
| _ -> (
21-
match compile_regex ~flags:second first with
22-
| Ok _regex -> Ok (first, Some second)
23-
| Error msg -> Error msg)
10+
| 0 -> Ok (first, None)
11+
| _ -> Ok (first, Some second)
2412
with Not_found -> Error "invalid regex"
2513

2614
let extractor = Ast_pattern.(__')

0 commit comments

Comments
 (0)