@@ -20,16 +20,13 @@ Choosing what to optimize
2020-------------------------
2121
2222Predicting which code would benefit from optimization can be difficult without
23- using performance analysis tools.
23+ using performance analysis ` tools < #tools-for-optimization >`_ .
2424
2525Oftentimes code that looks slow has no impact on overall performance, and code
2626that looks like it should be fast has a huge impact on performance. Further,
2727reasoning about why a certain chunk of code is slow is often impossible to do
2828without 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-
3330As an example, you may optimize a chunk of code by caching intermediate values.
3431However, if that code was slow due to memory constraints, caching the values and
3532reading 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
9693engine with the exact same build settings you used before. Then profile again
9794and 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,29 @@ 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+ When making low level optimizations, it can be a good idea to investigate the machine code
152+ generated by the compiler. Assembly viewers make this possible, by showing it in a human
153+ readable form called assembly. Viewing assembly allows you to compare the machine code
154+ before and after your changes, to confirm hypotheses used to guide optimization, and to
155+ see what the compiler is doing in general.
156+
157+ You may find the following resources useful:
158+
159+ * Agner Fog's `software optimization resources <https://www.agner.org/optimize/ >`__, especially his `C++ optimization guide <https://agner.org/optimize/optimizing_cpp.pdf >`__.
160+ * `Compiler Explorer <https://godbolt.org >`__, a popular multi-architecture assembly viewer.
161+
107162Pull request requirements
108163-------------------------
109164
110165When making an optimization PR you should:
111166
112167- Explain why you chose to optimize this code (e.g. include the profiling result, link the issue report, etc.).
113168- Show that you improved the code either by profiling again, or running systematic benchmarks.
169+ See `tools <#tools-for-optimization >`__ for more info.
114170- 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.)
121171
122172The most important point to get across in your PR is to highlight the source of
123173the performance issues, and have a clear explanation for how your PR fixes that
0 commit comments