Does Godot auto inline functions?

Godot Version

3.5, but any version really.

Question

Does Godot auto inline functions when you export the final product, or is every function called anew? For instance:

func add_two(num: int) -> int:
    var add_one = increment(num)
    return increment(add_one)

Assuming all increment(num) does is return num+1, would my add_two() function actually call increment() twice, or does it treat it as if “num+1” had been written there instead?

I posted an answer but changed my mind. I missed the ‘on export’. Sorry.

1 Like

There’s a thread on high function overhead, I doubt export is going to add a drastic time-saver like inlining gdscript. If you are worried about such micro-optimizations I would recommend converting this class to a GDExtension where one has micro-optimization control.

1 Like

I see, thanks. Well, if this is considered a “micro” optimization then I won’t worry about it until it presents itself as a problem.

If performance is important for your project, then GDExtension is one option. Another, probably easier option is to use C#, which automatically inlines function calls where it can be reasonably expected to improve performance. If you’re certain calls to a function should be inlined, you can also use the [MethodImpl(MethodImplOptions.AggressiveInlining)] attribute to push the JIT harder to inline calls that it otherwise might not.

If you are conscious enough of performance to be asking the question, I recommend using C# instead of GDScript (though this may not work as well with Godot 3.x), and I recommend being careful with how the performance-sensitive code may interface with Godot’s engine API. C# is much more performant than GDScript as a default, while also giving much greater control over things like memory management and function call inlining.

Performance comparison showing how C# is orders of magnitude faster with simple algorithms, not touching the engine API:
https://old.reddit.com/r/godot/comments/1g50mlq/c_vs_gdscript_performance_with_large_for_loops/

Information on the performance implications of how Godot implements its C# engine API bindings:

Performance comparison showing that using Node features like _Process in C# can degrade performance significantly, and so should be used conservatively where performance is a concern:

2 Likes

I’ve always been interested in this, but your link is comparing GDExtensions (C++) to GDScript. In the coments they were able to optimize the GDScript from 7208ms down to 147ms, and the C++ from 66ms down to 3ms.

2 Likes

Oop, my mistake.

There are some comments on the post, though, also discussing C# performance:

https://old.reddit.com/r/godot/comments/1g50mlq/c_vs_gdscript_performance_with_large_for_loops/ls7mdv2/

https://old.reddit.com/r/godot/comments/1g50mlq/c_vs_gdscript_performance_with_large_for_loops/ls83yir/

@gertkeno
That was a great link BTW, it took ages but I read most if not all of those comments. It was a great read - thanks.