Skip to content

Commit f88ccc9

Browse files
committed
Visit an arrow's argument types without a closure
Why: `iter_type_expr` visited the argument types of a `Tarrow` with `List.iter (fun {typ} -> f typ)`, which allocates a closure each time it reaches an arrow type. Many walks over types go through it (copying, marking, levels, occur checks), and function types are common. The arguments are now visited by a recursive function that takes `f`. The order of the visits is unchanged. Measured with scripts/compilerBenchmarks.js against the previous commit: allocated words -0.22% in total (-0.20% for the files compiled one by one, -0.24% in the builds of tests/tests), stress/NestedClosures.res -0.89%. On a 552 module React app (clean build, one domain) the build allocates 0.18% fewer words. Signed-off-by: Jono Prest <jjprest@gmail.com>
1 parent b82d944 commit f88ccc9

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎changes/iter-arg-types.polish.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Walk the parameters of a function type without allocating a closure, so the compiler allocates about 0.2% less.

‎compiler/ml/btype.ml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,18 @@ let rec iter_row f row =
240240
Misc.may (fun (_, l) -> List.iter f l) row.row_name
241241
| _ -> assert false
242242

243+
(* Not [List.iter] with a closure, which would be allocated at every arrow. *)
244+
let rec iter_arg_types f = function
245+
| [] -> ()
246+
| {typ} :: params ->
247+
f typ;
248+
iter_arg_types f params
249+
243250
let iter_type_expr f ty =
244251
match ty.desc with
245252
| Tvar _ -> ()
246253
| Tarrow (params, ret) ->
247-
List.iter (fun {typ} -> f typ) params;
254+
iter_arg_types f params;
248255
f ret
249256
| Ttuple l -> List.iter f l
250257
| Tconstr (_, l, _) -> List.iter f l

0 commit comments

Comments
 (0)