Stabilize RimWorld 1.6 caches and tick optimizations

This commit is contained in:
2026-10-02 04:00:27 -04:00
parent 3199c1a85e
commit 227dcd173f
14 changed files with 209 additions and 76 deletions
@@ -535,6 +535,13 @@ public sealed class ReflectionCaching : ClassWithFishPatches
public static bool Prefix(ICustomAttributeProvider obj, Type attributeType, bool inherit,
out object[]? __result, out CustomAttributeState __state)
{
if (obj is null || attributeType is null)
{
__state = default;
__result = null;
return true;
}
var key = new CustomAttributeCache(obj, attributeType.TypeHandle, inherit);
__state = new() { Key = key };
@@ -656,7 +663,9 @@ public sealed class ReflectionCaching : ClassWithFishPatches
{
var value = new CustomAttributeCacheValue
{
Attributes = CopyCachedAttributes(result),
Attributes = result.Length == 0
? (object[])Array.CreateInstance(Type.GetTypeFromHandle(key.Second), 0)
: CopyCachedAttributes(result),
Status = CustomAttributeCacheStatus.Completed
};
@@ -693,7 +702,7 @@ public sealed class ReflectionCaching : ClassWithFishPatches
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static object[] CopyCachedAttributes(object[] attributes)
=> attributes.Length != 0 ? (object[])attributes.Clone() : Array.Empty<object>();
=> attributes.Length != 0 ? (object[])attributes.Clone() : attributes;
private static object _lock = new();
}
@@ -1024,8 +1033,6 @@ public sealed class ReflectionCaching : ClassWithFishPatches
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Prefix(FieldInfo __instance, object? obj, ref object? __result)
{
ValidateGetValueCall(__instance, obj);
ref var cache = ref FieldGetters.GetOrAddReference(__instance.FieldHandle);
if (cache.Dirty
@@ -1035,6 +1042,8 @@ public sealed class ReflectionCaching : ClassWithFishPatches
return true;
}
// Unsupported fields fall back to Mono's implementation, including its exception behavior.
ValidateGetValueCall(__instance, obj);
__result = cache.Result!(obj);
return false;
}