What techniques do you use to uncover performance bottlenecks?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By steely
:warning: Old Version Published before Godot 3 was released.

I’d like to gather some community tips and tricks to help me (and others) tune the performance of GDScript code. I’ve been relying on intuition, general coding experience, and trial and error, but they can only get me so far.

I consider Mike Dunlavey’s Stack Overflow answer to be one of the classics, describing an extremely simple and effective way to detect performance bottlenecks that I’ve used in other languages. Is there a way to do this in a more precise manner with Godot? My project has an ugly once-per-second stutter caused by a bunch of calculations that must happen within a single frame, and I’d like to isolate that frame and break into it randomly with the debugger so I can identify what needs to get optimized.

A tool to generate a flame graph (example 1, example 2) would be extremely helpful for this. Does something like this exist for Godot?

If you’ve found another method to be effective, what was it?

If there are non-performant things specific to GDScript that you’ve learned to avoid, what are they?

If you know which script is causing FPS spikes, you can do the good old dichotomy by enclosing a scope of code by this:

var time_before = OS.get_ticks_msec()
# code...
var time_elapsed = OS.get_ticks_msec() - time_before

However it’s an archaic method when you get to do that on a large codebase where you have no clue about what causes the game to stutter.
There are new profiling tools in Godot’s 2.1 debugger, but I didn’t tested them yet. They might help.

Zylann | 2016-07-10 18:37

@Zylann I’m currently using 2.0.2 stable. Are the changes between 2.0.2 and 2.1 significant?

steely | 2016-07-10 22:25

Yes, there are lots of new features in the upcoming 2.1.
Most notably, there is this:

Zylann | 2016-07-10 22:39

Nice graph, but will it help me identify how much time is spent in each function? I already know the general area of low performance, I just want to isolate the hot spots.

steely | 2016-07-11 01:17

You can see script functions on the screenshot, even custom ones such as lerp_angle and normalize_angle. So I guess you’ll be able to see which ones take time in a larger project.

Zylann | 2016-07-11 21:27