Skip to content

Commit 9de83bb

Browse files
committed
better ident hashtbl
1 parent b8cb516 commit 9de83bb

12 files changed

+447
-197
lines changed

jscomp/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ ext/ordered_hash_set_make.ml : ext/ordered_hash_set.cppo.ml
7474

7575
ext/ordered_hash_set_string.ml:ext/ordered_hash_set.cppo.ml
7676
cppo -D TYPE_STRING $< -o $@
77+
78+
ext/string_hashtbl.ml: ext/hashtbl.cppo.ml
79+
cppo -D TYPE_STRING $< -o $@
80+
ext/int_hashtbl.ml: ext/hashtbl.cppo.ml
81+
cppo -D TYPE_INT $< -o $@
82+
ext/ident_hashtbl.ml: ext/hashtbl.cppo.ml
83+
cppo -D TYPE_IDENT $< -o $@
7784
## Stubs
7885
.c.o:
7986
$(NATIVE) -ccopt -o -ccopt $@ -c $<

jscomp/bin/all_ounit_tests.i.ml

Lines changed: 57 additions & 37 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-
(* 70 *) List.hd state.tests_planned
78+
(* 72 *) 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 _ -> (* 140 *) false
101+
| RSuccess _ | RError _ | RSkip _ | RTodo _ -> (* 144 *) false
102102

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

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

113113
let is_todo =
114114
function
115115
| RTodo _ -> (* 0 *) true
116-
| RSuccess _ | RFailure _ | RError _ | RSkip _ -> (* 140 *) false
116+
| RSuccess _ | RFailure _ | RError _ | RSkip _ -> (* 144 *) 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-
(* 210 *) was_successful t
148+
(* 216 *) 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-
(* 280 *) string_of_int n
158+
(* 288 *) string_of_int n
159159
| Label s ->
160-
(* 420 *) s
160+
(* 432 *) s
161161

162162
(* Return the number of available tests *)
163163
let rec test_case_count =
164164
function
165-
| TestCase _ -> (* 70 *) 1
166-
| TestLabel (_, t) -> (* 83 *) test_case_count t
165+
| TestCase _ -> (* 72 *) 1
166+
| TestLabel (_, t) -> (* 85 *) test_case_count t
167167
| TestList l ->
168168
(* 13 *) List.fold_left
169-
(fun c t -> (* 82 *) c + test_case_count t)
169+
(fun c t -> (* 84 *) c + test_case_count t)
170170
0 l
171171

172172
let string_of_path path =
173-
(* 140 *) String.concat ":" (List.rev_map string_of_node path)
173+
(* 144 *) 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
(* 13 *) let rec rfold_lefti cnt accup l =
197-
(* 95 *) match l with
197+
(* 97 *) match l with
198198
| [] ->
199199
(* 13 *) accup
200200

201201
| h::t ->
202-
(* 82 *) rfold_lefti (cnt + 1) (f accup h cnt) t
202+
(* 84 *) 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-
(* 422 *) match event_type with
220+
(* 434 *) match event_type with
221221
| GlobalEvent e ->
222222
(* 2 *) begin
223223
match e with
@@ -276,18 +276,18 @@ let format_event verbose event_type =
276276
end
277277

278278
| TestEvent e ->
279-
(* 420 *) begin
279+
(* 432 *) begin
280280
let string_of_result =
281281
if verbose then
282282
function
283-
| RSuccess _ -> (* 70 *) "ok\n"
283+
| RSuccess _ -> (* 72 *) "ok\n"
284284
| RFailure (_, _) -> (* 0 *) "FAIL\n"
285285
| RError (_, _) -> (* 0 *) "ERROR\n"
286286
| RSkip (_, _) -> (* 0 *) "SKIP\n"
287287
| RTodo (_, _) -> (* 0 *) "TODO\n"
288288
else
289289
function
290-
| RSuccess _ -> (* 70 *) "."
290+
| RSuccess _ -> (* 72 *) "."
291291
| RFailure (_, _) -> (* 0 *) "F"
292292
| RError (_, _) -> (* 0 *) "E"
293293
| RSkip (_, _) -> (* 0 *) "S"
@@ -296,11 +296,11 @@ let format_event verbose event_type =
296296
if verbose then
297297
match e with
298298
| EStart p ->
299-
(* 70 *) Printf.sprintf "%s start\n" (string_of_path p)
299+
(* 72 *) Printf.sprintf "%s start\n" (string_of_path p)
300300
| EEnd p ->
301-
(* 70 *) Printf.sprintf "%s end\n" (string_of_path p)
301+
(* 72 *) Printf.sprintf "%s end\n" (string_of_path p)
302302
| EResult result ->
303-
(* 70 *) string_of_result result
303+
(* 72 *) string_of_result result
304304
| ELog (lvl, str) ->
305305
(* 0 *) let prefix =
306306
match lvl with
@@ -313,20 +313,20 @@ let format_event verbose event_type =
313313
(* 0 *) str
314314
else
315315
match e with
316-
| EStart _ | EEnd _ | ELog _ | ELogRaw _ -> (* 140 *) ""
317-
| EResult result -> (* 70 *) string_of_result result
316+
| EStart _ | EEnd _ | ELog _ | ELogRaw _ -> (* 144 *) ""
317+
| EResult result -> (* 72 *) string_of_result result
318318
end
319319

320320
let file_logger fn =
321321
(* 1 *) let chn = open_out fn in
322322
(fun ev ->
323-
(* 211 *) output_string chn (format_event true ev);
323+
(* 217 *) 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-
(* 211 *) print_string (format_event verbose ev);
329+
(* 217 *) 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-
(* 211 *) std_log ev; file_log ev; log ev),
346+
(* 217 *) std_log ev; file_log ev; log ev),
347347
(fun () ->
348348
(* 1 *) std_close (); file_close (); close ())
349349

@@ -709,7 +709,7 @@ let assert_string str =
709709
(* 0 *) if not (str = "") then assert_failure str
710710

711711
let assert_equal ?(cmp = ( = )) ?printer ?pp_diff ?msg expected actual =
712-
(* 2000387 *) let get_error_string () =
712+
(* 2000391 *) let get_error_string () =
713713
(* 0 *) let res =
714714
buff_format_printf
715715
(fun fmt ->
@@ -925,7 +925,7 @@ let (@?) = assert_bool
925925

926926
(* Some shorthands which allows easy test construction *)
927927
let (>:) s t = (* 0 *) TestLabel(s, t) (* infix *)
928-
let (>::) s f = (* 70 *) TestLabel(s, TestCase(f)) (* infix *)
928+
let (>::) s f = (* 72 *) TestLabel(s, TestCase(f)) (* infix *)
929929
let (>:::) s l = (* 13 *) TestLabel(s, TestList(l)) (* infix *)
930930

931931
(* Utility function to manipulate test *)
@@ -1061,7 +1061,7 @@ let maybe_backtrace = ""
10611061
(* Run all tests, report starts, errors, failures, and return the results *)
10621062
let perform_test report test =
10631063
(* 1 *) let run_test_case f path =
1064-
(* 70 *) try
1064+
(* 72 *) try
10651065
f ();
10661066
RSuccess path
10671067
with
@@ -1080,22 +1080,22 @@ let perform_test report test =
10801080
let rec flatten_test path acc =
10811081
function
10821082
| TestCase(f) ->
1083-
(* 70 *) (path, f) :: acc
1083+
(* 72 *) (path, f) :: acc
10841084

10851085
| TestList (tests) ->
10861086
(* 13 *) fold_lefti
10871087
(fun acc t cnt ->
1088-
(* 82 *) flatten_test
1088+
(* 84 *) flatten_test
10891089
((ListItem cnt)::path)
10901090
acc t)
10911091
acc tests
10921092

10931093
| TestLabel (label, t) ->
1094-
(* 83 *) flatten_test ((Label label)::path) acc t
1094+
(* 85 *) flatten_test ((Label label)::path) acc t
10951095
in
10961096
let test_cases = List.rev (flatten_test [] [] test) in
10971097
let runner (path, f) =
1098-
(* 70 *) let result =
1098+
(* 72 *) let result =
10991099
report (EStart path);
11001100
run_test_case f path
11011101
in
@@ -1104,18 +1104,18 @@ let perform_test report test =
11041104
result
11051105
in
11061106
let rec iter state =
1107-
(* 71 *) match state.tests_planned with
1107+
(* 73 *) match state.tests_planned with
11081108
| [] ->
11091109
(* 1 *) state.results
11101110
| _ ->
1111-
(* 70 *) let (path, f) = !global_chooser state in
1111+
(* 72 *) let (path, f) = !global_chooser state in
11121112
let result = runner (path, f) in
11131113
iter
11141114
{
11151115
results = result :: state.results;
11161116
tests_planned =
11171117
List.filter
1118-
(fun (path', _) -> (* 2485 *) path <> path') state.tests_planned
1118+
(fun (path', _) -> (* 2628 *) path <> path') state.tests_planned
11191119
}
11201120
in
11211121
iter {results = []; tests_planned = test_cases}
@@ -1145,7 +1145,7 @@ let run_test_tt ?verbose test =
11451145
time_fun
11461146
perform_test
11471147
(fun ev ->
1148-
(* 210 *) log (OUnitLogger.TestEvent ev))
1148+
(* 216 *) log (OUnitLogger.TestEvent ev))
11491149
test
11501150
in
11511151

@@ -3614,6 +3614,12 @@ external hash_string : string -> int = "caml_bs_hash_string" "noalloc";;
36143614

36153615
external hash_string_int : string -> int -> int = "caml_bs_hash_string_and_int" "noalloc";;
36163616

3617+
external hash_string_small_int : string -> int -> int = "caml_bs_hash_string_and_small_int" "noalloc";;
3618+
3619+
external hash_stamp_and_name : int -> string -> int = "caml_bs_hash_stamp_and_name" "noalloc";;
3620+
3621+
external hash_small_int : int -> int = "caml_bs_hash_small_int" "noalloc";;
3622+
36173623
external hash_int : int -> int = "caml_bs_hash_int" "noalloc";;
36183624

36193625
end
@@ -4303,6 +4309,9 @@ let bench () =
43034309
done
43044310
end
43054311

4312+
4313+
type id (* = Ident.t *) = { stamp : int; name : string; mutable flags : int; }
4314+
let hash id = (* 4 *) Bs_hash_stubs.hash_stamp_and_name id.stamp id.name
43064315
let suites =
43074316
__FILE__
43084317
>:::
@@ -4324,7 +4333,18 @@ let suites =
43244333
(* 1 *) Array.init 100 (fun i -> (* 100 *) String.make i 'a' )
43254334
|> Array.iter (fun x ->
43264335
(* 100 *) Bs_hash_stubs.hash_string x =~ Hashtbl.hash x)
4336+
end;
4337+
__LOC__ >:: begin fun _ ->
4338+
(** only stamp matters here *)
4339+
(* 1 *) hash {stamp = 1 ; name = "xx"; flags = 0} =~ Bs_hash_stubs.hash_small_int 1 ;
4340+
hash {stamp = 11 ; name = "xx"; flags = 0} =~ Bs_hash_stubs.hash_small_int 11;
4341+
end;
4342+
__LOC__ >:: begin fun _ ->
4343+
(* only string matters here *)
4344+
(* 1 *) hash {stamp = 0 ; name = "Pervasives"; flags = 0} =~ Bs_hash_stubs.hash_string "Pervasives";
4345+
hash {stamp = 0 ; name = "UU"; flags = 0} =~ Bs_hash_stubs.hash_string "UU";
43274346
end
4347+
43284348
]
43294349

43304350
end

jscomp/bin/all_ounit_tests.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,6 +3614,12 @@ external hash_string : string -> int = "caml_bs_hash_string" "noalloc";;
36143614

36153615
external hash_string_int : string -> int -> int = "caml_bs_hash_string_and_int" "noalloc";;
36163616

3617+
external hash_string_small_int : string -> int -> int = "caml_bs_hash_string_and_small_int" "noalloc";;
3618+
3619+
external hash_stamp_and_name : int -> string -> int = "caml_bs_hash_stamp_and_name" "noalloc";;
3620+
3621+
external hash_small_int : int -> int = "caml_bs_hash_small_int" "noalloc";;
3622+
36173623
external hash_int : int -> int = "caml_bs_hash_int" "noalloc";;
36183624

36193625
end
@@ -4303,6 +4309,9 @@ let bench () =
43034309
done
43044310
end
43054311

4312+
4313+
type id (* = Ident.t *) = { stamp : int; name : string; mutable flags : int; }
4314+
let hash id = Bs_hash_stubs.hash_stamp_and_name id.stamp id.name
43064315
let suites =
43074316
__FILE__
43084317
>:::
@@ -4324,7 +4333,18 @@ let suites =
43244333
Array.init 100 (fun i -> String.make i 'a' )
43254334
|> Array.iter (fun x ->
43264335
Bs_hash_stubs.hash_string x =~ Hashtbl.hash x)
4336+
end;
4337+
__LOC__ >:: begin fun _ ->
4338+
(** only stamp matters here *)
4339+
hash {stamp = 1 ; name = "xx"; flags = 0} =~ Bs_hash_stubs.hash_small_int 1 ;
4340+
hash {stamp = 11 ; name = "xx"; flags = 0} =~ Bs_hash_stubs.hash_small_int 11;
4341+
end;
4342+
__LOC__ >:: begin fun _ ->
4343+
(* only string matters here *)
4344+
hash {stamp = 0 ; name = "Pervasives"; flags = 0} =~ Bs_hash_stubs.hash_string "Pervasives";
4345+
hash {stamp = 0 ; name = "UU"; flags = 0} =~ Bs_hash_stubs.hash_string "UU";
43274346
end
4347+
43284348
]
43294349

43304350
end

0 commit comments

Comments
 (0)