High performance ArraySegment<T> for C#.
Consider the following code for built in ArraySegment.
int n = segment.Array[segment.Offset + i];
IL generates two call instructions because .Array and .Offset are properties:
ArraySegmentX does not need two extra call instructions:
This is a micro optimization. In most cases, it doesn't matter. For heavy number crunching like in Mirror's delta compression, it does matter.
- byte[]: 1.5s
- ArraySegment: 3.6s
- ArraySegmentX: 1.5s
=> 2.4x faster
- byte[]: 1.7s
- ArraySegment: 6.4s
- ArraySegmentX: 1.7s
=> 3.76x faster
Unity's ArraySegment does not support direct [] access:
// supported
int n = segment.Array[segment.Offset + i];
// not supported
int n = segment[i];
ArraySegmentX adds [] access for covenience.
NOTE that this requires an IL call instruction. Use the long way where performance matters.