Small source generator that enables efficient unpacking of IEnumerable's members to 1d and 2d arrays.
$ dotnet add package IEnumerableUnpacker
IEnumerableUnpacker is a C# library that provides a quick and efficient way to unpack IEnumerable<T> to 1D and 2D arrays of T's members. It leverages parallel processing and optimized memory copying techniques to achieve high-performance array unpacking. 🚀
IEnumerable<T> to 1D and 2D arrays of T's membersYou can install IEnumerableUnpacker via NuGet Package Manager:
Install-Package IEnumerableUnpacker
Here's an example of how to use IEnumerableUnpacker:
[Unpackable]
public class UnpackMe<Titem, Titem2, UselessGeneric>
{
[Unpack("MyItegersOut")]
public int[] myIntegers;
[Unpack("MyIntegerOut")]
public int myInteger;
[Unpack("MyFloatsOut")]
public float[] myFloats;
[Unpack("MyGenericOut")]
public Titem[] myGeneric;
[Unpack("MyGeneric2Out")]
public Titem2 myGeneric2;
}
public static unsafe void UnpackUnpackMe<Titem, Titem2, UselessGeneric>(this IEnumerable<UnpackMe<Titem, Titem2, UselessGeneric>> source, out int[,] MyItegersOut, out int[] MyIntegerOut, out float[,] MyFloatsOut, out Titem[,] MyGenericOut, out Titem2[] MyGeneric2Out)
{
// Unpacking logic...
}
In this example:
Unpackable attribute is used to mark the class for unpacking.Unpack attribute is used to specify the output parameter names for the unpacked arrays.Unpack attribute will not be unpacked.For more detailed benchmarks and comparisons, please visit the project repository. 📊
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository. 😊
ArrayUnpacker is licensed under the MIT License.
I've decided to use 2D arrays because that's what I use with TorchSharp. Unpacking to flat types might be faster, so feel free to reach out to me if it's required! 📫