Skip to content

Commit 5b622d5

Browse files
committed
Add a 'tools' sections to the optimization article.
1 parent 39b1cd1 commit 5b622d5

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

engine/guidelines/optimization.rst

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@ Choosing what to optimize
2020
-------------------------
2121

2222
Predicting which code would benefit from optimization can be difficult without
23-
using performance analysis tools.
23+
using performance analysis `tools <#tools-for-optimization>`_.
2424

2525
Oftentimes code that looks slow has no impact on overall performance, and code
2626
that looks like it should be fast has a huge impact on performance. Further,
2727
reasoning about why a certain chunk of code is slow is often impossible to do
2828
without detailed metrics (e.g. from a profiler).
2929

30-
Instructions on using some common profilers with Godot can be found `here
31-
<https://docs.godotengine.org/en/stable/engine_details/development/debugging/using_cpp_profilers.html>`_.
32-
3330
As an example, you may optimize a chunk of code by caching intermediate values.
3431
However, if that code was slow due to memory constraints, caching the values and
3532
reading them later may be even slower than calculating them from scratch!
@@ -96,6 +93,50 @@ Once you have your baseline profile/benchmark, make your changes and rebuild the
9693
engine with the exact same build settings you used before. Then profile again
9794
and compare the results.
9895

96+
Tools for optimization
97+
~~~~~~~~~~~~~~~~~~~~~~
98+
99+
Profilers
100+
^^^^^^^^^
101+
102+
Profilers are the most important tool for everyone optimizing code. They show you which
103+
parts of the code are responsible for slow execution or heavy CPU load. Profilers are
104+
therefore excellent for identifying what needs to be optimized, and to test whether
105+
performance was improved after making changes. Godot has a built-in profiler, but it
106+
does not provide very detailed information. Instead, use dedicated C++ profilers, which are
107+
`explained in the Godot documentation <https://docs.godotengine.org/en/stable/engine_details/development/debugging/using_cpp_profilers.html>`__.
108+
109+
Benchmarks
110+
^^^^^^^^^^
111+
112+
Benchmarks can be a great and simple tool to test the impact of your changes
113+
of an isolated piece of code. However, they can be misleading because it's easy
114+
to write them in a way that doesn't reflect real-world performance. When using
115+
benchmarks to test the performance of your code, always be aware of their
116+
potential caveats, and familiarize yourself with good benchmark practices.
117+
118+
To start writing benchmarks in Godot, you can use the following code templates:
119+
120+
.. tabs::
121+
.. code-tab:: gdscript GDScript
122+
123+
var start = Time.get_ticks_msec()
124+
var s := "Lorem ipsum dolor sit amet";
125+
for i in range(10000):
126+
s.replace("e", "b") # Benchmarks the 'replace' function.
127+
print(Time.get_ticks_msec() - start, "ms")
128+
129+
.. code-tab:: cpp
130+
131+
String s = "Lorem ipsum dolor sit amet";
132+
133+
auto t0 = std::chrono::high_resolution_clock::now();
134+
for (int i = 0; i < 100000; i ++) {
135+
String s1 = s.replace("e", "b"); // Benchmarks the 'replace' function.
136+
}
137+
auto t1 = std::chrono::high_resolution_clock::now();
138+
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count() << "ms\n";
139+
99140
.. note::
100141

101142
Results will fluctuate, so you'll need to make your test project or
@@ -104,20 +145,25 @@ and compare the results.
104145
test multiple times, and observe how much the results fluctuate. Fluctuations of up
105146
to 10% are common and expected. The fastest run is usually the most accurate number.
106147

148+
Assembly viewers
149+
^^^^^^^^^^^^^^^^
150+
151+
Assembly viewers show the final compiled version of your code in a readable
152+
format called assembly. Examining the assembly can be an effective way to
153+
optimize low-level code. You may find the following resources useful:
154+
155+
* Agner Fog's `software optimization resources <https://www.agner.org/optimize/>`__, especially his `C++ optimization guide <https://agner.org/optimize/optimizing_cpp.pdf>`__.
156+
* `Compiler Explorer <https://godbolt.org>`__, a popular multi-architecture assembly viewer.
157+
107158
Pull request requirements
108159
-------------------------
109160

110161
When making an optimization PR you should:
111162

112163
- Explain why you chose to optimize this code (e.g. include the profiling result, link the issue report, etc.).
113164
- Show that you improved the code either by profiling again, or running systematic benchmarks.
165+
See `tools <#tools-for-optimization>`__ for more info.
114166
- Test on multiple platforms where appropriate, especially mobile.
115-
- When micro-optimizing, show assembly before / after where appropriate.
116-
117-
In particular, you should be aware that for micro-optimizations, C++ compilers will often
118-
be aware of basic tricks and will already perform them in optimized builds. This is why
119-
showing before / after assembly can be important in these cases.
120-
(`godbolt <https://godbolt.org/>`_ can be particularly useful for this purpose.)
121167

122168
The most important point to get across in your PR is to highlight the source of
123169
the performance issues, and have a clear explanation for how your PR fixes that

0 commit comments

Comments
 (0)