Skip to content

Commit

Permalink
Tweaked how interpolation registers are determined in HSD to avoid he…
Browse files Browse the repository at this point in the history
…ap allocations.
  • Loading branch information
MeltyPlayer committed Dec 3, 2024
1 parent 6a6d5d8 commit 1018dbc
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ public static void ReadKeyframes(
keyframes.Clear();

var keys = ReadFObjKeys_(br, datKeyframes);
var interpolations = GetInterpolationsFromFObjKeys_(keys);
Span<InterpolationRegisters> buffer
= stackalloc InterpolationRegisters[keys.Count];
var interpolations = GetInterpolationsFromFObjKeys_(keys, buffer);

if (interpolations.Count == 0) {
if (interpolations.Length == 0) {
return;
}

DatKeyframe? currentKeyframe = null;

var firstInterpolation = interpolations.First.Value;
var firstInterpolation = interpolations[0];
DatKeyframe nextKeyframe = new() {
Frame = firstInterpolation.FromFrame,
IncomingValue = firstInterpolation.FromValue,
Expand Down Expand Up @@ -80,7 +82,7 @@ public static void ReadKeyframes(
}
}

private class InterpolationRegisters {
private readonly struct InterpolationRegisters {
public required GxInterpolationType InterpolationType { get; init; }
public required float FromValue { get; init; }
public required float ToValue { get; init; }
Expand All @@ -94,10 +96,11 @@ private class InterpolationRegisters {
/// Helper method for getting the interpolations between each of the FObj
/// keyframes.
/// </summary>
private static LinkedList<InterpolationRegisters>
private static ReadOnlySpan<InterpolationRegisters>
GetInterpolationsFromFObjKeys_(
IReadOnlyList<FObjKey> keys) {
var registers = new LinkedList<InterpolationRegisters>();
IReadOnlyList<FObjKey> keys,
Span<InterpolationRegisters> buffer) {
var index = 0;

float fromValue = 0;
float toValue = 0;
Expand Down Expand Up @@ -160,19 +163,19 @@ private static LinkedList<InterpolationRegisters>
}

if (timeChanged && fromFrame != toFrame) {
registers.AddLast(new InterpolationRegisters {
buffer[index++] = new InterpolationRegisters {
InterpolationType = interpolationType,
FromValue = fromValue,
ToValue = toValue,
FromTangent = fromTangent,
ToTangent = toTangent,
FromFrame = fromFrame,
ToFrame = toFrame,
});
};
}
}

return registers;
return buffer[..index];
}

private class FObjKey {
Expand Down

0 comments on commit 1018dbc

Please sign in to comment.