Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a frozendictionary on net-core and a readonly one on netfx #72995

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand All @@ -19,16 +20,33 @@

namespace Microsoft.CodeAnalysis;

// On NetFx, frozen dictionary is very expensive when you give it a case insensitive comparer. This is due to
// unavoidable allocations it performs while doing its key-analysis that involve going through the non-span-aware
// culture types. So, on netfx, we use a plain ReadOnlyDictionary here.
#if NET
using FilePathToDocumentIds = FrozenDictionary<string, OneOrMany<DocumentId>>;
#else
using FilePathToDocumentIds = ReadOnlyDictionary<string, OneOrMany<DocumentId>>;
#endif

/// <summary>
/// Holds on a <see cref="DocumentId"/> to <see cref="TextDocumentState"/> map and an ordering.
/// </summary>
internal sealed class TextDocumentStates<TState>
where TState : TextDocumentState
{
#if NET
private static readonly ObjectPool<Dictionary<string, OneOrMany<DocumentId>>> s_filePathPool = new(() => new(SolutionState.FilePathComparer));
#endif

public static readonly TextDocumentStates<TState> Empty =
new([], ImmutableSortedDictionary.Create<DocumentId, TState>(DocumentIdComparer.Instance), FrozenDictionary<string, OneOrMany<DocumentId>>.Empty);
new([],
ImmutableSortedDictionary.Create<DocumentId, TState>(DocumentIdComparer.Instance),
#if NET
FilePathToDocumentIds.Empty);
#else
new(new Dictionary<string, OneOrMany<DocumentId>>()));
#endif

private readonly ImmutableList<DocumentId> _ids;

Expand All @@ -39,12 +57,12 @@ internal sealed class TextDocumentStates<TState>
/// </summary>
private readonly ImmutableSortedDictionary<DocumentId, TState> _map;

private FrozenDictionary<string, OneOrMany<DocumentId>>? _filePathToDocumentIds;
private FilePathToDocumentIds? _filePathToDocumentIds;

private TextDocumentStates(
ImmutableList<DocumentId> ids,
ImmutableSortedDictionary<DocumentId, TState> map,
FrozenDictionary<string, OneOrMany<DocumentId>>? filePathToDocumentIds)
FilePathToDocumentIds? filePathToDocumentIds)
{
Debug.Assert(map.KeyComparer == DocumentIdComparer.Instance);

Expand Down Expand Up @@ -342,10 +360,14 @@ public void AddDocumentIdsWithFilePath(ref TemporaryArray<DocumentId> temporaryA
: null;
}

private FrozenDictionary<string, OneOrMany<DocumentId>> ComputeFilePathToDocumentIds()
private FilePathToDocumentIds ComputeFilePathToDocumentIds()
{
#if NET
using var pooledDictionary = s_filePathPool.GetPooledObject();
var result = pooledDictionary.Object;
#else
var result = new Dictionary<string, OneOrMany<DocumentId>>(SolutionState.FilePathComparer);
#endif

foreach (var (documentId, state) in _map)
{
Expand All @@ -358,6 +380,10 @@ private FrozenDictionary<string, OneOrMany<DocumentId>> ComputeFilePathToDocumen
: OneOrMany.Create(documentId);
}

#if NET
return result.ToFrozenDictionary(SolutionState.FilePathComparer);
#else
return new(result);
#endif
}
}
Loading