Skip to content

[cpp] Keep the scalar type of a method argument with a constant default - #13249

Open
robknopf wants to merge 1 commit into
HaxeFoundation:developmentfrom
robknopf:cpp-default-arg-scalar
Open

robknopf wants to merge 1 commit into
HaxeFoundation:developmentfrom
robknopf:cpp-default-arg-scalar

Conversation

@robknopf

@robknopf robknopf commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

#11151 changed how the C++ target compiles a function argument that has a default value. A Float, Int or Bool argument with a default used to compile to a number. Since #11151 it compiles to ::Dynamic, so every call wraps the number in a heap object:

static function inverse(x:Float = 2.0):Float {
	return 1 / x;
}

inverse(0.5); // allocates on cpp since #11151

With this change, a method's argument with a constant default is ::hx::Null<T> again, as before #11151, and a direct call passes the number unboxed. The method used as a value still takes the argument as Dynamic, so null through a Dynamic call or Reflect.callMethod still takes the default. Dynamic methods and defaults that aren't constants are unchanged.

If #11151 boxed these arguments on purpose, I'd be glad to hear why.

The test is tests/unit/src/unit/issues/Issue13249.hx. static_asserts check that the arguments aren't taken as Dynamic: they fail to compile before the change and pass after. The rest checks the default taken for null, through overrides, closures that capture the argument, and an interface.

Checked on Linux x64 with hxcpp master, together with #13248: the C++ unit tests, the cppia unit tests (with and without -jit), and the sys, threads, event loop and misc C++ tests. Not checked: Windows, macOS, other compilers.

Written by an AI agent (Claude) working with me, and reviewed by a second agent.

robknopf added a commit to robknopf/haxe that referenced this pull request Nov 29, 2026
robknopf added a commit to robknopf/haxe that referenced this pull request Nov 29, 2026
@tobil4sk

Copy link
Copy Markdown
Member

I would also be curious if this could fix some of the performance loss in haxe 5 seen in the benchmarks.

The -0.0 boxing bug is incorrect behaviour (HaxeFoundation/hxcpp#1476), so I don't think it makes sense for the test to rely on it as a way to check whether boxing occurred.

The method's function value (its _dyn and callable type) still takes that argument as Dynamic, so a null passed through a Dynamic call or Reflect.callMethod still takes the default.

Is there any way we could avoid the Dynamic for the callable in the non-dynamic/non-reflective case?

It may be that #11151 boxed these arguments on purpose, for a reason I've missed. If so, I'd be glad to hear it.

@Aidan63 might be able to mention if there were any reasons.

Since the typed functions (HaxeFoundation#11151), every argument with a default was typed
through cpp_type_of_null, so a Float or Int argument with a default became
::Dynamic, and each call boxed it, allocating.

A method's argument with a constant default keeps its scalar type again,
printed as ::hx::Null<T> and read with .Default, as before HaxeFoundation#11151. The
method's function value (its _dyn and its callable type) still takes the
argument as Dynamic, so a null passed through it, by a Dynamic call or
Reflect.callMethod, still takes the default. A closure captures the body's
copy of the argument, so an override's signature is the same whatever its
body captures. Dynamic methods and defaults that aren't constants are
unchanged.
@robknopf

Copy link
Copy Markdown
Contributor Author

Thanks. hxcpp#1476 makes the -0 checks pass with or without this change, so they're gone. In their place, static_asserts check that the method doesn't take its Float, Int or Bool as Dynamic. They fail to compile on development and pass with this change.

Reviewing it again turned up a break in the first version: an override whose closure captured its argument kept Dynamic while the method it overrode didn't, so it no longer compiled. That's fixed (a closure captures the body's copy, so the capture doesn't matter), and the test now covers it both ways.

On the callable: a direct call never goes through it, so the non-dynamic, non-reflective case already passes the number unboxed. Dynamic stays only when the method is used as a value. Typing that value's argument as ::hx::Null<T> looks possible, as hxcpp's callables convert between signatures. But it reaches every place a function type is printed (closures, interfaces, dynamic functions), and anything still expecting Callable<R(Dynamic)> would quietly fall back to the slow path. I'd rather do it as a follow-up than grow this one.

You mentioned some regressions? Which benchmarks are you talking about?

@tobil4sk

Copy link
Copy Markdown
Member

Thanks for adjusting it! It would be nice if we could avoid the large amounts of raw c++ code in the tests , but if that's not feasible and it's fine with others then we can go with it.

I'll leave it up to @Aidan63 to comment on the direct call vs callable value situation as I don't have as good a grasp of the new callable system.

You mentioned some regressions? Which benchmarks are you talking about?

These are found on benchs.haxe.org, with the sources at https://github.com/HaxeBenchmarks/benchmark-runner. There was a slight performance regression in haxe 5 for some of them when the typed callables were implemented so I wonder if this boxing could be partially to blame.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants