From 2e243f529ffcc4469a5f6ecbfcbb16a3680912ac Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Wed, 25 Nov 2026 20:23:52 +0000 Subject: [PATCH] Instantiate non-generic types without a copy session Ctype.instance and instance_def opened a copy session even for a type that is neither generic nor marked by an enclosing session, which copy returns as it is. Typecore instantiates the expected type of every expression this way, so each of these empty sessions allocated for nothing. They now return the representative directly; marked types and partial instances still take the session. warning_scope skips empty attribute lists, which also saves the closure of its partial application. Allocation matters because every minor collection stops all compiler workers. On 77 Belt and tests/tests files, compiling allocates 3.7% fewer words, with byte-identical JS and diagnostics; a clean build of tests/tests (690 modules) writes the same 3484 files, .cmi, .cmj, .cmt and .ast included. Build time is within noise. Unit tests in tests/ounit_tests/ounit_instance_tests.ml check that a non-generic type and its structure come back as they are, that links are followed, that generic variables and links to generic or marked nodes are still copied, that a node marked by an enclosing session gives that session's copy, and that partial instances still replace a non-generic variable by a fresh one. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH Signed-off-by: Christoph Knittel --- .../instance-without-copy-session.internal.md | 1 + compiler/typing/builtin_attributes.ml | 6 +- compiler/typing/ctype.ml | 33 +++++-- tests/ounit_tests/ounit_instance_tests.ml | 89 +++++++++++++++++++ tests/ounit_tests/ounit_tests_main.ml | 1 + 5 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 changes/instance-without-copy-session.internal.md create mode 100644 tests/ounit_tests/ounit_instance_tests.ml diff --git a/changes/instance-without-copy-session.internal.md b/changes/instance-without-copy-session.internal.md new file mode 100644 index 0000000000..4460ab334c --- /dev/null +++ b/changes/instance-without-copy-session.internal.md @@ -0,0 +1 @@ +- Instantiate types that aren't generic without opening a copy session, so type checking allocates a little less. diff --git a/compiler/typing/builtin_attributes.ml b/compiler/typing/builtin_attributes.ml index 791629da29..71239565a7 100644 --- a/compiler/typing/builtin_attributes.ml +++ b/compiler/typing/builtin_attributes.ml @@ -165,7 +165,11 @@ let warning_attribute ?(ppwarning = true) = let warning_scope ?ppwarning attrs f = let prev = Warnings.backup () in try - List.iter (warning_attribute ?ppwarning) (List.rev attrs); + (* Most nodes have no attributes; skipping them also saves the closure the + partial application would allocate on every call *) + (match attrs with + | [] -> () + | _ -> List.iter (warning_attribute ?ppwarning) (List.rev attrs)); let ret = f () in Warnings.restore prev; ret diff --git a/compiler/typing/ctype.ml b/compiler/typing/ctype.ml index dce0214308..7b0834be7e 100644 --- a/compiler/typing/ctype.ml +++ b/compiler/typing/ctype.ml @@ -874,17 +874,32 @@ let simple_copy t = copy t let gadt_env env = if Env.has_local_constraints env then Some env else None +(* Whether [copy] returns the representative [ty] as it is: it is neither + generic nor marked by an enclosing copy session. Instantiating such a node + needs no copy session of its own. *) +let is_own_instance ty = + ty.level <> generic_level + && + match ty.desc with + | Tsubst _ -> false + | _ -> true + let instance ?partial env sch = - with_copy_session (fun () -> - let env = gadt_env env in - let partial = - match partial with - | None -> None - | Some keep -> Some (compute_univars sch, keep) - in - copy ?env ?partial sch) + let root = repr sch in + if Option.is_none partial && is_own_instance root then root + else + with_copy_session (fun () -> + let env = gadt_env env in + let partial = + match partial with + | None -> None + | Some keep -> Some (compute_univars sch, keep) + in + copy ?env ?partial sch) -let instance_def sch = with_copy_session (fun () -> copy sch) +let instance_def sch = + let root = repr sch in + if is_own_instance root then root else with_copy_session (fun () -> copy sch) let instance_list env schl = with_copy_session (fun () -> diff --git a/tests/ounit_tests/ounit_instance_tests.ml b/tests/ounit_tests/ounit_instance_tests.ml new file mode 100644 index 0000000000..e728899bad --- /dev/null +++ b/tests/ounit_tests/ounit_instance_tests.ml @@ -0,0 +1,89 @@ +(* [Ctype.instance] and [instance_def] return a type that is neither generic + nor marked by an enclosing copy session as it is, without a copy session of + their own. These tests pin the cases where that is safe and the ones that + must still copy. *) + +let ( >:: ), ( >::: ) = OUnit.(( >:: ), ( >::: )) +let assert_bool = OUnit.assert_bool + +let instance ?partial ty = Ctype.instance ?partial Env.empty ty + +let test_non_generic_is_shared _ = + let ty = Ctype.newvar () in + assert_bool "instance returns the node itself" (instance ty == ty); + assert_bool "instance_def returns the node itself" + (Ctype.instance_def ty == ty); + let pair = Ctype.newty (Types.Ttuple [ty; Predef.type_int ()]) in + assert_bool "a non-generic structure is shared too" (instance pair == pair) + +let test_link_is_followed _ = + let source = Ctype.newvar () and target = Ctype.newvar () in + Btype.link_type source target; + assert_bool "instance resolves the link" (instance source == target); + assert_bool "instance_def resolves the link" + (Ctype.instance_def source == target) + +let test_generic_is_copied _ = + let scheme = Btype.newgenvar () in + List.iter + (fun (name, copy) -> + assert_bool + (name ^ " gives a generic variable a fresh instance") + (copy != scheme); + assert_bool + (name ^ " gives an instance that is not generic") + ((Ctype.repr copy).level <> Btype.generic_level)) + [("instance", instance scheme); ("instance_def", Ctype.instance_def scheme)] + +(* The shortcut looks at the representative: a link to a generic node or to a + marked one is no shortcut *) +let test_link_target_decides _ = + let generic = Btype.newgenvar () and source = Ctype.newvar () in + Btype.link_type source generic; + let copy = instance source in + assert_bool "a link to a generic variable gives a fresh instance" + (copy != generic && (Ctype.repr copy).level <> Btype.generic_level); + Btype.with_copy_session (fun () -> + let marked = Ctype.newvar () and target = Ctype.newvar () in + let source = Ctype.newvar () in + Btype.link_type source marked; + Btype.save_desc marked marked.desc; + marked.desc <- Types.Tsubst target; + assert_bool "a link to a marked node gives the session's copy" + (instance source == target); + assert_bool "instance_def too" (Ctype.instance_def source == target)) + +(* Inside an enclosing copy session, a node marked with [Tsubst] stands for its + copy; instantiating it must still give that copy. *) +let test_enclosing_session_mark_is_reused _ = + Btype.with_copy_session (fun () -> + let marked = Ctype.newvar () and target = Ctype.newvar () in + Btype.save_desc marked marked.desc; + marked.desc <- Types.Tsubst target; + assert_bool "instance gives the enclosing session's copy" + (instance marked == target); + assert_bool "instance_def gives the enclosing session's copy" + (Ctype.instance_def marked == target)) + +(* A partial instance replaces a non-generic variable without free univars by + a fresh one, so it must not take the shortcut. *) +let test_partial_still_copies _ = + let ty = Ctype.newvar () in + let kept = instance ~partial:true ty in + assert_bool "partial:true gives a fresh variable" (kept != ty); + OUnit.assert_equal ~printer:string_of_int ~msg:"partial:true keeps the level" + (Ctype.repr ty).level (Ctype.repr kept).level; + let forgotten = instance ~partial:false ty in + assert_bool "partial:false gives a fresh variable" (forgotten != ty) + +let suites = + __FILE__ + >::: [ + "non_generic_is_shared" >:: test_non_generic_is_shared; + "link_is_followed" >:: test_link_is_followed; + "generic_is_copied" >:: test_generic_is_copied; + "link_target_decides" >:: test_link_target_decides; + "enclosing_session_mark_is_reused" + >:: test_enclosing_session_mark_is_reused; + "partial_still_copies" >:: test_partial_still_copies; + ] diff --git a/tests/ounit_tests/ounit_tests_main.ml b/tests/ounit_tests/ounit_tests_main.ml index ee9c8c5b4e..3c601f70c7 100644 --- a/tests/ounit_tests/ounit_tests_main.ml +++ b/tests/ounit_tests/ounit_tests_main.ml @@ -26,6 +26,7 @@ let suites = Ounit_ast_helper_tests.suites; Ounit_constructor_arguments_tests.suites; Ounit_object_mutability_tests.suites; + Ounit_instance_tests.suites; Ounit_pattern_printer_tests.suites; Ounit_js_analyzer_tests.suites; Ounit_external_shadow_tests.suites;