O ConcurrentDictionary
código-fonte menciona a possibilidade de usar TAlternateKey
, que é uma estrutura de referência.
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs
Além disso, depois de alguma pesquisa, encontrei outros exemplos interessantes. Como aqui: https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs
#if NET9_0_OR_GREATER
private readonly ConcurrentDictionary<string, CacheEntry>.AlternateLookup<ReadOnlySpan<char>> _stringAltLookup;
public CoherentState()
{
_stringAltLookup = _stringEntries.GetAlternateLookup<ReadOnlySpan<char>>();
}
#endif
//...
#if NET9_0_OR_GREATER
internal bool TryGetValue(ReadOnlySpan<char> key, [NotNullWhen(true)] out CacheEntry? entry)
=> _stringAltLookup.TryGetValue(key, out entry);
#endif
#if NET9_0_OR_GREATER
private readonly ConcurrentDictionary<string, CacheEntry>.AlternateLookup<ReadOnlySpan<char>> _stringAltLookup;
public CoherentState()
{
_stringAltLookup = _stringEntries.GetAlternateLookup<ReadOnlySpan<char>>();
}
#endif
Devo usar ReadOnlySpan<char>
como chave em ConcurrentDictionary
vez de string
para melhor desempenho?