Skip to content

Commit 70d1642

Browse files
committed
Deprecate polymorphic hashtbl
1 parent 13948f4 commit 70d1642

27 files changed

+490
-404
lines changed

compiler/bin-js_of_ocaml/compile.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ let run
183183
if not (Sys.file_exists file)
184184
then failwith (Printf.sprintf "export file %S does not exist" file);
185185
let ic = open_in_text file in
186-
let t = Hashtbl.create 17 in
186+
let t = String.Hashtbl.create 17 in
187187
(try
188188
while true do
189-
Hashtbl.add t (String.trim (In_channel.input_line_exn ic)) ()
189+
String.Hashtbl.add t (String.trim (In_channel.input_line_exn ic)) ()
190190
done;
191191
assert false
192192
with End_of_file -> ());
193193
close_in ic;
194-
Some (Hashtbl.fold (fun cmi () acc -> cmi :: acc) t [])
194+
Some (String.Hashtbl.fold (fun cmi () acc -> cmi :: acc) t [])
195195
in
196196
let runtime_files =
197197
if (not no_runtime) && (toplevel || dynlink)

compiler/lib-wasm/code_generation.ml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ type constant_global =
3737
}
3838

3939
type context =
40-
{ constants : (Var.t, W.expression) Hashtbl.t
40+
{ constants : W.expression Var.Hashtbl.t
4141
; mutable data_segments : string Var.Map.t
4242
; mutable constant_globals : constant_global Var.Map.t
4343
; mutable other_fields : W.module_field list
4444
; mutable imports : (Var.t * Wasm_ast.import_desc) StringMap.t StringMap.t
45-
; type_names : (string, Var.t) Hashtbl.t
46-
; types : (Var.t, Wasm_ast.type_field) Hashtbl.t
45+
; type_names : Var.t String.Hashtbl.t
46+
; types : Wasm_ast.type_field Var.Hashtbl.t
4747
; mutable closure_envs : Var.t Var.Map.t
4848
(** GC: mapping of recursive functions to their shared environment *)
4949
; mutable apply_funs : Var.t IntMap.t
@@ -63,13 +63,13 @@ type context =
6363
}
6464

6565
let make_context ~value_type =
66-
{ constants = Hashtbl.create 128
66+
{ constants = Var.Hashtbl.create 128
6767
; data_segments = Var.Map.empty
6868
; constant_globals = Var.Map.empty
6969
; other_fields = []
7070
; imports = StringMap.empty
71-
; type_names = Hashtbl.create 128
72-
; types = Hashtbl.create 128
71+
; type_names = String.Hashtbl.create 128
72+
; types = Var.Hashtbl.create 128
7373
; closure_envs = Var.Map.empty
7474
; apply_funs = IntMap.empty
7575
; cps_apply_funs = IntMap.empty
@@ -126,7 +126,7 @@ let register_data_segment x v st =
126126
let get_context st = st.context, st
127127

128128
let register_constant x e st =
129-
Hashtbl.add st.context.constants x e;
129+
Var.Hashtbl.add st.context.constants x e;
130130
(), st
131131

132132
type type_def =
@@ -138,21 +138,21 @@ type type_def =
138138
let register_type nm gen_typ st =
139139
let context = st.context in
140140
let { supertype; final; typ }, st = gen_typ () st in
141-
( (try Hashtbl.find context.type_names nm
141+
( (try String.Hashtbl.find context.type_names nm
142142
with Not_found ->
143143
let name = Var.fresh_n nm in
144144
let type_field = { Wasm_ast.name; typ; supertype; final } in
145145
context.other_fields <- Type [ type_field ] :: context.other_fields;
146-
Hashtbl.add context.type_names nm name;
147-
Hashtbl.add context.types name type_field;
146+
String.Hashtbl.add context.type_names nm name;
147+
Var.Hashtbl.add context.types name type_field;
148148
name)
149149
, st )
150150

151151
let rec type_index_sub ty ty' st =
152152
if Var.equal ty ty'
153153
then true, st
154154
else
155-
let type_field = Hashtbl.find st.context.types ty in
155+
let type_field = Var.Hashtbl.find st.context.types ty in
156156
match type_field.supertype with
157157
| None -> false, st
158158
| Some ty -> type_index_sub ty ty' st
@@ -168,20 +168,20 @@ let heap_type_sub (ty : W.heap_type) (ty' : W.heap_type) st =
168168
| (None_ | I31), I31 -> true, st
169169
| None_, None_ -> true, st
170170
| Type t, Struct ->
171-
( (let type_field = Hashtbl.find st.context.types t in
171+
( (let type_field = Var.Hashtbl.find st.context.types t in
172172
match type_field.typ with
173173
| Struct _ -> true
174174
| Array _ | Func _ -> false)
175175
, st )
176176
| Type t, Array ->
177-
( (let type_field = Hashtbl.find st.context.types t in
177+
( (let type_field = Var.Hashtbl.find st.context.types t in
178178
match type_field.typ with
179179
| Array _ -> true
180180
| Struct _ | Func _ -> false)
181181
, st )
182182
| Type t, Type t' -> type_index_sub t t' st
183183
| None_, Type t ->
184-
( (let type_field = Hashtbl.find st.context.types t in
184+
( (let type_field = Var.Hashtbl.find st.context.types t in
185185
match type_field.typ with
186186
| Struct _ | Array _ -> true
187187
| Func _ -> false)
@@ -282,7 +282,7 @@ let unit_name st = st.context.unit_name, st
282282

283283
let var x st =
284284
try Var.Map.find x st.vars, st
285-
with Not_found -> Expr (return (Hashtbl.find st.context.constants x)), st
285+
with Not_found -> Expr (return (Var.Hashtbl.find st.context.constants x)), st
286286

287287
let add_var ?typ x ({ var_count; vars; _ } as st) =
288288
match Var.Map.find_opt x vars with

compiler/lib-wasm/code_generation.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ open Stdlib
2121
type constant_global
2222

2323
type context =
24-
{ constants : (Code.Var.t, Wasm_ast.expression) Hashtbl.t
24+
{ constants : Wasm_ast.expression Code.Var.Hashtbl.t
2525
; mutable data_segments : string Code.Var.Map.t
2626
; mutable constant_globals : constant_global Code.Var.Map.t
2727
; mutable other_fields : Wasm_ast.module_field list
2828
; mutable imports : (Code.Var.t * Wasm_ast.import_desc) StringMap.t StringMap.t
29-
; type_names : (string, Code.Var.t) Hashtbl.t
30-
; types : (Code.Var.t, Wasm_ast.type_field) Hashtbl.t
29+
; type_names : Code.Var.t String.Hashtbl.t
30+
; types : Wasm_ast.type_field Code.Var.Hashtbl.t
3131
; mutable closure_envs : Code.Var.t Code.Var.Map.t
3232
(** GC: mapping of recursive functions to their shared environment *)
3333
; mutable apply_funs : Code.Var.t Stdlib.IntMap.t

compiler/lib-wasm/gc_target.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,7 @@ end
10681068
module Closure = struct
10691069
let get_free_variables ~context info =
10701070
List.filter
1071-
~f:(fun x -> not (Hashtbl.mem context.constants x))
1071+
~f:(fun x -> not (Code.Var.Hashtbl.mem context.constants x))
10721072
info.Closure_conversion.free_variables
10731073

10741074
let rec is_last_fun l f =

compiler/lib-wasm/generate.ml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ module Generate (Target : Target_sig.S) = struct
9898
| Int64 -> Memory.unbox_int64 e
9999

100100
let specialized_primitives =
101-
let h = Hashtbl.create 18 in
101+
let h = String.Hashtbl.create 18 in
102102
List.iter
103-
~f:(fun (nm, typ) -> Hashtbl.add h nm typ)
103+
~f:(fun (nm, typ) -> String.Hashtbl.add h nm typ)
104104
[ "caml_int32_bswap", (`Pure, [ Int32 ], Int32)
105105
; "caml_nativeint_bswap", (`Pure, [ Nativeint ], Nativeint)
106106
; "caml_int64_bswap", (`Pure, [ Int64 ], Int64)
@@ -182,14 +182,14 @@ module Generate (Target : Target_sig.S) = struct
182182
| Pc c -> Constant.translate c
183183

184184
let internal_primitives =
185-
let h = Hashtbl.create 128 in
185+
let h = String.Hashtbl.create 128 in
186186
List.iter
187187
~f:(fun (nm, k, f) ->
188-
Hashtbl.add h nm (k, fun _ _ transl_prim_arg l -> f transl_prim_arg l))
188+
String.Hashtbl.add h nm (k, fun _ _ transl_prim_arg l -> f transl_prim_arg l))
189189
internal_primitives;
190190
h
191191

192-
let register_prim name k f = Hashtbl.add internal_primitives name (k, f)
192+
let register_prim name k f = String.Hashtbl.add internal_primitives name (k, f)
193193

194194
let invalid_arity name l ~expected =
195195
failwith
@@ -698,15 +698,20 @@ module Generate (Target : Target_sig.S) = struct
698698
Value.unit
699699
| Prim (p, l) -> (
700700
match p with
701-
| Extern name when Hashtbl.mem internal_primitives name ->
702-
snd (Hashtbl.find internal_primitives name) ctx context transl_prim_arg l
701+
| Extern name when String.Hashtbl.mem internal_primitives name ->
702+
snd
703+
(String.Hashtbl.find internal_primitives name)
704+
ctx
705+
context
706+
transl_prim_arg
707+
l
703708
| _ -> (
704709
let l = List.map ~f:transl_prim_arg l in
705710
match p, l with
706711
| Extern name, l -> (
707712
try
708713
let ((_, arg_typ, res_typ) as typ) =
709-
Hashtbl.find specialized_primitives name
714+
String.Hashtbl.find specialized_primitives name
710715
in
711716
let* f = register_import ~name (Fun (specialized_primitive_type typ)) in
712717
let rec loop acc arg_typ l =
@@ -1243,10 +1248,10 @@ module Generate (Target : Target_sig.S) = struct
12431248
let init () =
12441249
Primitive.register "caml_make_array" `Mutable None None;
12451250
Primitive.register "caml_array_of_uniform_array" `Mutable None None;
1246-
Hashtbl.iter
1251+
String.Hashtbl.iter
12471252
(fun name (k, _) -> Primitive.register name k None None)
12481253
internal_primitives;
1249-
Hashtbl.iter
1254+
String.Hashtbl.iter
12501255
(fun name (k, _, _) -> Primitive.register name k None None)
12511256
specialized_primitives
12521257
end

compiler/lib-wasm/link.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ let link_to_directory ~files_to_link ~files ~enable_source_maps ~dir =
617617
runtime :: prelude :: List.map ~f:fst lst, (runtime_intf, List.map ~f:snd lst)
618618

619619
let compute_dependencies ~files_to_link ~files =
620-
let h = Hashtbl.create 128 in
620+
let h = String.Hashtbl.create 128 in
621621
let i = ref 2 in
622622
List.filter_map
623623
~f:(fun (file, (_, units)) ->
@@ -628,13 +628,13 @@ let compute_dependencies ~files_to_link ~files =
628628
~f:(fun s { unit_info; _ } ->
629629
StringSet.fold
630630
(fun unit_name s ->
631-
try IntSet.add (Hashtbl.find h unit_name) s with Not_found -> s)
631+
try IntSet.add (String.Hashtbl.find h unit_name) s with Not_found -> s)
632632
unit_info.requires
633633
s)
634634
~init:IntSet.empty
635635
units
636636
in
637-
List.iter ~f:(fun { unit_name; _ } -> Hashtbl.add h unit_name !i) units;
637+
List.iter ~f:(fun { unit_name; _ } -> String.Hashtbl.add h unit_name !i) units;
638638
incr i;
639639
Some (Some (IntSet.elements s)))
640640
else None)

compiler/lib-wasm/wasm_link.ml

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ module Read = struct
439439
}
440440

441441
type index =
442-
{ sections : (int, section) Hashtbl.t
443-
; custom_sections : (string, section) Hashtbl.t
442+
{ sections : section Int.Hashtbl.t
443+
; custom_sections : section String.Hashtbl.t
444444
}
445445

446446
let next_section ch =
@@ -454,14 +454,16 @@ module Read = struct
454454
let skip_section ch { pos; size; _ } = seek_in ch (pos + size)
455455

456456
let index ch =
457-
let index = { sections = Hashtbl.create 16; custom_sections = Hashtbl.create 16 } in
457+
let index =
458+
{ sections = Int.Hashtbl.create 16; custom_sections = String.Hashtbl.create 16 }
459+
in
458460
let rec loop () =
459461
match next_section ch with
460462
| None -> index
461463
| Some sect ->
462464
if sect.id = 0
463-
then Hashtbl.add index.custom_sections (name ch) sect
464-
else Hashtbl.add index.sections sect.id sect;
465+
then String.Hashtbl.add index.custom_sections (name ch) sect
466+
else Int.Hashtbl.add index.sections sect.id sect;
465467
skip_section ch sect;
466468
loop ()
467469
in
@@ -480,14 +482,14 @@ module Read = struct
480482
{ ch; type_mapping = [||]; type_index_count = 0; index = index ch }
481483

482484
let find_section contents n =
483-
match Hashtbl.find contents.index.sections n with
485+
match Int.Hashtbl.find contents.index.sections n with
484486
| { pos; _ } ->
485487
seek_in contents.ch pos;
486488
true
487489
| exception Not_found -> false
488490

489491
let get_custom_section contents name =
490-
Hashtbl.find_opt contents.index.custom_sections name
492+
String.Hashtbl.find_opt contents.index.custom_sections name
491493

492494
let focus_on_custom_section contents section =
493495
let pos, limit =
@@ -1866,7 +1868,7 @@ let rec resolve
18661868
~kind
18671869
i
18681870
({ module_; name; _ } as import) =
1869-
let i', index = Hashtbl.find exports (module_, name) in
1871+
let i', index = Poly.Hashtbl.find exports (module_, name) in
18701872
let imports = get_exportable_info intfs.(i').Read.imports kind in
18711873
if index < Array.length imports
18721874
then (
@@ -1911,22 +1913,22 @@ let f files ~output_file =
19111913
add_section out_ch ~id:1 buf;
19121914

19131915
(* 2: import *)
1914-
let exports = init_exportable_info (fun _ -> Hashtbl.create 128) in
1916+
let exports = init_exportable_info (fun _ -> Poly.Hashtbl.create 128) in
19151917
Array.iteri
19161918
~f:(fun i intf ->
19171919
iter_exportable_info
19181920
(fun kind lst ->
19191921
let h = get_exportable_info exports kind in
19201922
List.iter
19211923
~f:(fun (name, index) ->
1922-
Hashtbl.add h (files.(i).module_name, name) (i, index))
1924+
Poly.Hashtbl.add h (files.(i).module_name, name) (i, index))
19231925
lst)
19241926
intf.Read.exports)
19251927
intfs;
19261928
let import_list = ref [] in
19271929
let unresolved_imports = make_exportable_info 0 in
19281930
let resolved_imports =
1929-
let tbl = Hashtbl.create 128 in
1931+
let tbl = Poly.Hashtbl.create 128 in
19301932
Array.mapi
19311933
~f:(fun i intf ->
19321934
map_exportable_info
@@ -1937,12 +1939,12 @@ let f files ~output_file =
19371939
match resolve 0 ~files ~intfs ~subtyping_info ~exports ~kind i import with
19381940
| i', idx -> Resolved (i', idx)
19391941
| exception Not_found -> (
1940-
match Hashtbl.find tbl import with
1942+
match Poly.Hashtbl.find tbl import with
19411943
| status -> status
19421944
| exception Not_found ->
19431945
let idx = get_exportable_info unresolved_imports kind in
19441946
let status = Unresolved idx in
1945-
Hashtbl.replace tbl import status;
1947+
Poly.Hashtbl.replace tbl import status;
19461948
set_exportable_info unresolved_imports kind (1 + idx);
19471949
import_list := import :: !import_list;
19481950
status))
@@ -2149,7 +2151,7 @@ let f files ~output_file =
21492151
intfs
21502152
in
21512153
Write.uint buf export_count;
2152-
let exports = Hashtbl.create 128 in
2154+
let exports = String.Hashtbl.create 128 in
21532155
Array.iteri
21542156
~f:(fun i intf ->
21552157
iter_exportable_info
@@ -2164,7 +2166,7 @@ let f files ~output_file =
21642166
in
21652167
List.iter
21662168
~f:(fun (name, idx) ->
2167-
match Hashtbl.find exports name with
2169+
match String.Hashtbl.find exports name with
21682170
| i' ->
21692171
failwith
21702172
(Printf.sprintf
@@ -2173,7 +2175,7 @@ let f files ~output_file =
21732175
files.(i').file
21742176
files.(i).file)
21752177
| exception Not_found ->
2176-
Hashtbl.add exports name i;
2178+
String.Hashtbl.add exports name i;
21772179
Write.export buf kind name map.(idx))
21782180
lst)
21792181
intf.Read.exports)

0 commit comments

Comments
 (0)