Skip to content

perf: Reparse the game database in parallel#346

Draft
Phantomical wants to merge 2 commits intoKSPModdingLibs:devfrom
Phantomical:parallel-urldir
Draft

perf: Reparse the game database in parallel#346
Phantomical wants to merge 2 commits intoKSPModdingLibs:devfrom
Phantomical:parallel-urldir

Conversation

@Phantomical
Copy link
Collaborator

This PR has two linked changes:

  • It makes config file parsing thread-safe, and,
  • It parallelizes the game database rebuild at the start of the fast loader.

I have tuned the parallelism to go 3 levels deep from the root (e.g. each GameData/Mod/SubFolder will be done in parallel). This is enough to get some benefit on stock KSP. In my tests it takes about 900ms to process the Squad/Parts folder which is likely to be a lower bound on how much improvement there will be.

As it turns out, this is blocked from getting better performance with a modded folder by what looks to be contention on allocations when creating a modified stream reader, so the improvements seem to only really be about 0.5s in either case. I have a patch lying around that should address this, but I haven't tested it yet.

Stock Profiles

Before:

[KSPCF:FastLoader] 11th Gen Intel(R) Core(TM) i7-11700K @ 3.60GHz | 65371 MB | NVIDIA GeForce RTX 3060 (12110 MB)
Total loading time to main menu : 45.638s
- Configs and assemblies loaded in 6.663s
- Configs reload done in 1.502s
- Configs translated in 0.024s
- 2979 assets loaded in 3.383s :
  - 86 audio assets (79.689 MiB) in 0.499s, 159.719 MiB/s
  - 2238 texture assets (1.357 GiB) in 2.102s, 661.7 MiB/s
  - 654 model assets (67.207 MiB) in 0.780s, 86.209 MiB/s
- Asset bundles loaded in 0.611s
- GameDatabase (configs, resources, traits, upgrades...) loaded in 0.233s
- Built-in parts copied in 0.039s
- Part and internal configs extracted in 0.004s
- 509 parts and 2066 modules compiled in 8.721s
  - 4.1 modules/part, 17.134 ms/part, 4.221 ms/module
  - PartIcon compilation : 1.496s
- 27 internal spaces and 56 props compiled in 0.292s
- 2 DLC (Making History, Breaking Ground) loaded in 6.676s
- Planetary system loaded in 10.772s

After:

[KSPCF:FastLoader] 11th Gen Intel(R) Core(TM) i7-11700K @ 3.60GHz | 65371 MB | NVIDIA GeForce RTX 3060 (12110 MB)
Total loading time to main menu : 45.154s
- Configs and assemblies loaded in 6.525s
- Configs reload done in 1.108s
- Configs translated in 0.031s
- 2979 assets loaded in 3.443s :
  - 86 audio assets (79.689 MiB) in 0.365s, 218.481 MiB/s
  - 2238 texture assets (1.357 GiB) in 2.268s, 613.036 MiB/s
  - 654 model assets (67.207 MiB) in 0.807s, 83.317 MiB/s
- Asset bundles loaded in 0.578s
- GameDatabase (configs, resources, traits, upgrades...) loaded in 0.230s
- Built-in parts copied in 0.029s
- Part and internal configs extracted in 0.003s
- 509 parts and 2066 modules compiled in 8.616s
  - 4.1 modules/part, 16.928 ms/part, 4.171 ms/module
  - PartIcon compilation : 1.443s
- 27 internal spaces and 56 props compiled in 0.290s
- 2 DLC (Making History, Breaking Ground) loaded in 6.722s
- Planetary system loaded in 11.151s

And here's what the breakdown looks like internally
image

The config node uses a couple of mutable global statics in order to
avoid allocations. This means that if multiple threads attempt to load a
config node at the same time then they will stomp all over each other.

This commit fixes this issue by making the relevant statics
`[ThreadStatic]`, so that each thread gets its own version.
During startup FastLoader reloads the whole game database. This seems to
take about 2s on my machine, though I have also see in vary quite a bit
in other people's profiles. This PR parallelizes the directory traversal
and config node parsing. The final result is that it now takes about
900ms to do.

Tuning the parallelism for stock has actually been rather difficult.
What I have here is that it recurses 3 directories deep, so:
`GameData\ModName\InnerFolder`. This is probably undertuned for stock,
since most of the work is in `GameData\Squad\Parts` but going deeper
would likely result in too many tasks flying around (and, tbh, I expect
that to already be the case on a large modded install already).
@Phantomical Phantomical marked this pull request as draft November 23, 2025 09:05
@Phantomical
Copy link
Collaborator Author

Marking as draft until I get this performing like I expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant