.Dump() and the ZLinq library
Hi everyone,
I'm investigating the ZLinq, a highly optimized Linq implementation, which aims at reducing allocations.
I tried it with LinqPad, but the normal .Dump()
does not work out of the box. For dotnet 9, ZLinq uses ref struct
s, which makes calling Dump
fail for most scenarios.
I've created a custom "overload" to handle ValueEnumerable
results:
public static class DumpExtensions { public static ValueEnumerable<TEnumerator, T> Dump<TEnumerator, T>(this ValueEnumerable<TEnumerator, T> valueEnumerable) where TEnumerator : struct, IValueEnumerator<T> // Comment out next line for dotnet < 9 , allows ref struct { if (valueEnumerable.Enumerator.TryGetSpan(out var span)) { // Special case handling, if a Span can be accessed span.Dump(); } else { if (valueEnumerable.TryGetNonEnumeratedCount(out var count)) { // Todo: use count for displaying total number of items } var maxRows = Util.DumpDefaults.MaxRows ?? 1000; using var pooledArray = valueEnumerable .Take(maxRows) .ToArrayPool(); pooledArray.ArraySegment.Dump(); } return valueEnumerable; } }
This seems to work for me so far. Is anyone else interested in this and does it make sense to extend it?
There are some issues, for example the type is not displayed correctly. I'd like to have ValueEnumerable<Int32> (First 1000 items of 10000)
instead of ArraySegment<Int32> (10 items)
. But I couldn't figure out a way to achieve this. Any ideas?
Kind regards
Peter
Comments
Might be related to this one.
How about this: