Skip to content

Commit

Permalink
add MinorPerfTweaks patch
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyOThan committed Oct 14, 2024
1 parent f62ccb2 commit e72c3c1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ KSP_COMMUNITY_FIXES
// strut position tracking...
PartSystemsFastUpdate = true
// Various small performance patches (volume normalizer, eva module checks)
MinorPerfTweaks = true
// General micro-optimization of FlightIntegrator and VesselPrecalculate, significantely increase
// framerate in large part count situations.
FlightPerf = true
Expand Down
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
<Compile Include="Modding\ReflectionTypeLoadExceptionHandler.cs" />
<Compile Include="Performance\FewerSaves.cs" />
<Compile Include="Performance\ConfigNodePerf.cs" />
<Compile Include="Performance\MinorPerfTweaks.cs" />
<Compile Include="Performance\ModuleDockingNodeFindOtherNodesFaster.cs" />
<Compile Include="Performance\OptimizedModuleRaycasts.cs" />
<Compile Include="Performance\PQSCoroutineLeak.cs" />
Expand Down
78 changes: 78 additions & 0 deletions KSPCommunityFixes/Performance/MinorPerfTweaks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace KSPCommunityFixes.Performance
{
internal class MinorPerfTweaks : BasePatch
{
protected override Version VersionMin => new Version(1, 12, 3);

protected override void ApplyPatches()
{
AddPatch(PatchType.Override, typeof(Part), nameof(Part.isKerbalEVA));

AddPatch(PatchType.Override, typeof(VolumeNormalizer), nameof(VolumeNormalizer.Update));
}

// Called (sometimes multiple times) in Part.FixedUpdate()
public static bool Part_isKerbalEVA_Override(Part part)
{
part.cachedModules ??= new Dictionary<Type, PartModule>(10);

if (!part.cachedModules.TryGetValue(typeof(KerbalEVA), out PartModule module))
{
if (part.modules == null)
return false;

List<PartModule> modules = part.modules.modules;
int moduleCount = modules.Count;
for (int i = 0; i < moduleCount; i++)
{
if (modules[i] is KerbalEVA)
{
module = modules[i];
break;
}
}

part.cachedModules[typeof(KerbalEVA)] = module;
}

return module.IsNotNullRef();
}

// setting AudioListener.volume is actually quite costly (0.7% of the frame time),
// so avoid setting it when the value hasn't actually changed...
private static void VolumeNormalizer_Update_Override(VolumeNormalizer vn)
{
float newVolume;
if (GameSettings.SOUND_NORMALIZER_ENABLED)
{
vn.threshold = GameSettings.SOUND_NORMALIZER_THRESHOLD;
vn.sharpness = GameSettings.SOUND_NORMALIZER_RESPONSIVENESS;
AudioListener.GetOutputData(vn.samples, 0);
vn.level = 0f;

for (int i = 0; i < vn.sampleCount; i += 1 + GameSettings.SOUND_NORMALIZER_SKIPSAMPLES)
vn.level = Mathf.Max(vn.level, Mathf.Abs(vn.samples[i]));

if (vn.level > vn.threshold)
newVolume = vn.threshold / vn.level;
else
newVolume = 1f;

newVolume = Mathf.Lerp(AudioListener.volume, newVolume * GameSettings.MASTER_VOLUME, vn.sharpness * Time.deltaTime);
}
else
{
newVolume = Mathf.Lerp(AudioListener.volume, GameSettings.MASTER_VOLUME, vn.sharpness * Time.deltaTime);
}

if (newVolume != vn.volume)
AudioListener.volume = newVolume;

vn.volume = newVolume;
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ User options are available from the "ESC" in-game settings menu :<br/><img src="
- [**ModuleDockingNodeFindOtherNodesFaster**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/257) [KSP 1.12.3 - 1.12.5]<br/>Faster lookup of other docking nodes.
- [**CollisionEnhancerFastUpdate**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/257) [KSP 1.12.3 - 1.12.5]<br/>Optimization of the `CollisionEnhancer` component (responsible for part to terrain collision detection).
- [**PartSystemsFastUpdate**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/257) [KSP 1.12.3 - 1.12.5]<br/>Optimization of various flight scene auxiliary subsystems : temperature gauges, highlighter, strut position tracking...
- [**MinorPerfTweaks**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/257) [KSP 1.12.3 - 1.12.5]<br/>Various small performance patches (volume normalizer, eva module checks)
- [**FasterPartFindTransform**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/255) [KSP 1.12.3 - 1.12.5]<br/>Faster, and minimal GC alloc relacements for the Part FindModelTransform* and FindHeirarchyTransform* methods.

#### API and modding tools
Expand Down

0 comments on commit e72c3c1

Please sign in to comment.