Skip to content

ShipEffects: skip heavy Initialize for debris vessels#52

Merged
JonnyOThan merged 3 commits into
KSPModStewards:masterfrom
bimo1d:fix-shipeffects-debris-skip
May 8, 2026
Merged

ShipEffects: skip heavy Initialize for debris vessels#52
JonnyOThan merged 3 commits into
KSPModStewards:masterfrom
bimo1d:fix-shipeffects-debris-skip

Conversation

@bimo1d

@bimo1d bimo1d commented May 7, 2026

Copy link
Copy Markdown
Contributor

Hi,

Was profiling KSP with dotTrace and found a ~150-200ms main-thread hitch every time a part is destroyed or separated in vacuum. Tracked it down to ShipEffects.

When a Separator or any decoupler-style event fires in vacuum, KSP creates a new Vessel for each separated subassembly. On a typical multi-stage rocket that's 3-5 new Vessels in one frame. Each new Vessel instantiates ShipEffects (VesselModule, order 999), and on first OnLoadVessel runs the full Initialize - parses every SoundLayer config, calls GameDatabase.GetAudioClip for each clip, allocates new GameObject for audioParent. Around 33 sound layers per vessel times 5 vessels per separation event = ~150-200ms main thread stall.

The thing is, debris vessels don't need this. They have no engines, no command modules, no useful sound source. A drifting decoupler in space should not be running an audio engine init.

ShipEffects already has an early-exit for single-part vessels (asteroid/EVA fast path) but multi-part debris falls through.

dotTrace stack attached below.

Fix adds a debris check after the single-part exit in Initialize, plus a VesselTypeChange handler so debris that promotes to a real vessel, kerbal boarding a stranded vessel for example, gets a clean reinit. Mirrors the existing single-part asteroid/EVA fast path - sets ignoreVessel=true and returns early. Both FixedUpdate and LateUpdate already short-circuit on ignoreVessel, so debris stay silent the same way asteroids do today (the if (ignoreVessel && controlGroup != PhysicsControl.SONICBOOM) line in the parsing loop has no runtime effect anyway since LateUpdate exits on ignoreVessel before reaching the SonicBoom code).

Tested locally with a Harmony prefix doing the same thing - separation hitch is gone, normal vessels still init as before.

Thanks!

{2D79F6EA-2BFE-441B-B02D-5BADE686828C}

bimo1d and others added 3 commits May 7, 2026 08:19
Adds an early-exit for vesselType Debris/SpaceObject/DroppedPart in Initialize. Avoids ~150-200ms main-thread hitch on every multi-part separation in vacuum (5 new vessels x 33 sound layers each). Subscribes to GameEvents.onVesselTypeChange so debris that promotes to a real vessel (kerbal boarding a stranded vessel etc.) gets a clean reinit.
Removed the re-init handler. Promoted debris (kerbal boarding a stranded vessel) will stay silent until vessel reload - accepted as edge case. Matches existing single-part asteroid behavior, which also doesn't reinit on promotion.
@JonnyOThan JonnyOThan merged commit 71adc5c into KSPModStewards:master May 8, 2026
@bimo1d bimo1d deleted the fix-shipeffects-debris-skip branch May 8, 2026 20:29
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.

2 participants