I\'ve found myself in a position where I have to roll my own dynamic array implementation, due to various large performance benefits (in my case). However, after creating an enu
Okay, aside from benchmarking problems, here's how you can make your DynamicArray
class more like List
:
public DynamicArrayEnumerator GetEnumerator()
{
return new DynamicArrayEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
Now, code which knows it's working with a dynamic array can iterate with a DynamicArrayEnumerator
without any boxing, and without virtual dispatch. This is exactly what List
does. The compiler notices when a type implements the pattern in a custom manner, and will use the types involved instead of the interfaces.
With your current code, you're getting no benefit from creating a struct
- because you're boxing it in GetEnumerator()
.
Try the above change and fix the benchmark to work for longer. I'd expect to see a big difference.