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
+59 -15
View File
@@ -1,46 +1,90 @@
// 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/.
namespace PerformanceFish.Cache;
public static class ByMap<T> where T : new()
{
private static List<(Map map, T value)> _cache = new();
private static List<(Map map, T value)> _cache = Utility.AddNew<List<(Map map, T value)>>();
[ThreadStatic]
private static List<(Map map, T value)>? _cacheThreadStatic;
public static List<(Map map, T value)> GetCache
=> UnityData.IsInMainThread ? _cache
: _cacheThreadStatic ??= Utility.AddNew<List<(Map map, T value)>>();
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _cacheThreadStatic ??= Utility.AddNew<List<(Map map, T value)>>();
}
public static List<(Map map, T value)> GetCacheDirectly => _cache;
public static List<(Map map, T value)> GetCacheDirectly
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _cache;
}
static ByMap() => Utility.All.Add(_cache);
private static Func<Map, T> Initialize { get; }
= typeof(MapComponent).IsAssignableFrom(typeof(T))
? AccessTools.MethodDelegate<Func<Map, T>>(AccessTools.Method(typeof(Map), nameof(Map.GetComponent), Array.Empty<Type>(), new[] { typeof(T) }))
// missing T constraints cause the compiler to complain when trying to just use map => map.GetComponent<T>
: _ => new();
? AccessTools.MethodDelegate<Func<Map, T>>(AccessTools.Method(typeof(Map), nameof(Map.GetComponent),
Type.EmptyTypes, new[] { typeof(T) }))
// missing T constraints cause the compiler to complain when trying to just use map => map.GetComponent<T>
: static _ => new();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T GetFor(Map map)
{
var cache = GetCache;
for (var i = 0; i < cache.Count; i++)
{
var cacheEntry = cache[i];
ref var cacheEntry = ref cache.GetReferenceUnverifiable(i);
if (cacheEntry.map == map)
return cacheEntry.value;
if (Find.Maps.Count <= i + 1
&& !Find.Maps.Contains(cacheEntry.map))
{
cache.RemoveAt(i);
i--;
}
if (EntryValid(i, cacheEntry.map))
continue;
cache.RemoveAt(i);
i--;
}
return AddEntry(map, cache);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetReferenceFor(Map map)
{
var cache = GetCache;
for (var i = 0; i < cache.Count; i++)
{
ref var cacheEntry = ref cache.GetReferenceUnverifiable(i);
if (cacheEntry.map == map)
return ref cacheEntry.value;
if (EntryValid(i, cacheEntry.map))
continue;
cache.RemoveAt(i);
i--;
}
return ref AddEntryByRef(map, cache);
}
private static bool EntryValid(int i, Map map)
=> Find.Maps.Count > i && Find.Maps.Contains(map);
private static T AddEntry(Map map, List<(Map map, T value)> cache)
{
cache.Add((map, Initialize(map)));
return cache[^1].value;
}
private static ref T AddEntryByRef(Map map, List<(Map map, T value)> cache)
{
cache.Add((map, Initialize(map)));
return ref cache.GetReferenceUnverifiable(cache.Count - 1).value;
}
}