Skip to content

Commit a78975e

Browse files
committed
Optimise "include struct ... end" in more cases (ocaml/ocaml#11134)
1 parent b819c66 commit a78975e

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

lambda/translmod.ml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,8 @@ let rec more_idents = function
943943
| Tstr_class_type _ -> more_idents rem
944944
| Tstr_include{incl_mod={mod_desc =
945945
Tmod_constraint ({mod_desc = Tmod_structure str},
946-
_, _, _)}} ->
946+
_, _, _)
947+
| Tmod_structure str }} ->
947948
all_idents str.str_items @ more_idents rem
948949
| Tstr_include _ -> more_idents rem
949950
| Tstr_module
@@ -1227,15 +1228,16 @@ let transl_store_structure ~scopes glob map prims aliases str =
12271228
transl_store ~scopes rootpath (add_idents false ids subst)
12281229
cont rem)
12291230

1230-
| Tstr_include{
1231+
| Tstr_include({
12311232
incl_loc=loc;
12321233
incl_mod= {
12331234
mod_desc = Tmod_constraint (
12341235
({mod_desc = Tmod_structure str} as mexp), _, _,
1235-
(Tcoerce_structure (map, _)))};
1236+
(Tcoerce_structure _ | Tcoerce_none))}
1237+
| ({ mod_desc = Tmod_structure str} as mexp);
12361238
incl_attributes;
12371239
incl_type;
1238-
} ->
1240+
} as incl) ->
12391241
List.iter (Translattribute.check_attribute_on_module mexp)
12401242
incl_attributes;
12411243
(* Shouldn't we use mod_attributes instead of incl_attributes?
@@ -1262,9 +1264,17 @@ let transl_store_structure ~scopes glob map prims aliases str =
12621264
loop ids args))
12631265
| _ -> assert false
12641266
in
1267+
let map =
1268+
match incl.incl_mod.mod_desc with
1269+
| Tmod_constraint (_, _, _, Tcoerce_structure (map, _)) ->
1270+
map
1271+
| Tmod_structure _
1272+
| Tmod_constraint (_, _, _, Tcoerce_none) ->
1273+
List.init (List.length ids0) (fun i -> i, Tcoerce_none)
1274+
| _ -> assert false
1275+
in
12651276
Lsequence(lam, loop ids0 map)
12661277

1267-
12681278
| Tstr_include incl ->
12691279
let ids = bound_value_identifiers incl.incl_type in
12701280
let modl = incl.incl_mod in
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
(* TEST
2+
* native *)
3+
type alloc_count = { mutable total: float }
4+
let allocs = Sys.opaque_identity { total = 0. }
5+
let[@inline never] set_allocs () =
6+
allocs.total <- Gc.minor_words ()
7+
8+
let[@inline never] count txt =
9+
let now = int_of_float (Gc.minor_words () -. allocs.total) in
10+
Printf.printf "%20s: %d\n" txt now;
11+
set_allocs ()
12+
13+
let v = Sys.opaque_identity (ref 0)
14+
15+
let next () =
16+
let r = !v in incr v; r
17+
18+
let () = set_allocs ()
19+
20+
include struct
21+
let x = next ()
22+
let y = next ()
23+
end
24+
25+
let () = count "no signature"
26+
27+
include (struct
28+
let a = next ()
29+
let b = next ()
30+
end : sig val a : int val b : int end)
31+
32+
let () = count "trivial coercion"
33+
34+
include (struct
35+
let c = next ()
36+
let d = next ()
37+
end : sig val c : int end)
38+
39+
let () = count "prefix coercion"
40+
41+
include (struct
42+
let c = next ()
43+
let d = next ()
44+
end : sig val d : int end)
45+
46+
let () = count "reordering coercion"
47+
48+
let () =
49+
Printf.printf "%20s: %d%d%d%d%d%d\n" "outputs" x y a b c d
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
no signature: 0
2+
trivial coercion: 0
3+
prefix coercion: 0
4+
reordering coercion: 0
5+
outputs: 012347

0 commit comments

Comments
 (0)