Skip to content

perf: Pre-compute harvester raycasts at frame start#170

Merged
Phantomical merged 1 commit into
KSPModStewards:masterfrom
Phantomical:bulk-drill-raytrace
May 11, 2026
Merged

perf: Pre-compute harvester raycasts at frame start#170
Phantomical merged 1 commit into
KSPModStewards:masterfrom
Phantomical:bulk-drill-raytrace

Conversation

@Phantomical
Copy link
Copy Markdown
Contributor

@Phantomical Phantomical commented May 11, 2026

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.
image

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

Overall test profile for an asteroid harvester
image

I think this is the final bit needed for #167

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.
@Phantomical Phantomical merged commit 1007e29 into KSPModStewards:master May 11, 2026
1 check passed
@Phantomical Phantomical deleted the bulk-drill-raytrace branch May 11, 2026 18:47
@Phantomical Phantomical mentioned this pull request May 11, 2026
Phantomical added a commit that referenced this pull request May 11, 2026
This adds changelog entries for #170 and #169
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant