Skip to content

Commit 0cf660a

Browse files
committed
Skip scraping a primitive's type when no specialization reads it
Why: `specialize_primitive` scraped the primitive's whole function type (a deep copy plus head expansion) on every use, but only unified operators and comparisons look at it. For the other 95% of uses (on a 552 module React app, 21,582 of 22,770) the result was thrown away and `find_builtin` answered. For a function type whose primitive is in neither table, it now calls `find_builtin` directly. Review: Could the skipped scrape have had an effect? Scraping a function type looks nothing up: only a Tconstr head is expanded. What is left is a copy with fresh type ids, made during Lambda translation after the .cmi and .cmt are written, whose result nothing read. The corpus artifacts and .cmt files are byte identical to master. Signed-off-by: Jono Prest <jjprest@gmail.com>
1 parent 21f7f7f commit 0cf660a

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

‎compiler/ml/translcore.ml‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ let specialize_comparison
412412
(* Specialize a primitive from available type information,
413413
raise Not_found if primitive is unknown *)
414414

415-
let specialize_primitive p env ty (* ~has_constant_constructor *) =
415+
let specialize_typed_primitive p env ty (* ~has_constant_constructor *) =
416416
let fn_expr = is_function_type env ty in
417417
let unified =
418418
match fn_expr with
@@ -429,6 +429,20 @@ let specialize_primitive p env ty (* ~has_constant_constructor *) =
429429
| None -> Primitive table.objcomp
430430
with Not_found -> find_builtin p.prim_name)
431431

432+
(* Only unified operators and comparisons read the primitive's type, so the
433+
others skip scraping it (a copy of the whole function type) when it is a
434+
function type: scraping one then looks nothing up (only a [Tconstr] head
435+
is expanded, and only a [Tconstr] result is looked up), so the copy is
436+
all the skipped work. *)
437+
let specialize_primitive p env ty =
438+
match (Btype.repr ty).desc with
439+
| Tarrow _
440+
when not
441+
(Hashtbl.mem Unified_ops.index_by_name p.prim_name
442+
|| Hashtbl.mem comparisons_table p.prim_name) ->
443+
find_builtin p.prim_name
444+
| _ -> specialize_typed_primitive p env ty
445+
432446
(* [is_unit] excluded: unit shares the undefined constant but is not a
433447
[%null] / [%undefined] literal, and comparing it never suppressed the
434448
warning. *)

0 commit comments

Comments
 (0)