godot icon indicating copy to clipboard operation
godot copied to clipboard

Improve performance of `nearest_po2()`

Open MewPurPur opened this issue 1 year ago • 5 comments

Note: This PR used to change behavior too, but now it's just a performance PR

Expanded nearest_po2() so that it doesn't have to do manual work figuring out GDScript's int is 64-bit. This optimization is already done in C# and makes the function a few times faster (at least 30% faster, probably a lot more in reality)

MewPurPur avatar Jan 26 '23 01:01 MewPurPur

It'd be great if you could also update the Mathf.NearestPo2 method in C# :pray:. Keep in mind we use 32-bit int in C# and I don't think we want to change that to 64-bit.

Also, we should add some unit tests for this method.

raulsntos avatar Jan 26 '23 14:01 raulsntos

@raulsntos C# already uses this PR's optimization, but keeps the edge cases. I could add if (value <= 1) { return 1; } at the beginning though, perhaps at a slight performance cost but not too relevant. Should I though? If we're fine with those edge cases, I can only keep this GDScript optimization and otherwise keep the function's return values as they are.

I would like some sort of consensus on this before proceeding with adding test cases.

MewPurPur avatar Jan 26 '23 15:01 MewPurPur

I could add if (value <= 1) { return 1; } at the beginning

Yeah, that's what I meant, if we're changing the behavior for GDScript we should also change it for C#.

I would like some sort of consensus on this before proceeding with adding test cases.

Makes total sense, I just wanted to note that this method doesn't currently have unit tests.

raulsntos avatar Jan 26 '23 15:01 raulsntos

Also I don't really know where to add such tests. We have tests on the engine funcs, but I can't find anything for the GlobalScope. Few of the math functions in variant_utility aren't carbon copies of ones used in the engine, which are tested in math funcs. Which is why I'm not so sure about the PR--

MewPurPur avatar Jan 26 '23 19:01 MewPurPur

Stripped this PR a bit and its compat-breaking aspects. Now it's just a performance boost for GDScript and documentation tweak.

Should be safe, but it's not that important. Can wait for after 4.0

p.s. Should change label from bug to performance, given this.

MewPurPur avatar Feb 09 '23 13:02 MewPurPur

Weirdly enough, I don't measure the performance boost I previously did. I have no idea why, previously I measured a 30% boost when the function had extra steps. Maybe I benchmarked poorly. Or maybe something else happened that optimized it.

Anyway, I'm yet again reshaping this. Now it's just a documentation PR lol

MewPurPur avatar Apr 12 '23 20:04 MewPurPur

pokey pokey!

MewPurPur avatar Aug 05 '23 18:08 MewPurPur

Thanks!

akien-mga avatar Aug 07 '23 13:08 akien-mga