Lesson 8 of 9 · 24m
Profiling Before Optimizing
Measure frame time, physics cost, draw calls, memory, and allocation pressure before rewriting code based on intuition.
The most expensive optimization is the one that did not need to happen. Teams rewrite clean GDScript into object pools because frame rate dips, then discover the real cost was overdraw, shader compilation, or physics bodies that never sleep. Godot gives you enough instrumentation to avoid that embarrassment. Measure before making the codebase worse in the name of speed.
Establish the frame budget and the test scene
At 60 frames per second, one frame is about 16.7 milliseconds. At 30 it is 33.3 milliseconds. That budget includes rendering, physics, scripting, audio, and engine overhead:
60 FPS -> 16.7 ms per frame
30 FPS -> 33.3 ms per frame
144 FPS -> 6.9 ms per frameProfile a representative worst-case scene: realistic entity counts, particles, camera zoom, and player actions. A menu with no enemies tells you nothing about combat.
Script cost is usually architectural
The language is rarely the first problem. The shape of the work is:
func _process(delta: float) -> void:
for enemy in get_tree().get_nodes_in_group("enemy"):
enemy.update_target(player.global_position)A tree-wide group query every frame is architectural waste when membership changes rarely. Cache explicit membership instead:
var enemies: Array[Enemy] = []
func register_enemy(enemy: Enemy) -> void:
enemies.append(enemy)That is not a micro-optimization. It replaces repeated scene-tree scanning with ownership.
Separate rendering and physics causes
Large sprite counts, transparency overlap, particles, lights, shadows, and shader complexity can dominate frame time. Reducing script cost will not fix overdraw. Physics has different costs: active bodies, shape counts, collision layers, broadphase behavior, and per-tick raycasts. Stagger work that does not need perfect tick frequency:
scan_timer += delta
if scan_timer >= 0.25:
scan_timer = 0.0
scan_for_targets()Test on the weakest target hardware you intend to support; a desktop GPU can conceal a mobile disaster.
Optimize allocation only with evidence
Frequent temporary allocations can create spikes, especially in tight loops or large arrays. Use memory and object monitors before preallocating everything. Object pooling is warranted for high-frequency entities such as bullets or damage numbers, not for a boss that spawns twice per level. Record commit, test scene, engine version, and hardware before comparing results.
Field notes
- Debug builds and editor runs do not match release-export performance.
- Profilers add cost; use measurements comparatively rather than treating them as physics-perfect.
- Averages hide spikes, and frame pacing problems often appear as maximum-frame outliers.
- Optimization without a target device is speculation with extra steps.
- A stable 60 FPS can be correct even on a 144 Hz monitor; decide the target before chasing numbers.
Was this lesson helpful?