Skip to content

Commit

Permalink
Revert "FlightIntegrator and friends micro-optimization (#230)"
Browse files Browse the repository at this point in the history
This reverts commit 7346056.
  • Loading branch information
JonnyOThan committed Oct 7, 2024
1 parent 7346056 commit 07288d9
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 2,711 deletions.
4 changes: 0 additions & 4 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,6 @@ KSP_COMMUNITY_FIXES
// state synchronization and caching solar panels scaled space raycasts results.
OptimizedModuleRaycasts = true
// General micro-optimization of FlightIntegrator and VesselPrecalculate, significantely increase
// framerate in large part count situations.
FlightPerf = true
// ##########################
// Modding
// ##########################
Expand Down
4 changes: 0 additions & 4 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
<DoNotPublicize Include="Assembly-CSharp:SaveUpgradePipeline.SaveUpgradePipeline.OnSetCfgNodeVersion" />
<Publicize Include="UnityEngine.CoreModule:UnityEngine.Object.m_CachedPtr" />
<Publicize Include="UnityEngine.IMGUIModule" />
<Publicize Include="UnityEngine.CoreModule:Unity.Collections.NativeArray`1.m_Buffer" />
<Publicize Include="mscorlib:System.IO.MonoIO" />
<Publicize Include="mscorlib:System.IO.MonoIOError" />
<Publicize Include="mscorlib:System.IO.MonoIOStat" />
Expand Down Expand Up @@ -125,10 +124,8 @@
<Compile Include="BugFixes\StrategyDuration.cs" />
<Compile Include="BugFixes\ZeroCostTechNodes.cs" />
<Compile Include="Library\Collections\Deque.cs" />
<Compile Include="Library\Collections\FastStack.cs" />
<Compile Include="Library\Extensions.cs" />
<Compile Include="Library\LocalizationUtils.cs" />
<Compile Include="Library\Numerics.cs" />
<Compile Include="Library\ObjectPool.cs" />
<Compile Include="Modding\ModUpgradePipeline.cs" />
<Compile Include="Performance\AsteroidAndCometDrillCache.cs" />
Expand All @@ -141,7 +138,6 @@
<Compile Include="Performance\DisableMapUpdateInFlight.cs" />
<Compile Include="Performance\DragCubeGeneration.cs" />
<Compile Include="Performance\FastLoader.cs" />
<Compile Include="Performance\FlightPerf.cs" />
<Compile Include="Performance\IMGUIOptimization.cs" />
<Compile Include="Performance\LocalizerPerf.cs" />
<Compile Include="Performance\LowerMinPhysicsDTPerFrame.cs" />
Expand Down
43 changes: 0 additions & 43 deletions KSPCommunityFixes/Library/Collections/FastStack.cs

This file was deleted.

60 changes: 15 additions & 45 deletions KSPCommunityFixes/Library/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using System;
using System.Runtime.CompilerServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace KSPCommunityFixes
{
static class Extensions
{
/// <summary>
/// Transforms a direction by this matrix.
/// </summary>
public static Vector3 MultiplyVector(this Matrix4x4 m, float x, float y, float z)
{
return new Vector3(
m.m00 * x + m.m01 * y + m.m02 * z,
m.m10 * x + m.m11 * y + m.m12 * z,
m.m20 * x + m.m21 * y + m.m22 * z);
}

/// <summary>
/// Get an assembly qualified type name in the "assemblyName:typeName" format
/// </summary>
Expand All @@ -22,46 +34,4 @@ public static bool IsPAWOpen(this Part part)
return part.PartActionWindow.IsNotNullOrDestroyed() && part.PartActionWindow.isActiveAndEnabled;
}
}

static class ParticleBuffer
{
private static NativeArray<ParticleSystem.Particle> particleBuffer = new NativeArray<ParticleSystem.Particle>(1000, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
private static long particleSize = UnsafeUtility.SizeOf<ParticleSystem.Particle>();

/// <summary>
/// Get a native array of active Particle in this ParticleSystem
/// </summary>
/// <param name="particleCount">The amount of particles in the system, usually ParticleSystem.particleCount. After returning, this will be the amount of active particles, which might be lower.</param>
/// <returns></returns>
public static NativeArray<ParticleSystem.Particle> GetParticlesNativeArray(this ParticleSystem particleSystem, ref int particleCount)
{
if (particleBuffer.Length < particleCount)
{
particleBuffer.Dispose();
particleBuffer = new NativeArray<ParticleSystem.Particle>(particleCount * 2, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
}
particleCount = particleSystem.GetParticles(particleBuffer);
return particleBuffer;
}

/// <summary>
/// Get the position of the particle at the specified index, avoiding to have to make copies of the (huge) particle struct
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe Vector3 GetParticlePosition(this NativeArray<ParticleSystem.Particle> buffer, int particleIndex)
{
// note : the position Vector3 is the first field of the struct
return *(Vector3*)((byte*)buffer.m_Buffer + particleIndex * particleSize);
}

/// <summary>
/// Set the position of the particle at the specified index, avoiding to have to make copies of the (huge) particle struct
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetParticlePosition(this NativeArray<ParticleSystem.Particle> buffer, int particleIndex, Vector3 position)
{
// note : the position Vector3 is the first field of the struct
*(Vector3*)((byte*)buffer.m_Buffer + particleIndex * particleSize) = position;
}
}
}
Loading

0 comments on commit 07288d9

Please sign in to comment.