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

Experiment with array creation. (part2) #73005

Merged
merged 46 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
becb0c5
Simplify code
CyrusNajmabadi Apr 12, 2024
df92e11
no need to realize array
CyrusNajmabadi Apr 12, 2024
5875b5a
Switch builder type
CyrusNajmabadi Apr 12, 2024
608afd9
Switch builder type
CyrusNajmabadi Apr 12, 2024
338facc
Switch builder type
CyrusNajmabadi Apr 12, 2024
a2351ac
Switch builder type
CyrusNajmabadi Apr 12, 2024
bf88bf2
Switch builder type
CyrusNajmabadi Apr 12, 2024
23d2beb
Switch builder type
CyrusNajmabadi Apr 12, 2024
c4f733b
Switch builder type
CyrusNajmabadi Apr 12, 2024
c7c148a
Simplify
CyrusNajmabadi Apr 12, 2024
9db2644
Switch builder type
CyrusNajmabadi Apr 12, 2024
c8c58dd
Switch builder type
CyrusNajmabadi Apr 12, 2024
49a00d1
Switch builder type
CyrusNajmabadi Apr 12, 2024
a5bb571
Switch builder type
CyrusNajmabadi Apr 12, 2024
a45c91c
Switch builder type
CyrusNajmabadi Apr 12, 2024
4db8848
Switch builder type
CyrusNajmabadi Apr 12, 2024
56399c2
Switch builder type
CyrusNajmabadi Apr 12, 2024
2b62e27
Switch builder type
CyrusNajmabadi Apr 12, 2024
ee1e680
Switch builder type
CyrusNajmabadi Apr 12, 2024
34f78e3
Switch builder type
CyrusNajmabadi Apr 12, 2024
4edfaf4
Simplify
CyrusNajmabadi Apr 12, 2024
96b3322
Simplify
CyrusNajmabadi Apr 12, 2024
5958326
Switch builder type
CyrusNajmabadi Apr 12, 2024
761663d
Switch builder type
CyrusNajmabadi Apr 12, 2024
7e04145
Switch builder type
CyrusNajmabadi Apr 12, 2024
d4dbced
Switch builder type
CyrusNajmabadi Apr 12, 2024
e8ff1a6
Switch builder type
CyrusNajmabadi Apr 12, 2024
7709282
Switch builder type
CyrusNajmabadi Apr 12, 2024
c55b63e
Switch builder type
CyrusNajmabadi Apr 12, 2024
977bceb
Switch builder type
CyrusNajmabadi Apr 12, 2024
207ea49
Merge branch 'fixedArray' into fixedArray2
CyrusNajmabadi Apr 12, 2024
e0a62fd
Docs
CyrusNajmabadi Apr 12, 2024
85cbded
docs
CyrusNajmabadi Apr 12, 2024
1b76e1f
docs
CyrusNajmabadi Apr 12, 2024
e2e8fdb
Fix ambiguity
CyrusNajmabadi Apr 12, 2024
68ec52d
Add check before adding
CyrusNajmabadi Apr 12, 2024
b76289b
clarify
CyrusNajmabadi Apr 12, 2024
55a16c4
grammar
CyrusNajmabadi Apr 12, 2024
4b12ae8
grammar
CyrusNajmabadi Apr 12, 2024
818edfb
use array
CyrusNajmabadi Apr 12, 2024
f74c9f4
fix
CyrusNajmabadi Apr 12, 2024
43ec1cd
fix
CyrusNajmabadi Apr 12, 2024
b1e7f9b
optimize array addition
CyrusNajmabadi Apr 12, 2024
07b7123
Invert
CyrusNajmabadi Apr 12, 2024
626306d
Merge remote-tracking branch 'upstream/main' into fixedArray2
CyrusNajmabadi Apr 12, 2024
a9536ff
fix
CyrusNajmabadi Apr 13, 2024
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 @@ -70,14 +70,7 @@ protected override async Task FixAllAsync(
await editor.ApplyExpressionLevelSemanticEditsAsync(
document,
originalNodes,
t =>
{
using var _ = ArrayBuilder<SyntaxNode>.GetInstance(capacity: 2, out var additionalNodesToTrack);
additionalNodesToTrack.Add(t.identifier);
additionalNodesToTrack.Add(t.declarator);

return (t.invocationOrCreation, additionalNodesToTrack.ToImmutable());
},
static t => (t.invocationOrCreation, ImmutableArray.Create<SyntaxNode>(t.identifier, t.declarator)),
Copy link
Member Author

Choose a reason for hiding this comment

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

in some cases, i just literally fixed things up to avoid any builders whatsoever

(_, _, _) => true,
(semanticModel, currentRoot, t, currentNode)
=> ReplaceIdentifierWithInlineDeclaration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private static ExpressionSyntax GenerateTupleDeclaration(ITypeSymbol typeSymbol,

return TupleExpression(
OpenParenToken.WithTrailingTrivia(),
SeparatedList(builder.ToImmutable(), separatorBuilder.ToImmutableAndFree()),
SeparatedList(builder, separatorBuilder),
Copy link
Member Author

Choose a reason for hiding this comment

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

this guy doesn't need to realize the immutable arrays. so this can stay nicely as ArrayBuilders.

CloseParenToken)
.WithTrailingTrivia(parensDesignation.GetTrailingTrivia());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

Expand Down Expand Up @@ -244,7 +245,7 @@ private void RegisterFixForMethodOverloads(

ImmutableArray<CodeAction> NestByOverload()
{
using var _ = ArrayBuilder<CodeAction>.GetInstance(codeFixData.Length, out var builder);
var builder = new FixedSizeArrayBuilder<CodeAction>(codeFixData.Length);
foreach (var data in codeFixData)
Copy link
Member Author

Choose a reason for hiding this comment

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

note: all loops were audited to make sure they always add an item for each input iterated over. loops that did not do this were not updated.

{
// We create the mandatory data.CreateChangedSolutionNonCascading fix first.
Expand Down Expand Up @@ -276,12 +277,12 @@ ImmutableArray<CodeAction> NestByOverload()
builder.Add(codeAction);
}

return builder.ToImmutableAndClear();
return builder.MoveToImmutable();
}

ImmutableArray<CodeAction> NestByCascading()
{
using var _ = ArrayBuilder<CodeAction>.GetInstance(capacity: 2, out var builder);
using var builder = TemporaryArray<CodeAction>.Empty;
Copy link
Member Author

Choose a reason for hiding this comment

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

tiny array cases could just use temporaryarray. this guy is then cancellation-safe. teh values are placed on the stack, and copied at the end. since this is only for <4 element cases, this is basically free.


var nonCascadingActions = codeFixData.SelectAsArray(data =>
{
Expand Down Expand Up @@ -312,7 +313,7 @@ ImmutableArray<CodeAction> NestByCascading()
builder.Add(CodeAction.Create(nestedCascadingTitle, cascadingActions, isInlinable: false));
}

return builder.ToImmutable();
return builder.ToImmutableAndClear();
}
}

Expand All @@ -321,7 +322,7 @@ private ImmutableArray<CodeFixData> PrepareCreationOfCodeActions(
SeparatedSyntaxList<TArgumentSyntax> arguments,
ImmutableArray<ArgumentInsertPositionData<TArgumentSyntax>> methodsAndArgumentsToAdd)
{
using var _ = ArrayBuilder<CodeFixData>.GetInstance(methodsAndArgumentsToAdd.Length, out var builder);
var builder = new FixedSizeArrayBuilder<CodeFixData>(methodsAndArgumentsToAdd.Length);

// Order by the furthest argument index to the nearest argument index. The ones with
// larger argument indexes mean that we matched more earlier arguments (and thus are
Expand All @@ -343,7 +344,7 @@ private ImmutableArray<CodeFixData> PrepareCreationOfCodeActions(
builder.Add(codeFixData);
}

return builder.ToImmutable();
return builder.MoveToImmutable();
}

private static string GetCodeFixTitle(string resourceString, IMethodSymbol methodToUpdate, bool includeParameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ public override async Task<CodeAction> TryGetMergedFixAsync(

private static async Task<ImmutableArray<SyntaxNode>> GetAttributeNodesToFixAsync(ImmutableArray<AttributeRemoveAction> attributeRemoveFixes, CancellationToken cancellationToken)
{
using var _ = ArrayBuilder<SyntaxNode>.GetInstance(attributeRemoveFixes.Length, out var builder);
var builder = new FixedSizeArrayBuilder<SyntaxNode>(attributeRemoveFixes.Length);
foreach (var attributeRemoveFix in attributeRemoveFixes)
{
var attributeToRemove = await attributeRemoveFix.GetAttributeToRemoveAsync(cancellationToken).ConfigureAwait(false);
builder.Add(attributeToRemove);
}

return builder.ToImmutableAndClear();
return builder.MoveToImmutable();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ private static ImmutableArray<string> GetAllNamespaceImportsForDeclaringDocument
private static ImmutableArray<SyntaxNode> CreateImports(Document document, ImmutableArray<string> names, bool withFormatterAnnotation)
{
var generator = SyntaxGenerator.GetGenerator(document);
using var _ = ArrayBuilder<SyntaxNode>.GetInstance(names.Length, out var builder);
var builder = new FixedSizeArrayBuilder<SyntaxNode>(names.Length);
for (var i = 0; i < names.Length; ++i)
builder.Add(CreateImport(generator, names[i], withFormatterAnnotation));

return builder.ToImmutableAndClear();
return builder.MoveToImmutable();
}

private static SyntaxNode CreateImport(SyntaxGenerator syntaxGenerator, string name, bool withFormatterAnnotation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Tags;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Completion.Providers;

Expand Down Expand Up @@ -97,7 +98,7 @@ public static CompletionItem CreateAttributeItemWithoutSuffix(CompletionItem att
var attributeItems = attributeItem.GetProperties();

// Remember the full type name so we can get the symbol when description is displayed.
using var _ = ArrayBuilder<KeyValuePair<string, string>>.GetInstance(attributeItems.Length + 1, out var builder);
var builder = new FixedSizeArrayBuilder<KeyValuePair<string, string>>(attributeItems.Length + 1);
builder.AddRange(attributeItems);
builder.Add(new KeyValuePair<string, string>(AttributeFullName, attributeItem.DisplayText));

Expand All @@ -107,7 +108,7 @@ public static CompletionItem CreateAttributeItemWithoutSuffix(CompletionItem att
var item = CompletionItem.CreateInternal(
displayText: attributeNameWithoutSuffix,
sortText: sortTextBuilder.ToStringAndFree(),
properties: builder.ToImmutable(),
properties: builder.MoveToImmutable(),
tags: attributeItem.Tags,
rules: attributeItem.Rules,
displayTextPrefix: attributeItem.DisplayTextPrefix,
Expand Down Expand Up @@ -219,12 +220,8 @@ private static (ISymbol? symbol, int overloadCount) GetSymbolAndOverloadCount(Co
public static CompletionItem MarkItemToAlwaysFullyQualify(CompletionItem item)
{
var itemProperties = item.GetProperties();

using var _ = ArrayBuilder<KeyValuePair<string, string>>.GetInstance(itemProperties.Length + 1, out var builder);
builder.AddRange(itemProperties);
builder.Add(new KeyValuePair<string, string>(AlwaysFullyQualifyKey, AlwaysFullyQualifyKey));

return item.WithProperties(builder.ToImmutable());
ImmutableArray<KeyValuePair<string, string>> properties = [.. itemProperties, KeyValuePairUtil.Create(AlwaysFullyQualifyKey, AlwaysFullyQualifyKey)];
return item.WithProperties(properties);
}

public static bool ShouldAlwaysFullyQualify(CompletionItem item) => item.TryGetProperty(AlwaysFullyQualifyKey, out var _);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,11 @@ private static async Task<ImmutableArray<DocumentHighlights>> CreateSpansAsync(
await AddLocationSpanAsync(location, solution, spanSet, tagMap, HighlightSpanKind.Reference, cancellationToken).ConfigureAwait(false);
}

using var _1 = ArrayBuilder<DocumentHighlights>.GetInstance(tagMap.Count, out var list);
var list = new FixedSizeArrayBuilder<DocumentHighlights>(tagMap.Count);
foreach (var kvp in tagMap)
{
list.Add(new DocumentHighlights(kvp.Key, [.. kvp.Value]));
}

return list.ToImmutableAndClear();
return list.MoveToImmutable();
}

private static bool ShouldIncludeDefinition(ISymbol symbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,10 @@ private ImmutableArray<VariableInfo> MarkVariableInfoToUseAsReturnValueIfPossibl

private static ImmutableArray<VariableInfo> GetMethodParameters(Dictionary<ISymbol, VariableInfo> variableInfoMap)
{
using var _ = ArrayBuilder<VariableInfo>.GetInstance(variableInfoMap.Count, out var list);
var list = new FixedSizeArrayBuilder<VariableInfo>(variableInfoMap.Count);
list.AddRange(variableInfoMap.Values);

list.Sort();
return list.ToImmutable();
return list.MoveToImmutable();
}

/// <param name="bestEffort">When false, variables whose data flow is not understood
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public static ImmutableArray<ISymbol> GetDelegatableMembers(

var parameters = GetNonCapturedPrimaryConstructorParameters(fields, properties);

using var _1 = ArrayBuilder<ISymbol>.GetInstance(fields.Length + properties.Length + parameters.Length, out var result);
result.AddRange(fields);
result.AddRange(properties);
result.AddRange(parameters);
return result.ToImmutableAndClear();
return [.. fields, .. properties, .. parameters];
Copy link
Member Author

Choose a reason for hiding this comment

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

much nicer :)


ImmutableArray<IParameterSymbol> GetNonCapturedPrimaryConstructorParameters(
ImmutableArray<IFieldSymbol> fields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ private static BlockStructure GetBlockStructure(

private static BlockStructure CreateBlockStructure(in BlockStructureContext context)
{
using var _ = ArrayBuilder<BlockSpan>.GetInstance(context.Spans.Count, out var updatedSpans);
var updatedSpans = new FixedSizeArrayBuilder<BlockSpan>(context.Spans.Count);
foreach (var span in context.Spans)
updatedSpans.Add(UpdateBlockSpan(span, context.Options));

return new BlockStructure(updatedSpans.ToImmutableAndClear());
return new BlockStructure(updatedSpans.MoveToImmutable());
}

private static BlockSpan UpdateBlockSpan(BlockSpan blockSpan, in BlockStructureOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ pieces[index] is var piece &&
private static ImmutableArray<SyntaxNodeOrToken> GetSubRange(
ArrayBuilder<SyntaxNodeOrToken> pieces, int start, int end)
{
using var _ = ArrayBuilder<SyntaxNodeOrToken>.GetInstance(end - start, out var result);
var result = new FixedSizeArrayBuilder<SyntaxNodeOrToken>(end - start);
for (var i = start; i < end; i++)
result.Add(pieces[i]);

return result.ToImmutableAndClear();
return result.MoveToImmutable();
}

private bool IsDecomposableChainPart(SyntaxNode? node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,14 @@ private static async Task<ImmutableArray<DiagnosticData>> RemapDiagnosticLocatio
}

// Round tripping the diagnostics should ensure they get correctly remapped.
using var _ = ArrayBuilder<DiagnosticData>.GetInstance(diagnostics.Length, out var builder);
var builder = new FixedSizeArrayBuilder<DiagnosticData>(diagnostics.Length);
foreach (var diagnosticData in diagnostics)
{
var diagnostic = await diagnosticData.ToDiagnosticAsync(textDocument.Project, cancellationToken).ConfigureAwait(false);
builder.Add(DiagnosticData.Create(diagnostic, textDocument));
}

return builder.ToImmutableAndClear();
return builder.MoveToImmutable();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ async Task<IUnifiedSuggestedAction> GetUnifiedSuggestedActionAsync(Solution orig
{
if (action.NestedActions.Length > 0)
{
using var _ = ArrayBuilder<IUnifiedSuggestedAction>.GetInstance(action.NestedActions.Length, out var unifiedNestedActions);
var unifiedNestedActions = new FixedSizeArrayBuilder<IUnifiedSuggestedAction>(action.NestedActions.Length);
foreach (var nestedAction in action.NestedActions)
{
var unifiedNestedAction = await GetUnifiedSuggestedActionAsync(originalSolution, nestedAction, fix).ConfigureAwait(false);
Expand All @@ -158,7 +158,7 @@ async Task<IUnifiedSuggestedAction> GetUnifiedSuggestedActionAsync(Solution orig
var set = new UnifiedSuggestedActionSet(
originalSolution,
categoryName: null,
actions: unifiedNestedActions.ToImmutableAndClear(),
actions: unifiedNestedActions.MoveToImmutable(),
title: null,
priority: action.Priority,
applicableToSpan: fix.PrimaryDiagnostic.Location.SourceSpan);
Expand Down Expand Up @@ -454,14 +454,14 @@ public static async Task<ImmutableArray<UnifiedSuggestedActionSet>> GetFilterAnd

var filteredRefactorings = FilterOnAnyThread(refactorings, selection, filterOutsideSelection);

using var _ = ArrayBuilder<UnifiedSuggestedActionSet>.GetInstance(filteredRefactorings.Length, out var orderedRefactorings);
var orderedRefactorings = new FixedSizeArrayBuilder<UnifiedSuggestedActionSet>(filteredRefactorings.Length);
foreach (var refactoring in filteredRefactorings)
{
var orderedRefactoring = await OrganizeRefactoringsAsync(workspace, document, selection, refactoring, cancellationToken).ConfigureAwait(false);
orderedRefactorings.Add(orderedRefactoring);
}

return orderedRefactorings.ToImmutableAndClear();
return orderedRefactorings.MoveToImmutable();
}

private static ImmutableArray<CodeRefactoring> FilterOnAnyThread(
Expand Down Expand Up @@ -546,7 +546,7 @@ async Task<IUnifiedSuggestedAction> GetUnifiedSuggestedActionSetAsync(CodeAction
{
if (codeAction.NestedActions.Length > 0)
{
using var _1 = ArrayBuilder<IUnifiedSuggestedAction>.GetInstance(codeAction.NestedActions.Length, out var nestedActions);
var nestedActions = new FixedSizeArrayBuilder<IUnifiedSuggestedAction>(codeAction.NestedActions.Length);
foreach (var nestedAction in codeAction.NestedActions)
{
var unifiedAction = await GetUnifiedSuggestedActionSetAsync(nestedAction, applicableToSpan, selection, cancellationToken).ConfigureAwait(false);
Expand All @@ -556,7 +556,7 @@ async Task<IUnifiedSuggestedAction> GetUnifiedSuggestedActionSetAsync(CodeAction
var set = new UnifiedSuggestedActionSet(
originalSolution,
categoryName: null,
actions: nestedActions.ToImmutableAndClear(),
actions: nestedActions.MoveToImmutable(),
title: null,
priority: codeAction.Priority,
applicableToSpan: applicableToSpan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(InlayHintParams request)
var options = _optionsService.GetInlineHintsOptions(document.Project.Language);
var hints = await inlineHintService.GetInlineHintsAsync(document, textSpan, options, displayAllOverride: false, cancellationToken).ConfigureAwait(false);

using var _ = ArrayBuilder<LSP.InlayHint>.GetInstance(hints.Length, out var inlayHints);
var syntaxVersion = await document.GetSyntaxVersionAsync(cancellationToken).ConfigureAwait(false);
var inlayHintCache = context.GetRequiredLspService<InlayHintCache>();

// Store the members in the resolve cache so that when we get a resolve request for a particular
// member we can re-use the inline hint.
var resultId = inlayHintCache.UpdateCache(new InlayHintCache.InlayHintCacheEntry(hints, syntaxVersion));

var inlayHints = new LSP.InlayHint[hints.Length];
for (var i = 0; i < hints.Length; i++)
{
var hint = hints[i];
Expand Down Expand Up @@ -88,10 +88,10 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(InlayHintParams request)
Data = new InlayHintResolveData(resultId, i, request.TextDocument)
};

inlayHints.Add(inlayHint);
inlayHints[i] = inlayHint;
}

return inlayHints.ToArray();
return inlayHints;
}

/// <summary>
Expand Down
Loading
Loading