Skip to content

Commit 431769f

Browse files
authored
Remove constant overhead from ModuleColorChanger.Update() (#304)
Reduce the constant overhead from ModuleColorChanger by avoiding re-setting the shader property when the state hasn't changed
1 parent afcc469 commit 431769f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

GameData/KSPCommunityFixes/Settings.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ KSP_COMMUNITY_FIXES
478478
// Significantly reduces the time it takes to open the craft browser and to search by name. Most noticeable with lots of craft.
479479
CraftBrowserOptimisations = true
480480

481+
// Reduce the constant overhead from ModuleColorChanger
482+
ModuleColorChangerOptimization = true
483+
481484
// ##########################
482485
// Modding
483486
// ##########################

KSPCommunityFixes/KSPCommunityFixes.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
<Compile Include="BugFixes\DoubleCurvePreserveTangents.cs" />
167167
<Compile Include="BugFixes\RestoreMaxPhysicsDT.cs" />
168168
<Compile Include="Performance\FloatingOriginPerf.cs" />
169+
<Compile Include="Performance\ModuleColorChangerOptimization.cs" />
169170
<Compile Include="Performance\OptimisedVectorLines.cs" />
170171
<Compile Include="Performance\GameDatabasePerf.cs" />
171172
<Compile Include="Performance\PartSystemsFastUpdate.cs" />
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace KSPCommunityFixes.Performance
2+
{
3+
internal class ModuleColorChangerOptimization : BasePatch
4+
{
5+
protected override void ApplyPatches()
6+
{
7+
AddPatch(PatchType.Postfix, typeof(ModuleColorChanger), nameof(ModuleColorChanger.Start));
8+
AddPatch(PatchType.Override, typeof(ModuleColorChanger), nameof(ModuleColorChanger.FixedUpdate));
9+
AddPatch(PatchType.Override, typeof(ModuleColorChanger), nameof(ModuleColorChanger.Update));
10+
}
11+
12+
static void ModuleColorChanger_Start_Postfix(ModuleColorChanger __instance)
13+
{
14+
__instance.SetState(__instance.currentRateState); // ensure initial state is correct
15+
}
16+
17+
static void ModuleColorChanger_FixedUpdate_Override(ModuleColorChanger mcc)
18+
{
19+
return; // state checking moved to Update()
20+
}
21+
22+
static void ModuleColorChanger_Update_Override(ModuleColorChanger mcc)
23+
{
24+
if (!mcc.useRate || !mcc.isValid)
25+
return;
26+
27+
if (HighLogic.LoadedSceneIsEditor && mcc.part.frozen)
28+
return;
29+
30+
if (mcc.animState)
31+
{
32+
if (mcc.currentRateState < 1f)
33+
{
34+
mcc.currentRateState += mcc.animRate * TimeWarp.deltaTime;
35+
if (mcc.currentRateState > 1f)
36+
mcc.currentRateState = 1f;
37+
38+
mcc.SetState(mcc.currentRateState);
39+
}
40+
}
41+
else
42+
{
43+
if (mcc.currentRateState > 0f)
44+
{
45+
mcc.currentRateState -= mcc.animRate * TimeWarp.deltaTime;
46+
if (mcc.currentRateState < 0f)
47+
mcc.currentRateState = 0f;
48+
49+
mcc.SetState(mcc.currentRateState);
50+
}
51+
}
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)