Skip to content

Commit 17fc09a

Browse files
committed
improve perf
1 parent 8369d84 commit 17fc09a

16 files changed

+337
-112
lines changed

jscomp/bin/all_ounit_tests.i.ml

Lines changed: 74 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ open OUnitTypes
7575

7676
(** Most simple heuristic, just pick the first test. *)
7777
let simple state =
78-
(* 153 *) List.hd state.tests_planned
78+
(* 154 *) List.hd state.tests_planned
7979

8080
end
8181
module OUnitUtils
@@ -98,22 +98,22 @@ let is_success =
9898
let is_failure =
9999
function
100100
| RFailure _ -> (* 0 *) true
101-
| RSuccess _ | RError _ | RSkip _ | RTodo _ -> (* 306 *) false
101+
| RSuccess _ | RError _ | RSkip _ | RTodo _ -> (* 308 *) false
102102

103103
let is_error =
104104
function
105105
| RError _ -> (* 0 *) true
106-
| RSuccess _ | RFailure _ | RSkip _ | RTodo _ -> (* 306 *) false
106+
| RSuccess _ | RFailure _ | RSkip _ | RTodo _ -> (* 308 *) false
107107

108108
let is_skip =
109109
function
110110
| RSkip _ -> (* 0 *) true
111-
| RSuccess _ | RFailure _ | RError _ | RTodo _ -> (* 306 *) false
111+
| RSuccess _ | RFailure _ | RError _ | RTodo _ -> (* 308 *) false
112112

113113
let is_todo =
114114
function
115115
| RTodo _ -> (* 0 *) true
116-
| RSuccess _ | RFailure _ | RError _ | RSkip _ -> (* 306 *) false
116+
| RSuccess _ | RFailure _ | RError _ | RSkip _ -> (* 308 *) false
117117

118118
let result_flavour =
119119
function
@@ -145,7 +145,7 @@ let rec was_successful =
145145
| [] -> (* 3 *) true
146146
| RSuccess _::t
147147
| RSkip _::t ->
148-
(* 459 *) was_successful t
148+
(* 462 *) was_successful t
149149

150150
| RFailure _::_
151151
| RError _::_
@@ -155,22 +155,22 @@ let rec was_successful =
155155
let string_of_node =
156156
function
157157
| ListItem n ->
158-
(* 612 *) string_of_int n
158+
(* 616 *) string_of_int n
159159
| Label s ->
160-
(* 918 *) s
160+
(* 924 *) s
161161

162162
(* Return the number of available tests *)
163163
let rec test_case_count =
164164
function
165-
| TestCase _ -> (* 153 *) 1
166-
| TestLabel (_, t) -> (* 176 *) test_case_count t
165+
| TestCase _ -> (* 154 *) 1
166+
| TestLabel (_, t) -> (* 177 *) test_case_count t
167167
| TestList l ->
168168
(* 23 *) List.fold_left
169-
(fun c t -> (* 175 *) c + test_case_count t)
169+
(fun c t -> (* 176 *) c + test_case_count t)
170170
0 l
171171

172172
let string_of_path path =
173-
(* 306 *) String.concat ":" (List.rev_map string_of_node path)
173+
(* 308 *) String.concat ":" (List.rev_map string_of_node path)
174174

175175
let buff_format_printf f =
176176
(* 0 *) let buff = Buffer.create 13 in
@@ -194,12 +194,12 @@ let mapi f l =
194194

195195
let fold_lefti f accu l =
196196
(* 23 *) let rec rfold_lefti cnt accup l =
197-
(* 198 *) match l with
197+
(* 199 *) match l with
198198
| [] ->
199199
(* 23 *) accup
200200

201201
| h::t ->
202-
(* 175 *) rfold_lefti (cnt + 1) (f accup h cnt) t
202+
(* 176 *) rfold_lefti (cnt + 1) (f accup h cnt) t
203203
in
204204
rfold_lefti 0 accu l
205205

@@ -217,7 +217,7 @@ open OUnitUtils
217217
type event_type = GlobalEvent of global_event | TestEvent of test_event
218218

219219
let format_event verbose event_type =
220-
(* 920 *) match event_type with
220+
(* 926 *) match event_type with
221221
| GlobalEvent e ->
222222
(* 2 *) begin
223223
match e with
@@ -276,31 +276,31 @@ let format_event verbose event_type =
276276
end
277277

278278
| TestEvent e ->
279-
(* 918 *) begin
279+
(* 924 *) begin
280280
let string_of_result =
281281
if verbose then
282-
(* 459 *) function
283-
| RSuccess _ -> (* 153 *) "ok\n"
282+
(* 462 *) function
283+
| RSuccess _ -> (* 154 *) "ok\n"
284284
| RFailure (_, _) -> (* 0 *) "FAIL\n"
285285
| RError (_, _) -> (* 0 *) "ERROR\n"
286286
| RSkip (_, _) -> (* 0 *) "SKIP\n"
287287
| RTodo (_, _) -> (* 0 *) "TODO\n"
288288
else
289-
(* 459 *) function
290-
| RSuccess _ -> (* 153 *) "."
289+
(* 462 *) function
290+
| RSuccess _ -> (* 154 *) "."
291291
| RFailure (_, _) -> (* 0 *) "F"
292292
| RError (_, _) -> (* 0 *) "E"
293293
| RSkip (_, _) -> (* 0 *) "S"
294294
| RTodo (_, _) -> (* 0 *) "T"
295295
in
296296
if verbose then
297-
(* 459 *) match e with
297+
(* 462 *) match e with
298298
| EStart p ->
299-
(* 153 *) Printf.sprintf "%s start\n" (string_of_path p)
299+
(* 154 *) Printf.sprintf "%s start\n" (string_of_path p)
300300
| EEnd p ->
301-
(* 153 *) Printf.sprintf "%s end\n" (string_of_path p)
301+
(* 154 *) Printf.sprintf "%s end\n" (string_of_path p)
302302
| EResult result ->
303-
(* 153 *) string_of_result result
303+
(* 154 *) string_of_result result
304304
| ELog (lvl, str) ->
305305
(* 0 *) let prefix =
306306
match lvl with
@@ -312,21 +312,21 @@ let format_event verbose event_type =
312312
| ELogRaw str ->
313313
(* 0 *) str
314314
else
315-
(* 459 *) match e with
316-
| EStart _ | EEnd _ | ELog _ | ELogRaw _ -> (* 306 *) ""
317-
| EResult result -> (* 153 *) string_of_result result
315+
(* 462 *) match e with
316+
| EStart _ | EEnd _ | ELog _ | ELogRaw _ -> (* 308 *) ""
317+
| EResult result -> (* 154 *) string_of_result result
318318
end
319319

320320
let file_logger fn =
321321
(* 1 *) let chn = open_out fn in
322322
(fun ev ->
323-
(* 460 *) output_string chn (format_event true ev);
323+
(* 463 *) output_string chn (format_event true ev);
324324
flush chn),
325325
(fun () -> (* 1 *) close_out chn)
326326

327327
let std_logger verbose =
328328
(* 1 *) (fun ev ->
329-
(* 460 *) print_string (format_event verbose ev);
329+
(* 463 *) print_string (format_event verbose ev);
330330
flush stdout),
331331
(fun () -> (* 1 *) ())
332332

@@ -343,7 +343,7 @@ let create output_file_opt verbose (log,close) =
343343
(* 0 *) null_logger
344344
in
345345
(fun ev ->
346-
(* 460 *) std_log ev; file_log ev; log ev),
346+
(* 463 *) std_log ev; file_log ev; log ev),
347347
(fun () ->
348348
(* 1 *) std_close (); file_close (); close ())
349349

@@ -705,7 +705,7 @@ let assert_failure msg =
705705
(* 0 *) failwith ("OUnit: " ^ msg)
706706

707707
let assert_bool msg b =
708-
(* 2009418 *) if not b then (* 0 *) assert_failure msg
708+
(* 2009421 *) if not b then (* 0 *) assert_failure msg
709709

710710
let assert_string str =
711711
(* 0 *) if not (str = "") then (* 0 *) assert_failure str
@@ -951,7 +951,7 @@ let (@?) = assert_bool
951951

952952
(* Some shorthands which allows easy test construction *)
953953
let (>:) s t = (* 0 *) TestLabel(s, t) (* infix *)
954-
let (>::) s f = (* 153 *) TestLabel(s, TestCase(f)) (* infix *)
954+
let (>::) s f = (* 154 *) TestLabel(s, TestCase(f)) (* infix *)
955955
let (>:::) s l = (* 23 *) TestLabel(s, TestList(l)) (* infix *)
956956

957957
(* Utility function to manipulate test *)
@@ -1087,7 +1087,7 @@ let maybe_backtrace = ""
10871087
(* Run all tests, report starts, errors, failures, and return the results *)
10881088
let perform_test report test =
10891089
(* 1 *) let run_test_case f path =
1090-
(* 153 *) try
1090+
(* 154 *) try
10911091
f ();
10921092
RSuccess path
10931093
with
@@ -1106,22 +1106,22 @@ let perform_test report test =
11061106
let rec flatten_test path acc =
11071107
function
11081108
| TestCase(f) ->
1109-
(* 153 *) (path, f) :: acc
1109+
(* 154 *) (path, f) :: acc
11101110

11111111
| TestList (tests) ->
11121112
(* 23 *) fold_lefti
11131113
(fun acc t cnt ->
1114-
(* 175 *) flatten_test
1114+
(* 176 *) flatten_test
11151115
((ListItem cnt)::path)
11161116
acc t)
11171117
acc tests
11181118

11191119
| TestLabel (label, t) ->
1120-
(* 176 *) flatten_test ((Label label)::path) acc t
1120+
(* 177 *) flatten_test ((Label label)::path) acc t
11211121
in
11221122
let test_cases = List.rev (flatten_test [] [] test) in
11231123
let runner (path, f) =
1124-
(* 153 *) let result =
1124+
(* 154 *) let result =
11251125
report (EStart path);
11261126
run_test_case f path
11271127
in
@@ -1130,18 +1130,18 @@ let perform_test report test =
11301130
result
11311131
in
11321132
let rec iter state =
1133-
(* 154 *) match state.tests_planned with
1133+
(* 155 *) match state.tests_planned with
11341134
| [] ->
11351135
(* 1 *) state.results
11361136
| _ ->
1137-
(* 153 *) let (path, f) = !global_chooser state in
1137+
(* 154 *) let (path, f) = !global_chooser state in
11381138
let result = runner (path, f) in
11391139
iter
11401140
{
11411141
results = result :: state.results;
11421142
tests_planned =
11431143
List.filter
1144-
(fun (path', _) -> (* 11781 *) path <> path') state.tests_planned
1144+
(fun (path', _) -> (* 11935 *) path <> path') state.tests_planned
11451145
}
11461146
in
11471147
iter {results = []; tests_planned = test_cases}
@@ -1171,7 +1171,7 @@ let run_test_tt ?verbose test =
11711171
time_fun
11721172
perform_test
11731173
(fun ev ->
1174-
(* 459 *) log (OUnitLogger.TestEvent ev))
1174+
(* 462 *) log (OUnitLogger.TestEvent ev))
11751175
test
11761176
in
11771177

@@ -8986,6 +8986,15 @@ val exclude_tail : 'a list -> 'a * 'a list
89868986

89878987
val length_compare : 'a list -> int -> [`Gt | `Eq | `Lt ]
89888988

8989+
(**
8990+
8991+
{[length xs = length ys + n ]}
8992+
input n should be positive
8993+
TODO: input checking
8994+
*)
8995+
8996+
val length_larger_than_n :
8997+
int -> 'a list -> 'a list -> bool
89898998

89908999
val filter_map2 : ('a -> 'b -> 'c option) -> 'a list -> 'b list -> 'c list
89919000

@@ -9270,14 +9279,25 @@ let try_take n l =
92709279

92719280

92729281
let rec length_compare l n =
9273-
(* 13 *) if n < 0 then (* 2 *) `Gt
9282+
(* 19 *) if n < 0 then (* 2 *) `Gt
92749283
else
9275-
(* 11 *) begin match l with
9276-
| _ ::xs -> (* 8 *) length_compare xs (n - 1)
9284+
(* 17 *) begin match l with
9285+
| _ ::xs -> (* 11 *) length_compare xs (n - 1)
92779286
| [] ->
9278-
(* 3 *) if n = 0 then (* 2 *) `Eq
9287+
(* 6 *) if n = 0 then (* 5 *) `Eq
92799288
else (* 1 *) `Lt
92809289
end
9290+
(**
9291+
9292+
{[length xs = length ys + n ]}
9293+
*)
9294+
let rec length_larger_than_n n xs ys =
9295+
(* 6 *) match xs, ys with
9296+
| _, [] -> (* 3 *) length_compare xs n = `Eq
9297+
| _::xs, _::ys ->
9298+
(* 3 *) length_larger_than_n n xs ys
9299+
| [], _ -> (* 0 *) false
9300+
92819301

92829302

92839303
let exclude_tail (x : 'a list) =
@@ -9542,6 +9562,15 @@ let suites =
95429562
(Ext_list.length_compare [] (-1)) `Gt ;
95439563
OUnit.assert_equal
95449564
(Ext_list.length_compare [] (0)) `Eq ;
9565+
end;
9566+
__LOC__ >:: begin fun _ ->
9567+
(* 1 *) OUnit.assert_bool __LOC__
9568+
(Ext_list.length_larger_than_n 1 [1;2] [1]);
9569+
OUnit.assert_bool __LOC__
9570+
(Ext_list.length_larger_than_n 0 [1;2] [1;2]);
9571+
OUnit.assert_bool __LOC__
9572+
(Ext_list.length_larger_than_n 2 [1;2] [])
9573+
95459574
end
95469575

95479576
]

jscomp/bin/all_ounit_tests.ml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8986,6 +8986,15 @@ val exclude_tail : 'a list -> 'a * 'a list
89868986

89878987
val length_compare : 'a list -> int -> [`Gt | `Eq | `Lt ]
89888988

8989+
(**
8990+
8991+
{[length xs = length ys + n ]}
8992+
input n should be positive
8993+
TODO: input checking
8994+
*)
8995+
8996+
val length_larger_than_n :
8997+
int -> 'a list -> 'a list -> bool
89898998

89908999
val filter_map2 : ('a -> 'b -> 'c option) -> 'a list -> 'b list -> 'c list
89919000

@@ -9278,6 +9287,17 @@ let rec length_compare l n =
92789287
if n = 0 then `Eq
92799288
else `Lt
92809289
end
9290+
(**
9291+
9292+
{[length xs = length ys + n ]}
9293+
*)
9294+
let rec length_larger_than_n n xs ys =
9295+
match xs, ys with
9296+
| _, [] -> length_compare xs n = `Eq
9297+
| _::xs, _::ys ->
9298+
length_larger_than_n n xs ys
9299+
| [], _ -> false
9300+
92819301

92829302

92839303
let exclude_tail (x : 'a list) =
@@ -9542,6 +9562,15 @@ let suites =
95429562
(Ext_list.length_compare [] (-1)) `Gt ;
95439563
OUnit.assert_equal
95449564
(Ext_list.length_compare [] (0)) `Eq ;
9565+
end;
9566+
__LOC__ >:: begin fun _ ->
9567+
OUnit.assert_bool __LOC__
9568+
(Ext_list.length_larger_than_n 1 [1;2] [1]);
9569+
OUnit.assert_bool __LOC__
9570+
(Ext_list.length_larger_than_n 0 [1;2] [1;2]);
9571+
OUnit.assert_bool __LOC__
9572+
(Ext_list.length_larger_than_n 2 [1;2] [])
9573+
95459574
end
95469575

95479576
]

0 commit comments

Comments
 (0)