Lots of new optimizations utilizing prepatcher

To start, this mod now requires Prepatcher and Fishery.
New optimizations include: gene ticking improvements, world object improvements, hediff optimizations, world pawn gc optimizations, better mothballing, dds support, ContentFinder caching, additions and changes to reflection caching, as well as various other additions. Multiplayer support has been improved too, but should be considered highly experimental. The WorkGiver_DoBill bug is fixed in this build. Multiple patches aiming to improve RimWorld's load time have been added too.
This commit is contained in:
bbradson
2023-06-03 20:24:38 +02:00
parent cb0a407425
commit ca9321add8
125 changed files with 11272 additions and 3681 deletions
@@ -1,4 +1,4 @@
// Copyright (c) 2022 bradson
// Copyright (c) 2023 bradson
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
@@ -7,8 +7,12 @@
//mostly integrated into vanilla with 1.4
using System.Linq;
using AllDesignationsOnCache = PerformanceFish.Cache.ByIntRefreshable<Verse.DesignationManager, Verse.Thing, PerformanceFish.DesignationManagerCaching.DesignationCache<Verse.Thing>>;
using DesignationsOfDefCache = PerformanceFish.Cache.ByIntRefreshable<Verse.DesignationManager, Verse.DesignationDef, PerformanceFish.DesignationManagerCaching.DesignationCache<Verse.DesignationDef>>;
using AllDesignationsOnCache =
PerformanceFish.Cache.ByIntRefreshable<Verse.DesignationManager, Verse.Thing,
PerformanceFish.DesignationManagerCaching.DesignationCache<Verse.Thing>>;
using DesignationsOfDefCache =
PerformanceFish.Cache.ByIntRefreshable<Verse.DesignationManager, Verse.DesignationDef,
PerformanceFish.DesignationManagerCaching.DesignationCache<Verse.DesignationDef>>;
namespace PerformanceFish;
@@ -17,8 +21,12 @@ public class DesignationManagerCaching : ClassWithFishPatches
public class SpawnedDesignationsOfDef_Patch : FishPatch
{
public override string Description => "Designation caching";
public override Expression<Action> TargetMethod => () => default(DesignationManager)!.SpawnedDesignationsOfDef(null);
public static bool Prefix(DesignationManager __instance, DesignationDef def, ref IEnumerable<Designation> __result, out bool __state)
public override Expression<Action> TargetMethod
=> () => default(DesignationManager)!.SpawnedDesignationsOfDef(null);
public static bool Prefix(DesignationManager __instance, DesignationDef def,
ref IEnumerable<Designation> __result, out bool __state)
{
if (def.GetType() != typeof(DesignationDef)) // different types have different indexing. Can't use those
{
@@ -32,15 +40,18 @@ public class DesignationManagerCaching : ClassWithFishPatches
__result = value.designations;
return __state = false;
}
public static void Postfix(DesignationManager __instance, DesignationDef def, bool __state, IEnumerable<Designation> __result)
public static void Postfix(DesignationManager __instance, DesignationDef def, bool __state,
IEnumerable<Designation> __result)
{
if (__state)
RefreshDesignationCache(DesignationsOfDefCache.Get, __instance, def, __result);
}
}
public static void RefreshDesignationCache<T>(/*Cache.ByIndex<DesignationManager, T, DesignationCache<T>>*/
Dictionary<Cache.ByIntRefreshable<DesignationManager, T, DesignationCache<T>>, DesignationCache<T>> dict, DesignationManager instance, T key, IEnumerable<Designation> result)
public static void RefreshDesignationCache<T>( /*Cache.ByIndex<DesignationManager, T, DesignationCache<T>>*/
Dictionary<Cache.ByIntRefreshable<DesignationManager, T, DesignationCache<T>>, DesignationCache<T>> dict,
DesignationManager instance, T key, IEnumerable<Designation> result)
where T : notnull
{
if (dict.TryGetValue(new(instance, key), out var cache))
@@ -54,16 +65,24 @@ public class DesignationManagerCaching : ClassWithFishPatches
public class AnySpawnedDesignationOfDef_Patch : FishPatch
{
public override string Description => "Designation caching";
public override Expression<Action> TargetMethod => () => default(DesignationManager)!.AnySpawnedDesignationOfDef(null);
public static bool AnySpawnedDesignationOfDef(DesignationManager __instance, DesignationDef def) => __instance.SpawnedDesignationsOfDef(def).Any();
public static CodeInstructions Transpiler(CodeInstructions CodeInstructions) => Reflection.GetCodeInstructions(AnySpawnedDesignationOfDef);
public override Expression<Action> TargetMethod
=> () => default(DesignationManager)!.AnySpawnedDesignationOfDef(null);
public static bool AnySpawnedDesignationOfDef(DesignationManager __instance, DesignationDef def)
=> __instance.SpawnedDesignationsOfDef(def).Any();
public static CodeInstructions Transpiler(CodeInstructions CodeInstructions)
=> Reflection.GetCodeInstructions(AnySpawnedDesignationOfDef);
}
public class AllDesignationsOn_Patch : FishPatch
{
public override string Description => "Designation caching";
public override Expression<Action> TargetMethod => () => default(DesignationManager)!.AllDesignationsOn(null);
public static bool Prefix(DesignationManager __instance, Thing t, ref IEnumerable<Designation> __result, out bool __state)
public static bool Prefix(DesignationManager __instance, Thing t, ref IEnumerable<Designation> __result,
out bool __state)
{
if (!AllDesignationsOnCache.TryGetValue(new(__instance, t), out var value))
return __state = true;
@@ -71,7 +90,9 @@ public class DesignationManagerCaching : ClassWithFishPatches
__result = value.designations;
return __state = false;
}
public static void Postfix(DesignationManager __instance, Thing t, bool __state, IEnumerable<Designation> __result)
public static void Postfix(DesignationManager __instance, Thing t, bool __state,
IEnumerable<Designation> __result)
{
if (__state)
RefreshDesignationCache(AllDesignationsOnCache.Get, __instance, t, __result);
@@ -82,15 +103,22 @@ public class DesignationManagerCaching : ClassWithFishPatches
{
public override string Description => "Designation caching";
public override Expression<Action> TargetMethod => () => default(DesignationManager)!.DesignationOn(null);
public static Designation DesignationOn(DesignationManager __instance, Thing t) => __instance.AllDesignationsOn(t).FirstOrDefault();
public static CodeInstructions Transpiler(CodeInstructions CodeInstructions) => Reflection.GetCodeInstructions(DesignationOn);
public static Designation DesignationOn(DesignationManager __instance, Thing t)
=> __instance.AllDesignationsOn(t).FirstOrDefault();
public static CodeInstructions Transpiler(CodeInstructions CodeInstructions)
=> Reflection.GetCodeInstructions(DesignationOn);
}
public class DesignationOn_ByDef_Patch : FishPatch
{
public override string Description => "Designation caching";
public override Expression<Action> TargetMethod => () => default(DesignationManager)!.DesignationOn(null, null);
public static Designation? DesignationOn(DesignationManager __instance, Thing t, DesignationDef def) => FirstDesignationOfDef(__instance.AllDesignationsOn(t), def);
public static Designation? DesignationOn(DesignationManager __instance, Thing t, DesignationDef def)
=> FirstDesignationOfDef(__instance.AllDesignationsOn(t), def);
public static Designation? FirstDesignationOfDef(IEnumerable<Designation> designations, DesignationDef def)
{
if (designations is not List<Designation> designationList)
@@ -102,8 +130,10 @@ public class DesignationManagerCaching : ClassWithFishPatches
if (designation.def == def)
return designation;
}
return null;
}
public static Designation? Fallback(IEnumerable<Designation> designations, DesignationDef def)
{
foreach (var designation in designations)
@@ -111,20 +141,28 @@ public class DesignationManagerCaching : ClassWithFishPatches
if (designation.def == def)
return designation;
}
return null;
}
public static CodeInstructions Transpiler(CodeInstructions CodeInstructions) => Reflection.GetCodeInstructions(DesignationOn);
public static CodeInstructions Transpiler(CodeInstructions CodeInstructions)
=> Reflection.GetCodeInstructions(DesignationOn);
}
public class HasMapDesignationOn_Patch : FishPatch
{
public override string Description => "Designation caching";
public override Expression<Action> TargetMethod => () => default(DesignationManager)!.HasMapDesignationOn(null);
public static bool HasMapDesignationOn(DesignationManager __instance, Thing t) => __instance.AllDesignationsOn(t).Any();
public static CodeInstructions Transpiler(CodeInstructions CodeInstructions) => Reflection.GetCodeInstructions(HasMapDesignationOn);
public static bool HasMapDesignationOn(DesignationManager __instance, Thing t)
=> __instance.AllDesignationsOn(t).Any();
public static CodeInstructions Transpiler(CodeInstructions CodeInstructions)
=> Reflection.GetCodeInstructions(HasMapDesignationOn);
}
public struct DesignationCache<T> : Cache.IIsRefreshable<Cache.ByIntRefreshable<DesignationManager, T, DesignationCache<T>>, DesignationCache<T>>
public record struct DesignationCache<T> : Cache.IIsRefreshable<
Cache.ByIntRefreshable<DesignationManager, T, DesignationCache<T>>, DesignationCache<T>>
where T : notnull
{
public IEnumerable<Designation> designations;
@@ -139,6 +177,7 @@ public class DesignationManagerCaching : ClassWithFishPatches
_manager = manager;
_managerListState = manager.allDesignations.Version();
}
public void UpdateList(IEnumerable<Designation> designations)
{
var cachedList = (List<Designation>)this.designations;
@@ -147,12 +186,17 @@ public class DesignationManagerCaching : ClassWithFishPatches
ShouldRefreshNow = false;
_managerListState = _manager.allDesignations.Version();
}
public bool ShouldRefreshNow
{
get => _nextRefreshTick < Current.Game.tickManager.TicksGame || _managerListState != _manager.allDesignations.Version();
get
=> _nextRefreshTick < Current.Game.tickManager.TicksGame
|| _managerListState != _manager.allDesignations.Version();
set => _nextRefreshTick = value ? 0 : Current.Game.tickManager.TicksGame + 3072 + Math.Abs(Rand.Int % 2048);
}
public DesignationCache<T> SetNewValue(Cache.ByIntRefreshable<DesignationManager, T, DesignationCache<T>> key) => throw new NotImplementedException();
public DesignationCache<T> SetNewValue(Cache.ByIntRefreshable<DesignationManager, T, DesignationCache<T>> key)
=> throw new NotImplementedException();
}
}