How we should set InternalBufferCapacity when declaring IBufferElementData?
It’s the attribute that reserves a set amount of size for the buffer in the chunk, before it would swap to a separately allocated buffer (which incurs a slight perf hit compared to reading the array in the chunk).
If the number goes over, an external buffer is allocated.
If the number drops to equal or lower, it deallocates the extra array and uses the chunk memory again.
Here’s some working examples:
// pre-allocates 1 seconds worth of input frames.
[InternalBufferCapacity(60)]
public readonly struct InputFrame : IBufferElementData
{
public readonly uint Age;
public readonly InputFrameFlags Flags;
public InputFrame(InputFrameFlags flags)
{
Flags = flags;
Age = 0u;
}
private InputFrame(InputFrameFlags flags, uint age)
{
Flags = flags;
Age = age;
}
public InputFrame NextAge()
{
return new InputFrame(Flags, Age + 1);
}
}
// pre-allocates 4 potential direction events to be compressed into an a single InputFrame
[InternalBufferCapacity(4)]
public readonly struct DirectionEvent : IBufferElementData
{
public readonly float2 Axis;
public readonly bool IsStick;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DirectionEvent(float2 axis, bool isStick = false)
{
Axis = axis;
IsStick = isStick;
}
}
// pre-allocates 4 potential button events to be compressed into a single InputFrame
[InternalBufferCapacity(4)]
public readonly struct ButtonEvent : IBufferElementData
{
public readonly InputEventId Value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ButtonEvent(InputEventId value) => Value = value;
}
1 Like
If some buffer goes to heap it never back to chunk memory automatically. It will be in heap until you call TrimExcess.
3 Likes
So it’s always better to pick a number that you are sure you will never reach it. right?
Yes if possible