I\'m have a large list of value types that needs to be given to OpenGL. It would be great if this could happen as quickly as possible. What I\'m doing now looks like this:
Since you're using GL I'll assume you know what you're doing and skip all the caveats. Try this, or see https://stackoverflow.com/a/35588774/194921
[StructLayout(LayoutKind.Explicit)]
public struct ConvertHelper<TFrom, TTo>
where TFrom : class
where TTo : class {
[FieldOffset( 0)] public long before;
[FieldOffset( 8)] public TFrom input;
[FieldOffset(16)] public TTo output;
static public TTo Convert(TFrom thing) {
var helper = new ConvertHelper<TFrom, TTo> { input = thing };
unsafe {
long* dangerous = &helper.before;
dangerous[2] = dangerous[1]; // ie, output = input
}
var ret = helper.output;
helper.input = null;
helper.output = null;
return ret;
}
}
class PublicList<T> {
public T[] _items;
}
public static T[] GetBackingArray<T>(this List<T> list) {
return ConvertHelper<List<T>, PublicList<T>>.Convert(list)._items;
}
If you need to access internal array repeatedly, it good practice to store accessor as delegate.
In this example, it's delegate to dynamic method. First call may not be fast, but subsequent calls (on List of same type) will be much faster.
public static class ListExtensions
{
static class ArrayAccessor<T>
{
public static Func<List<T>, T[]> Getter;
static ArrayAccessor()
{
var dm = new DynamicMethod("get", MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard, typeof(T[]), new Type[] { typeof(List<T>) }, typeof(ArrayAccessor<T>), true);
var il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0); // Load List<T> argument
il.Emit(OpCodes.Ldfld, typeof(List<T>).GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance)); // Replace argument by field
il.Emit(OpCodes.Ret); // Return field
Getter = (Func<List<T>, T[]>)dm.CreateDelegate(typeof(Func<List<T>, T[]>));
}
}
public static T[] GetInternalArray<T>(this List<T> list)
{
return ArrayAccessor<T>.Getter(list);
}
}
Make sure to include:
using System.Reflection;
using System.Reflection.Emit;
The IList<T> interface isn't that difficult to do (well, not so long as Reflector is free and functioning, hint hint).
You can create your own implementation and expose the internal array as a public property.
You may want to consider if your approach to this is wrong. If you find yourself using reflection to do this - you've already lost.
I can think of a few ways to approach this though which one is ideal depends a lot on whether this is a multi-threaded piece of code or not.
Let's assume it's not ...
Think about the characteristics of the array. Each time this method is called an N-length array is created. Your goal is to improve performance (which implies you want to minimize allocations and data copies).
Can you hint at compile or runtime what the ideal starting size for the array is? I mean - if 95% of the time the N-length is 100k or less ... start with a 100k item array. Keep using it until you hit a case where the array is too small.
When you hit this case you can decide what you do based on your understanding of the program. Should the array grow 10%? Should it grow to the literal needed length? Can you use what you have and continue the process for the rest of the data?
Over time the ideal size will be found. You can even have your program monitor the final size each time it runs and use that as a hint for allocation the next time it starts (perhaps this array length depends on environmental factors such as resolution, etc).
In other words - what I'm suggesting is that you not use the List-to-Array method and that you pre-allocate an array, keep it around forever, and grow it as needed.
If your program has threading issues you will obviously need to address those.
I wouldn't recommend what you want to do. Why are you using a List<T>
in the first place? If you can tell us precisely what characteristics the data-structure that you want to create should have, and how it should interface with the consuming API, we might be able to give you a proper solution to your problem.
But I will try to answer the question as asked.
Can I do this without copying, like somehow get a pointer to the array used internally by List?
Yes, although you would be relying on an undocumented implementation detail. As of NET 4.0, the backing array field is called _items
.
Vertex[] vertices = (Vertex[]) typeof(List<Vertex>)
.GetField("_items", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(VList);
Do note that this array will almost certainly have slack at the end (that's the whole point of List<T>
), so array.Length
on this array won't be all that useful. The API that consumes the array would need to be notified of the "real" length of the array through other means (by telling it what the list's real Count
was).
You can do that with reflection:
public static T[] GetUnderlyingArray<T>(this List<T> list)
{
var field = list.GetType().GetField("_items",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
return (T[])field.GetValue(list);
}
edit: ah someone already said it while I was testing this..