dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.91k stars 4.63k forks source link

Frozen collection follow-ups #77891

Open stephentoub opened 1 year ago

stephentoub commented 1 year ago

https://github.com/dotnet/runtime/pull/77799 adds new FrozenDictionary and FrozenSet types. These are in decent shape but not yet ready to ship. Several follow-ups are required:

andrewjsaid commented 10 months ago

Would tries be useful in some situations for strings? Should we consider doing reflection emit code gen in some circumstances?

After I saw that routing in ASP.NET Core uses reflection emitted Trie, I also spent some time attempting to implement an alternative to stringly-keyed ASCII FrozenDictionary in a similar way. After lots of attempts, I no longer think it's possible for Tree structures to be faster than Hash based data structures in general purpose use-cases. However the IL-Emit Trie did indeed come out faster than the FrozenDictionary in my small experiments.

https://github.com/andrewjsaid/trie

rampaa commented 4 months ago

It's something we'll consider for .NET 9.

Is it being considered for .NET 9?

stephentoub commented 4 months ago

It's something we'll consider for .NET 9.

Is it being considered for .NET 9?

Unlikely at this point.

sanghel-orbyta commented 3 months ago

Any chance of considering adding something like:

Perhaps via analogous methods such as those for .ToImmutableSortedDictionary/Set()

stephentoub commented 3 months ago

Any chance of considering adding something like:

  • FrozenSortedSet<T>
  • FrozenSortedDictionary<T>
  • IEnumerable<T>.ToFrozenSortedSet<T>
  • IEnumerable<T>.ToFrozenSortedDictionary<T> ?

Perhaps via analogous methods such as those for .ToImmutableSortedDictionary/Set()

What functionality are you hoping for here? Specifically just that the exposed Items and Key/Value arrays are sorted and that enumerating happens in that order as well?

sanghel-orbyta commented 3 months ago

@stephentoub exactly.

I have a ConcurrentDictionary that I accumulate into and then need to read from ordered by key, as quickly as possibile (using this for some UDP traffic re-ordering, the dictionary is a ConcurrentDictionary<long, (int packetOffset, int packetLength)>, that keeps track of offsets in a Memory<byte> buffer... linear, I know, but I'm not expert enough to know how to do this with a ring buffer with pooled memory alocations avoiding GC... any ideas on this?).

Right now I'm using .ToImmutableSortedDictionary(), but from what I've understood, a FrozenDictionary would be quicker to enumerate.

stephentoub commented 3 months ago

What functionality are you hoping for here? Specifically just that the exposed Items and Key/Value arrays are sorted and that enumerating happens in that order as well?

@stephentoub exactly.

Thanks. In that case, I don't think this warrants or necessitates new types. We could just have the creation of a FrozenDictionary/Set sort the arrays at construction, either using a type's built-in notion of ordering (via IComparable) which could be done without any API additions, or via an extra overload that accepts a Comparer to use for that purpose.

sanghel-orbyta commented 3 months ago

In that case, I don't think this warrants or necessitates new types. We could just have the creation of a FrozenDictionary/Set sort the arrays at construction

That would definitely work, thanks for the feedback @stephentoub .