Skip to content

Threading Framework & Threaded Collisions#6814

Merged
BMagnu merged 16 commits into
scp-fs2open:masterfrom
BMagnu:threading
Jul 12, 2025
Merged

Threading Framework & Threaded Collisions#6814
BMagnu merged 16 commits into
scp-fs2open:masterfrom
BMagnu:threading

Conversation

@BMagnu

@BMagnu BMagnu commented Jul 8, 2025

Copy link
Copy Markdown
Member

This PR adds a somewhat generic system to manage helper threads for FSO.

For now, it is gated behind the CLI flag -threads <n>, where n has to be 2 or larger to activate threading. Note that due to this being heterogeneous multithreading with a main thread and n-1 helper threads, only 2 threads will likely not see much improvement at all.

To achieve that, FSO will manage an arbitrarily large helper thread pool that can be spun up for specific tasks, such as collision. In the future, AI turret behaviour or model load could be parallelized as well, but that's out of scope for this for now.

Performance-wise, a 4-thread FSO can achieve almost 1.8 times speedup compared to singlethreaded FSO in CPU-heavy but still reasonable missions:
benchmark heavy mission
In missions that are not CPU-bound (i.e. they have only very few collisions, in this case the performance cost is almost entirely GPU-latency based), like Icarus, the PR at least doesn't reduce performance, and can cause small uplifts in heavy sections:
benchmark icarus

For collisions specifically, the flow diagram looks approximately like this:
fso threading

@BMagnu BMagnu added feature A totally new sort of functionality optimization Trying to make the engine and tools more efficient labels Jul 8, 2025
@MoerasGrizzly

Copy link
Copy Markdown

This is great!

I do wonder how this compares to using collision groups for optimisation’s sake: Can we go without them in the future without too much trouble or is collission checking still a heavy-but-now-more-managable concern?

@wookieejedi

Copy link
Copy Markdown
Member

Overall collision groups just help with ship to ship collisions, while this kind of threading helps with all collisions including weapon to ship collisions. Ultimately to get the most performance it would be good to still use collisional groups when possible.

@BMagnu

BMagnu commented Jul 9, 2025

Copy link
Copy Markdown
Member Author

With really collision-heavy missions, you gain at most a factor of two in terms of performance currently, so if you'd add more than twice the collisions with collision groups, you lose all the speedup this gives you.
But that's unlikely, since by far the most collisions checked are weapon / ship, not the typically collision-grouped ship ship collisions

@wookieejedi wookieejedi 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.

Works in all my tests thus far, and is especially invaluable in speeding up heavy missions with lots of lasers/weapons.

@BMagnu BMagnu merged commit bdde909 into scp-fs2open:master Jul 12, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature A totally new sort of functionality optimization Trying to make the engine and tools more efficient

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants