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

Cache the compilation states in compilation order to avoid recalculation #76380

Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -397,6 +397,22 @@ public static ImmutableArray<TResult> SelectAsArray<TSource, TResult>(this IRead
return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

public static ImmutableArray<TResult> SelectAsArray<TSource, TResult, TArg>(this IReadOnlyCollection<TSource>? source, Func<TSource, TArg, TResult> selector, TArg arg)
{
if (source == null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (source == null)

Didn't realize when I made this change this was under compilers. I can move elsewhere if the compiler folks don't want this override (but it does map closely to some other overloads in this file)

return ImmutableArray<TResult>.Empty;

var builder = new TResult[source.Count];
var index = 0;
foreach (var item in source)
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved
{
builder[index] = selector(item, arg);
index++;
}

return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

public static ImmutableArray<TResult> SelectManyAsArray<TSource, TResult>(this IEnumerable<TSource>? source, Func<TSource, IEnumerable<TResult>> selector)
{
if (source == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ internal sealed class TextDocumentStates<TState>
#endif

private readonly ImmutableList<DocumentId> _ids;
private ImmutableArray<TState> _statesInCompilationOrder;
private FilePathToDocumentIds? _filePathToDocumentIds;

private TextDocumentStates(
Expand All @@ -61,6 +62,7 @@ private TextDocumentStates(
_ids = ids;
States = map;
_filePathToDocumentIds = filePathToDocumentIds;
_statesInCompilationOrder = ImmutableArray<TState>.Empty;
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved
}

public TextDocumentStates(IEnumerable<TState> states)
Expand Down Expand Up @@ -117,10 +119,15 @@ public TState GetRequiredState(DocumentId documentId)
/// Get states ordered in compilation order.
/// </summary>
/// <returns></returns>
public IEnumerable<TState> GetStatesInCompilationOrder()
public ImmutableArray<TState> GetStatesInCompilationOrder()
{
var map = States;
return Ids.Select(id => map[id]);
if (_statesInCompilationOrder.IsEmpty)
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved
{
var map = States;
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved
_statesInCompilationOrder = Ids.SelectAsArray(static (id, map) => map[id], map);
}

return _statesInCompilationOrder;
}

public ImmutableArray<TValue> SelectAsArray<TValue>(Func<TState, TValue> selector)
Expand Down