perf: Pre-compute harvester raycasts at frame start#170
Merged
Phantomical merged 1 commit intoMay 11, 2026
Conversation
Before this commit about 1/4 of the runtime of the various harvesters is spent running raycasts. This can also end up being much worse because occasionally a raycast will trigger a transform sync. Instead of doing this, we can use RaycastCommand.ScheduleBatch to queue all of these up at the start of FixedUpdate, and then individual consumers can look up the result later on when they need it. Consumers subscribe for a single ray trace per part module, with a transform that defines where the array originates from. The RaycastManager seems to take ~22us with 16 active drills, most of which is spent scheduling the 3 resulting jobs. The actual jobs themselves take about 50us, so this work should scale pretty well to much larger vessels if needed.
Merged
Phantomical
added a commit
that referenced
this pull request
May 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before this commit about 1/4 of the runtime of the various harvesters is spent running raycasts. This can also end up being much worse because occasionally a raycast will trigger a transform sync.
Instead of doing this, we can use RaycastCommand.ScheduleBatch to queue all of these up at the start of FixedUpdate, and then individual consumers can look up the result later on when they need it. Consumers subscribe for a single ray trace per part module, with a transform that defines where the array originates from.
The RaycastManager seems to take ~22us with 16 active drills, most of which is spent scheduling the 3 resulting jobs. The actual jobs themselves take about 50us, so this work should scale pretty well to much larger vessels if needed.
Here's what the raycast dispatch looks like. If we ever decide to use burst for other stuff then this could be burst-compiled but otherwise it really doesn't matter.

And here's what an individual converter looks like with this (and KSPModdingLibs/KSPCommunityFixes#374)

Overall test profile for an asteroid harvester

I think this is the final bit needed for #167