Skip to content

Add a subsection for microbenchmarks to the profiling page. - #12200

Open
Ivorforce wants to merge 1 commit into
godotengine:masterfrom
Ivorforce:microbenchmarks
Open

Add a subsection for microbenchmarks to the profiling page.#12200
Ivorforce wants to merge 1 commit into
godotengine:masterfrom
Ivorforce:microbenchmarks

Conversation

@Ivorforce

Copy link
Copy Markdown
Member

We currently have no documented way of performing microbenchmarks.

There is no perfectly suitable page to add this to, but I think the profiling page is a well suited candidate even though it's not technically profiling, because people are somewhat likely to find it here.
But I'm open to moving the information elsewhere!

@Ivorforce
Ivorforce requested a review from a team as a code owner July 29, 2026 23:59
@Ivorforce Ivorforce added the area:engine details Issues and PRs related to the Engine Details section of the documentation label Jul 29, 2026
@Ivorforce
Ivorforce requested a review from a team July 29, 2026 23:59
@Ivorforce
Ivorforce force-pushed the microbenchmarks branch 3 times, most recently from 71a3894 to 856f27b Compare July 30, 2026 00:08
@lawnjelly

lawnjelly commented Jul 30, 2026

Copy link
Copy Markdown
Member

More things to mention (specially for c++):

  • do the first test runs without timing
  • first run to commit pages that have been allocated
  • first run to warm the cache
  • run the main loop multiple times, the smallest measurement is often most useful
  • don't run anything else on your PC while benchmarking (especially e.g. browsers)

These are all very sensitive to other things using your OS.

As an alternative for microbenching you can use RDTSC / RDTSCP (forces processor to finish previous instructions) to measure CPU clock ticks. This used to be easier, but on modern multicore PCs which scale speeds there's more considerations, but most have invariant TSC on the CPU regardless of stepping (check this though for your CPU).

(in fact I must revisit using RDTSC for a recent couple of benchmarks in PRs)

More possibles:

  • if you run the same data each loop, it won't stress the cache, but it may do e.g. perfect branch prediction
  • good practice often to create a random data set before benchmarking
  • the size of the data set will determine how much cache thrashing will go on
  • might be worth mentioning websites dedicated to benchmarking (sometimes you can do the benchmark online, with pre-built helpers, especially for code which is localised and doesn't need to pull in the rest of Godot)
  • one I discovered today - don't use rand() within a benchmark, it's not O(1), and might do locking for threads

Obviously we shouldn't need to be a full reference, but we should probably include some of the major pitfalls, otherwise we'll get new benchmarkers and have to explain the same artifacts each time. And emphasize how difficult it can be to get correct results, I regularly get artifacts even after having done benchmarks here and there for many years 😁 .

Comment thread engine_details/development/profiling/index.rst Outdated
Comment thread engine_details/development/profiling/index.rst Outdated
Comment thread engine_details/development/profiling/index.rst Outdated
Comment thread engine_details/development/profiling/index.rst Outdated
@Ivorforce

Ivorforce commented Jul 30, 2026

Copy link
Copy Markdown
Member Author

More things to mention (specially for c++):

  • do the first test runs without timing
  • first run to commit pages that have been allocated
  • first run to warm the cache
  • run the main loop multiple times, the smallest measurement is often most useful
  • don't run anything else on your PC while benchmarking (especially e.g. browsers)

Those are all good tips, but I deliberately didn't include any in my PR.

If we wanted to be a "best benchmarking practices" reference, we'd have to include a lot more info, for example about how to avoid dead code elimination or why cold checks are often more important than hot checks. I don't think this is in scope for the docs, so I opted for saying "hey be careful about it and read a guide somewhere" instead.

Obviously we shouldn't need to be a full reference, but we should probably include some of the major pitfalls, otherwise we'll get new benchmarkers and have to explain the same artifacts each time. And emphasize how difficult it can be to get correct results, I regularly get artifacts even after having done benchmarks here and there for many years 😁 .

I'd rather find a blessed guide to link to somewhere and not give any tips here at all. I don't have one at hand, unfortunately, but it sounds like you might be interested in writing one 😄

@lawnjelly

Copy link
Copy Markdown
Member

but it sounds like you might be interested in writing one

Absolutely not 🤣
Yeah, agreed maybe we can just link to some good benchmarking websites.

https://quick-bench.com
https://github.com/google/benchmark

Unfortunately most tutorials seem to be directing you to use pre-existing libraries / tools for benchmarking instead of doing it from scratch, which is a bit sad, but maybe we'll find one.

Maybe it's because it's so easy to get wrong (even for the pros!), beginners get steered away from it.

@Ivorforce

Ivorforce commented Jul 30, 2026

Copy link
Copy Markdown
Member Author

I had an AI research credible stable resources we can link, found 3:

I've only heard of the last one, but the first two also look like incredible fits, so I think they are all worth linking. I'll adjust in a push.
Regarding your links, I'd rather avoid linking anything that's using libraries. Most benchmarks will probably be hand written and a library will be too much mental overhead for a single PR.

@lawnjelly

Copy link
Copy Markdown
Member

Regarding your links, I'd rather avoid linking anything that's using libraries. Most benchmarks will probably be hand written and a library will be too much mental overhead for a single PR.

I mostly feel the same way, although I'm trying to begrudgingly recognise that for most people spending hours diagnosing artifacts in their benchmarks isn't their idea of fun (like it might be for you or I 😁 ), and I know some quite advanced programmers will still just use off the shelf libs rather than write their own, just because of the pitfalls.

I did check brendangregg's website earlier but didn't spot that particular article (most were quite in depth).

The Bakhvalov book I may have looked through before at some point, seems like a good one to link covering optimization and benchmarking. 👍

@Ivorforce

Ivorforce commented Jul 30, 2026

Copy link
Copy Markdown
Member Author

If you think some people would prefer a library for it we could mention that and perhaps link one or two examples.

@lawnjelly

Copy link
Copy Markdown
Member

Looks good.

Something may be worth adding that gdscript benchmarking can be difficult to use / not hugely enlightening when the cost of the function under test (e.g. c++) is very cheap in relation to the overheads of gdscript.

In many situations you get so much variation from the gdscript overheads, these might be 1000x as e.g. a c++ function that you are trying to optimize. But still I've seen people making PRs and trying to use a gdscript benchmark where it is clearly inappropriate.

Not quite sure how to word it.

@lawnjelly lawnjelly left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although there's a few things we could add, this is fine to get started imo.

@AThousandShips AThousandShips left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread engine_details/development/profiling/index.rst Outdated
Comment thread engine_details/development/profiling/index.rst Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:engine details Issues and PRs related to the Engine Details section of the documentation enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants