Skip to content

Commit

Permalink
Revert "[KK][GT] Cache temporary arrays in ChaControl.UpdateVisible (#47
Browse files Browse the repository at this point in the history
)"

This reverts commit 3124e14.
  • Loading branch information
ManlyMarco committed Sep 1, 2023
1 parent 739a4da commit 560ff2c
Showing 1 changed file with 0 additions and 76 deletions.
76 changes: 0 additions & 76 deletions src/KK_Fix_GarbageTruck/GarbageTruck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,82 +379,6 @@ public int GetHashCode(ChaReference.RefObjKey x)
return ((int)x).GetHashCode();
}
}

/// <summary>
/// Cache allocations of many small arrays in ChaControl.UpdateVisible.
/// </summary>
[HarmonyTranspiler]
[HarmonyPatch(typeof(ChaControl), nameof(ChaControl.UpdateVisible))]
private static IEnumerable<CodeInstruction> FixUpdateVisible(IEnumerable<CodeInstruction> insts)
{
var matcher = new CodeMatcher(insts);
var get1DArray = AccessTools.Method(typeof(AntiGarbageHooks), nameof(Get1DArrayForUpdateVisible));
var get2DArray = AccessTools.Method(typeof(AntiGarbageHooks), nameof(Get2DArrayForUpdateVisible));
var array2DTypes = new Dictionary<Type, Type> {
{ typeof(byte[,]), typeof(byte) },
{ typeof(bool[,]), typeof(bool) },
{ typeof(ChaReference.RefObjKey[,]), typeof(ChaReference.RefObjKey) },
};
int id = 0;
matcher
.MatchForward(true, new CodeMatch(OpCodes.Newarr))
.Repeat(m => {
var elementType = m.Instruction.operand as Type;
m
.SetAndAdvance(OpCodes.Ldc_I4, id)
.Insert(
new CodeInstruction(OpCodes.Ldtoken, elementType),
new CodeInstruction(OpCodes.Call, get1DArray));
id++;
})
.Start()
.MatchForward(true,
new CodeMatch(inst =>
inst.opcode == OpCodes.Newobj &&
inst.operand is ConstructorInfo constructor &&
array2DTypes.ContainsKey(constructor.DeclaringType)))
.Repeat(m => {
var elementType = array2DTypes[(m.Instruction.operand as ConstructorInfo).DeclaringType];
m
.SetAndAdvance(OpCodes.Ldc_I4, id)
.Insert(
new CodeInstruction(OpCodes.Ldtoken, elementType),
new CodeInstruction(OpCodes.Call, get2DArray));
id++;
});
if (id != _updateVisibleAllocationCount)
throw new Exception($"Unexpected number of array allocations in UpdateVisible: {id}");
Array.Clear(_updateVisibleArrays, 0, _updateVisibleAllocationCount);
return matcher.Instructions();
}
private static readonly int _updateVisibleAllocationCount = 45;
private static Array[] _updateVisibleArrays = new Array[_updateVisibleAllocationCount];
private static Array Get1DArrayForUpdateVisible(int size, int id, RuntimeTypeHandle typeHandle)
{
var arr = _updateVisibleArrays[id];
if (arr != null)
{
Array.Clear(arr, 0, size);
return arr;
}

arr = Array.CreateInstance(Type.GetTypeFromHandle(typeHandle), size);
_updateVisibleArrays[id] = arr;
return arr;
}
private static Array Get2DArrayForUpdateVisible(int size0, int size1, int id, RuntimeTypeHandle typeHandle)
{
var arr = _updateVisibleArrays[id];
if (arr != null)
{
Array.Clear(arr, 0, size0 * size1);
return arr;
}

arr = Array.CreateInstance(Type.GetTypeFromHandle(typeHandle), size0, size1);
_updateVisibleArrays[id] = arr;
return arr;
}
}
}
}

0 comments on commit 560ff2c

Please sign in to comment.