Skip to content

Commit 78e29c1

Browse files
cknittclaude
andauthored
Check record labels once per record type (#8799)
* Check record labels once per record type Record expressions and patterns without a known type checked every field against all labels of each candidate record type, which is quadratic in the number of fields. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Knittel <ck@cca.io> * Add CHANGELOG entry Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Christoph Knittel <ck@cca.io> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 904027b commit 78e29c1

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
#### :nail_care: Polish
6262

63+
- Speed up type checking of record expressions and patterns with many fields whose type is not known from context. https://github.com/rescript-lang/rescript/pull/8799
6364
- Print list literals with one list cell per line, so their output no longer grows quadratically with their length. https://github.com/rescript-lang/rescript/pull/8794
6465
- Compile dict spreads to object spread syntax instead of `Object.assign`. https://github.com/rescript-lang/rescript/pull/8747
6566
- Make compilation slightly faster and stop rewatch from copying files twice for modules with an interface. https://github.com/rescript-lang/rescript/pull/8769

‎compiler/ml/typecore.ml‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -995,36 +995,55 @@ module Label = Name_choice (struct
995995
let unbound_name_error = Typetexp.unbound_label_error
996996
end)
997997
998-
let disambiguate_label_by_ids closed ids labels =
999-
let check_ids (lbl, _) =
998+
(* [ids] are the fields of one record expression or pattern. Whether a
999+
candidate label fits them only depends on the label's record type, so the
1000+
checks are done once per type ([lbl_all] is shared by its labels) rather
1001+
than for every field, which would be quadratic in the number of fields. *)
1002+
let disambiguate_label_by_ids closed ids =
1003+
let num_ids = List.length ids in
1004+
let check_ids lbl_all =
10001005
(* check that all ids are present *)
10011006
let lbls = Hashtbl.create 8 in
1002-
Array.iter (fun lbl -> Hashtbl.add lbls lbl.lbl_name ()) lbl.lbl_all;
1007+
Array.iter (fun lbl -> Hashtbl.add lbls lbl.lbl_name ()) lbl_all;
10031008
List.for_all (Hashtbl.mem lbls) ids
10041009
in
1005-
let mandatory_labels_are_present num_ids lbl =
1010+
let mandatory_labels_are_present lbl_all =
10061011
(* check that all mandatory labels are present *)
1007-
let has_optional_labels = Array.exists label_is_optional lbl.lbl_all in
1012+
let has_optional_labels = Array.exists label_is_optional lbl_all in
10081013
if has_optional_labels then (
10091014
let mandatory_lbls = ref 0 in
1010-
lbl.lbl_all
1015+
lbl_all
10111016
|> Array.iter (fun l ->
10121017
if not (label_is_optional l) then incr mandatory_lbls);
10131018
num_ids >= !mandatory_lbls)
1014-
else num_ids = Array.length lbl.lbl_all
1019+
else num_ids = Array.length lbl_all
10151020
in
1016-
let check_closed (lbl, _) =
1017-
(not closed) || mandatory_labels_are_present (List.length ids) lbl
1021+
let checked = ref [] in
1022+
let check lbl_all =
1023+
match List.assq lbl_all !checked with
1024+
| result -> result
1025+
| exception Not_found ->
1026+
let result =
1027+
(check_ids lbl_all, (not closed) || mandatory_labels_are_present lbl_all)
1028+
in
1029+
checked := (lbl_all, result) :: !checked;
1030+
result
10181031
in
1019-
let labels' = Ext_list.filter labels check_ids in
1020-
if labels' = [] then (false, labels)
1021-
else
1022-
let labels'' = Ext_list.filter labels' check_closed in
1023-
if labels'' = [] then (false, labels') else (true, labels'')
1032+
fun labels ->
1033+
let labels' =
1034+
Ext_list.filter labels (fun (lbl, _) -> fst (check lbl.lbl_all))
1035+
in
1036+
if labels' = [] then (false, labels)
1037+
else
1038+
let labels'' =
1039+
Ext_list.filter labels' (fun (lbl, _) -> snd (check lbl.lbl_all))
1040+
in
1041+
if labels'' = [] then (false, labels') else (true, labels'')
10241042
10251043
(* Only issue warnings once per record constructor/pattern *)
10261044
let disambiguate_record_elem_list loc closed env opath record_elem_list =
10271045
let ids = List.map (fun {lid} -> Longident.last lid.txt) record_elem_list in
1046+
let disambiguate_by_ids = disambiguate_label_by_ids closed ids in
10281047
let w_amb = ref [] in
10291048
let warn loc msg =
10301049
let open Warnings in
@@ -1048,7 +1067,7 @@ let disambiguate_record_elem_list loc closed env opath record_elem_list =
10481067
let ok, labels =
10491068
match opath with
10501069
| Some _ -> (true, scope) (* disambiguate only checks scope *)
1051-
| _ -> disambiguate_label_by_ids closed ids scope
1070+
| _ -> disambiguate_by_ids scope
10521071
in
10531072
if ok then Label.disambiguate lid env opath labels ~warn ~scope
10541073
else fst (List.hd labels)

0 commit comments

Comments
 (0)