Skip to content

Commit bb363d4

Browse files
authored
Optimise the allocation of optional arguments (oxcaml#11)
1 parent da6ff04 commit bb363d4

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

testsuite/tests/typing-local/alloc.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,14 @@ let makeverylong n =
395395
ignore_local (local_array 100_000 n);
396396
()
397397

398+
let fun_with_optional_arg ?(local_ foo = 5) () =
399+
let _ = foo + 5 in
400+
()
401+
402+
let optionalarg ((f : ?foo:local_ int -> unit -> unit), n) =
403+
let () = f ~foo:n () in
404+
()
405+
398406
let run name f x =
399407
let prebefore = Gc.allocated_bytes () in
400408
let before = Gc.allocated_bytes () in
@@ -446,7 +454,8 @@ let () =
446454
run "stringbint" readstringbint ();
447455
run "bigstringbint" readbigstringbint ();
448456
run "verylong" makeverylong 42;
449-
run "manylong" makemanylong 100
457+
run "manylong" makemanylong 100;
458+
run "optionalarg" optionalarg (fun_with_optional_arg, 10)
450459

451460

452461
(* In debug mode, Gc.minor () checks for minor heap->local pointers *)

testsuite/tests/typing-local/alloc.reference

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
bigstringbint: OK
3232
verylong: OK
3333
manylong: OK
34+
optionalarg: OK

typing/typecore.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,12 @@ let register_allocation_mode alloc_mode =
362362
| Amode _const -> ()
363363
| Amodevar _ -> allocations := alloc_mode :: !allocations
364364

365-
let register_allocation (expected_mode : expected_mode) =
365+
let register_allocation_value_mode mode =
366366
register_allocation_mode
367-
(Value_mode.regional_to_global_alloc expected_mode.mode)
367+
(Value_mode.regional_to_global_alloc mode)
368+
369+
let register_allocation (expected_mode : expected_mode) =
370+
register_allocation_value_mode expected_mode.mode
368371

369372
let optimise_allocations () =
370373
if Clflags.Extension.is_enabled Local then begin
@@ -433,6 +436,7 @@ let option_none env ty mode loc =
433436
mkexp (Texp_construct(mknoloc lid, cnone, [])) ty mode loc env
434437

435438
let option_some env texp mode =
439+
register_allocation_value_mode mode;
436440
let lid = Longident.Lident "Some" in
437441
let csome = Env.find_ident_constructor Predef.ident_some env in
438442
mkexp ( Texp_construct(mknoloc lid , csome, [texp]) )

0 commit comments

Comments
 (0)