Commit 3caae95
authored
Loading optimizations (#269)
Various loading performance focused changes, part of the general loading re-implementation :
- Added performance metrics logging upon reaching the main menu
- Added (almost) entirely custom MU model parser, roughly 3 times the throughput of the stock parser (~300 MB/s on my machine). Will only really benefit to people having fast NVME drives with good random read performance.
- KSPCF now maintains dictionaries of loaded models and texture assets by their url/name, and patch the stock `GameDatabase.GetModel*` / `GameDatabase.GetTexture*` method to use them instead of doing a linear search.
This was especially bad with models, as the method would compare the requested string to the `GameObject.name` property for every model in the database.
Unfortunately, we have no way to know if additional assets are loaded by custom means, so we have to fallback to a linear search when the requested texture isn't known and we can't assume that a texture that isn't found at some point won't be found in the future, so in cases where a not loaded / absent texture is queried, there is no performance improvement.
Overall, this benefit to many scenarios, during initial loading for model parsing and part compilation, and more marginally for scenes switches [when various initialization paths are re-acquiring a texture reference](https://github.com/search?q=GameDatabase.Instance.GetTexture+OR+GameDatabase.Instance.GetModel+language%3AC%23&type=code).
Other changes, also loading related :
- As a part of the `MinorPerfTweaks` patch, patched the `FlightGlobals.fetch` property to not fallback to a `FindObjectOfType()` call when the `FlightGlobals._fetch` field is null, which is always the case during loading. In a stock + BDB test case, this alone was about 10% of the total loading time, 7+ seconds. The call doesn't seem necessary as `FlightGlobals._fetch` is set/unset from `FlightGlobals.Awake()` / `FlightGlobals.OnDestroy()`. I guess there are some very edge cases where a (just about to be destroyed) instance could be acquired by a call to `FindObjectOfType()`, but in any case I would qualify such behavior as a bug. On a side note, the same issue is present with `PhysicsGlobals`, but I haven't been able to detect any benefits in patching that one.
- New patch `PartParsingPerf`
- Slightly faster part icon generation. Part icon GameObject generation is done by cloning the part prefab and basically removing everything on it. The operation represent roughly 35% of the part compilation time, a good two thirds of that being in instantiating the copy of prefab, and running all the associated code (part / modules KSPField scaffolding and Awake() code), only to immediately destroy everything. Unfortunately, this can't really be avoided due to the need to run `PartModule.OnIconCreate()` and in some case, some model-manipulation code in Awake().
- Faster `Part` fields parsing, by creating a dictionary of IL-emitted parser delegates instead of the very generic reflection based stock approach.
Overall, these changes can provide a quite significant boost to loading time, mainly to part compilation and model loading. On a hot boot (config/model/texture loading not throughput limited by I/O), total time from exe launch to main menu for a stock + BDB install is now around 35 seconds on my 5800X3D.1 parent 6d58c6c commit 3caae95
11 files changed
Lines changed: 2453 additions & 110 deletions
File tree
- GameData/KSPCommunityFixes
- KSPCommunityFixes
- Library
- Performance
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
459 | 459 | | |
460 | 460 | | |
461 | 461 | | |
| 462 | + | |
| 463 | + | |
462 | 464 | | |
463 | 465 | | |
464 | 466 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
46 | 57 | | |
47 | 58 | | |
48 | 59 | | |
| |||
64 | 75 | | |
65 | 76 | | |
66 | 77 | | |
67 | | - | |
| 78 | + | |
68 | 79 | | |
69 | 80 | | |
70 | 81 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
| |||
50 | 49 | | |
51 | 50 | | |
52 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
53 | 60 | | |
54 | 61 | | |
55 | 62 | | |
| |||
72 | 79 | | |
73 | 80 | | |
74 | 81 | | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | 82 | | |
81 | 83 | | |
82 | 84 | | |
| |||
113 | 115 | | |
114 | 116 | | |
115 | 117 | | |
116 | | - | |
| 118 | + | |
117 | 119 | | |
118 | 120 | | |
119 | | - | |
120 | 121 | | |
121 | 122 | | |
122 | 123 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
| 158 | + | |
| 159 | + | |
158 | 160 | | |
159 | 161 | | |
160 | 162 | | |
161 | 163 | | |
162 | 164 | | |
163 | 165 | | |
164 | 166 | | |
| 167 | + | |
165 | 168 | | |
166 | 169 | | |
167 | 170 | | |
| |||
173 | 176 | | |
174 | 177 | | |
175 | 178 | | |
| 179 | + | |
176 | 180 | | |
177 | 181 | | |
178 | 182 | | |
| |||
254 | 258 | | |
255 | 259 | | |
256 | 260 | | |
| 261 | + | |
257 | 262 | | |
258 | 263 | | |
259 | 264 | | |
| |||
0 commit comments