Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline nullable basic types without allocations #11856

Open
Speedphoenix opened this issue Dec 6, 2024 · 4 comments
Open

Inline nullable basic types without allocations #11856

Speedphoenix opened this issue Dec 6, 2024 · 4 comments

Comments

@Speedphoenix
Copy link

At the moment Null<Int> and Null<Float> used exclusively in one scope create unnecessary allocations on static platforms.

The compiler could inline those away like it does for anonymous structures:

var a: Null<Int> = 5;
var b: Null<Int> = null;
if (a != null)
	trace(a);

This is functionally the same as:

var a_null: Bool = false;
var a_val: Int = 5;
var b_null: Bool = true;
var b_val: Int;
if (!a_null)
	trace(a_val);
@Speedphoenix
Copy link
Author

This is similar to #11332, but for nullables rather than parameter enums

@Simn
Copy link
Member

Simn commented Dec 6, 2024

@basro Do you think this is something we could add to the inline constructor code somewhat easily?

@basro
Copy link
Contributor

basro commented Dec 6, 2024

I don't have much knowledge of what goes on with null on static targets so I don't have any idea of what would need to change.

@Simn could you get me a snapshot of the syntax tree before and after the inline constructor for a code sample that showcases the problem? I'd be able to reason about what is going on with that.
I'd prefer to avoid setting up the environment needed to compile haxe if possible.

@Simn
Copy link
Member

Simn commented Dec 9, 2024

Looking at this again, I wonder in which case this could even come up without being const-propagated away. If we statically know the null-ness and value of a local variable, then we likely end up optimizing it anyway. There could be artificial cases like this:

function main() {
	var a:Null<Int> = Math.random() > 0.5 ? 0 : 1;
	if (a != null) {
		trace(a);
	}
}

Here we don't know the value, but we do know the null-ness, which would allow us to optimize the branch away. However, this would require careful dataflow analysis in order to not break down immediately, e.g. on reassignment.

On balance, I don't know if this is worth the effort. Do we have a real-life example of some code which suffers from this and isn't already optimized properly with -D analyzer-optimize?

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

No branches or pull requests

3 participants