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

Remove most usage of "new ArrayBuilder" #74689

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
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 @@ -131,7 +131,7 @@ private static CompletionFilter CreateCompletionFilter(

public (ImmutableArray<CompletionFilter> filters, int data) GetFiltersAndAddToSet(RoslynCompletionItem item)
{
var listBuilder = new ArrayBuilder<CompletionFilter>();
using var _ = ArrayBuilder<CompletionFilter>.GetInstance(out var listBuilder);
var vectorForSingleItem = new BitVector32();

if (item.Flags.IsExpanded())
Expand All @@ -150,7 +150,7 @@ private static CompletionFilter CreateCompletionFilter(
}
}

return (listBuilder.ToImmutableAndFree(), vectorForSingleItem.Data);
return (listBuilder.ToImmutableAndClear(), vectorForSingleItem.Data);
}

// test only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1390,9 +1390,12 @@ static IEnumerable<int> M()
private static string[] GetLocalNames(EvaluationContext context)
{
string unused;
var locals = new ArrayBuilder<LocalAndMethod>();
var locals = ArrayBuilder<LocalAndMethod>.GetInstance();
context.CompileGetLocals(locals, argumentsOnly: false, typeName: out unused, testData: null);
return locals.Select(l => l.LocalName).ToArray();
var result = locals.Select(l => l.LocalName).ToArray();

locals.Free();
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3863,7 +3863,7 @@ static void DummySequencePoint()
EvaluationContext context;
context = CreateMethodContext(runtime, "C.<F>d__0.MoveNext", atLineNumber: 500);
string unused;
var locals = new ArrayBuilder<LocalAndMethod>();
var locals = ArrayBuilder<LocalAndMethod>.GetInstance();
context.CompileGetLocals(locals, argumentsOnly: true, typeName: out unused, testData: null);
var names = locals.Select(l => l.LocalName).ToArray();
// The order must confirm the order of the arguments in the method signature.
Expand All @@ -3877,7 +3877,7 @@ static void DummySequencePoint()
EvaluationContext context;
context = CreateMethodContext(runtime, "C.<F>d__0.MoveNext", atLineNumber: 500);
string unused;
var locals = new ArrayBuilder<LocalAndMethod>();
var locals = ArrayBuilder<LocalAndMethod>.GetInstance();
context.CompileGetLocals(locals, argumentsOnly: true, typeName: out unused, testData: null);
var names = locals.Select(l => l.LocalName).ToArray();
// The problem is not fixed in versions before 4.5: the order of arguments can be wrong.
Expand Down Expand Up @@ -3913,7 +3913,7 @@ static void DummySequencePoint()
EvaluationContext context;
context = CreateMethodContext(runtime, "C.<F>d__0.MoveNext", atLineNumber: 500);
string unused;
var locals = new ArrayBuilder<LocalAndMethod>();
var locals = ArrayBuilder<LocalAndMethod>.GetInstance();
context.CompileGetLocals(locals, argumentsOnly: true, typeName: out unused, testData: null);
var names = locals.Select(l => l.LocalName).ToArray();
// The order must confirm the order of the arguments in the method signature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal bool Equals(InstanceAndMethod other)
internal Dispatcher(ImmutableArray<TInterface> items)
{
_implementations = items;
_calls = new ArrayBuilder<InstanceAndMethod>();
_calls = ArrayBuilder<InstanceAndMethod>.GetInstance();
}

internal TResult Invoke<TResult>(object instance, MethodId method, Func<TInterface, TResult> f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ private static bool TryGetFindMethod(Assembly queryAssembly, [NotNullWhen(true)]
{
try
{
var candidates = new ArrayBuilder<MethodInfo>();
using var _ = ArrayBuilder<MethodInfo>.GetInstance(out var candidates);

foreach (var candidate in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public ActiveStatementsDescription(string oldMarkedSource, string newMarkedSourc

var activeStatementCount = Math.Max(OldStatements.Length, (newActiveStatementMarkers.Length == 0) ? -1 : newActiveStatementMarkers.Max(m => m.Id));

var newMappedSpans = new ArrayBuilder<SourceFileSpan>();
var newMappedRegions = new ArrayBuilder<ImmutableArray<SourceFileSpan>>();
using var _1 = ArrayBuilder<SourceFileSpan>.GetInstance(out var newMappedSpans);
using var _2 = ArrayBuilder<ImmutableArray<SourceFileSpan>>.GetInstance(out var newMappedRegions);
var newExceptionRegionMarkers = SourceMarkers.GetExceptionRegions(newMarkedSource);

newMappedSpans.ZeroInit(activeStatementCount);
Expand Down Expand Up @@ -137,7 +137,7 @@ internal static ImmutableArray<UnmappedActiveStatement> GetUnmappedActiveStateme
{
var map = new Dictionary<string, List<ActiveStatement>>();

var activeStatements = new ArrayBuilder<UnmappedActiveStatement>();
using var _ = ArrayBuilder<UnmappedActiveStatement>.GetInstance(out var activeStatements);

var sourceIndex = 0;
foreach (var markedSource in markedSources)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private async Task RefreshOptionsAsync(CancellationToken cancellationToken)
RoslynDebug.Assert(configurationsFromClient.Length == SupportedOptions.Sum(option => option is IPerLanguageValuedOption ? 2 : 1));

// LSP ensures the order of result from client should match the order we sent from server.
var optionsToUpdate = new ArrayBuilder<KeyValuePair<OptionKey2, object?>>();
using var _ = ArrayBuilder<KeyValuePair<OptionKey2, object?>>.GetInstance(out var optionsToUpdate);

for (var i = 0; i < configurationsFromClient.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ internal abstract class AbstractFormatDocumentHandlerBase<RequestType, ResponseT
var services = document.Project.Solution.Services;
var textChanges = Formatter.GetFormattedTextChanges(root, [formattingSpan], services, formattingOptions, rules: default, cancellationToken);

var edits = new ArrayBuilder<LSP.TextEdit>();
using var _ = ArrayBuilder<LSP.TextEdit>.GetInstance(out var edits);
edits.AddRange(textChanges.Select(change => ProtocolConversions.TextChangeToTextEdit(change, text)));
return edits.ToArrayAndFree();
return edits.ToArray();
}

public abstract LSP.TextDocumentIdentifier GetTextDocumentIdentifier(RequestType request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public FormatDocumentOnTypeHandler(IGlobalOptionService globalOptions)
return [];
}

var edits = new ArrayBuilder<TextEdit>();
using var _ = ArrayBuilder<TextEdit>.GetInstance(out var edits);
edits.AddRange(textChanges.Select(change => ProtocolConversions.TextChangeToTextEdit(change, documentSyntax.Text)));
return edits.ToArrayAndFree();
return edits.ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ private static string GetSignatureText(SignatureHelpItem item)

return sb.ToString();
}

private static ClassifiedTextElement GetSignatureClassifiedText(SignatureHelpItem item)
{
var taggedTexts = new ArrayBuilder<TaggedText>();
using var _ = ArrayBuilder<TaggedText>.GetInstance(out var taggedTexts);

taggedTexts.AddRange(item.PrefixDisplayParts);

Expand All @@ -160,7 +161,7 @@ private static ClassifiedTextElement GetSignatureClassifiedText(SignatureHelpIte
taggedTexts.AddRange(item.SuffixDisplayParts);
taggedTexts.AddRange(item.DescriptionParts);

return new ClassifiedTextElement(taggedTexts.ToArrayAndFree().Select(part => new ClassifiedTextRun(part.Tag.ToClassificationTypeName(), part.Text)));
return new ClassifiedTextElement(taggedTexts.ToArray().Select(part => new ClassifiedTextRun(part.Tag.ToClassificationTypeName(), part.Text)));
}
}
}
4 changes: 3 additions & 1 deletion src/Scripting/CSharpTest/ObjectFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ public void DebuggerProxy_DiagnosticBag()
[Fact]
public void DebuggerProxy_ArrayBuilder()
{
var obj = new ArrayBuilder<int>();
var obj = ArrayBuilder<int>.GetInstance();
obj.AddRange(new[] { 1, 2, 3, 4, 5 });

var str = s_formatter.FormatObject(obj, SingleLineOptions);
Expand All @@ -875,6 +875,8 @@ public void DebuggerProxy_ArrayBuilder()
"4",
"5"
);

obj.Free();
}

[Fact, WorkItem(8542, "https://github.com/dotnet/roslyn/issues/8452")]
Expand Down
9 changes: 5 additions & 4 deletions src/VisualStudio/LiveShare/Impl/ProjectsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ internal class ProjectsHandler : ILspRequestHandler<object, object[], Solution>
{
public async Task<object[]> HandleAsync(object param, RequestContext<Solution> requestContext, CancellationToken cancellationToken)
{
var projects = new ArrayBuilder<CustomProtocol.Project>();
using var _1 = ArrayBuilder<CustomProtocol.Project>.GetInstance(out var projects);
using var _2 = ArrayBuilder<Uri>.GetInstance(out var externalUris);
var solution = requestContext.Context;
foreach (var project in solution.Projects)
{
var externalUris = new ArrayBuilder<Uri>();
foreach (var sourceFile in project.Documents)
{
var uri = new Uri(sourceFile.FilePath);
Expand All @@ -37,7 +37,7 @@ public async Task<object[]> HandleAsync(object param, RequestContext<Solution> r
}
}
#pragma warning disable 0612
await requestContext.ProtocolConverter.RegisterExternalFilesAsync(externalUris.ToArrayAndFree()).ConfigureAwait(false);
await requestContext.ProtocolConverter.RegisterExternalFilesAsync(externalUris.ToArray()).ConfigureAwait(false);
#pragma warning restore 0612

var lspProject = new CustomProtocol.Project
Expand All @@ -48,9 +48,10 @@ public async Task<object[]> HandleAsync(object param, RequestContext<Solution> r
};

projects.Add(lspProject);
externalUris.Clear();
}

return projects.ToArrayAndFree();
return projects.ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public DataModel()
where !field.IsSpecialName
select field;

var builder = new ArrayBuilder<ActivityLevel?>();
using var _ = ArrayBuilder<ActivityLevel?>.GetInstance(out var builder);

var features = new Dictionary<string, ActivityLevel>();
var root = new ActivityLevel("All");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public FormatDocumentOnTypeHandler()

public async Task<TextEdit[]> HandleRequestAsync(DocumentOnTypeFormattingParams request, RequestContext context, CancellationToken cancellationToken)
{
var edits = new ArrayBuilder<TextEdit>();
if (string.IsNullOrEmpty(request.Character))
{
return edits.ToArrayAndFree();
return [];
}

using var _ = ArrayBuilder<TextEdit>.GetInstance(out var edits);
var document = context.Document;
var formattingService = document?.Project.Services.GetService<IXamlFormattingService>();
if (document != null && formattingService != null)
Expand All @@ -53,7 +53,7 @@ public async Task<TextEdit[]> HandleRequestAsync(DocumentOnTypeFormattingParams
}
}

return edits.ToArrayAndFree();
return edits.ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public bool SetGlobalOptions(ImmutableArray<KeyValuePair<OptionKey2, object?>> o

private bool SetGlobalOptions(OneOrMany<KeyValuePair<OptionKey2, object?>> options)
{
var changedOptions = new ArrayBuilder<(OptionKey2, object?)>(options.Count);
using var _ = ArrayBuilder<(OptionKey2, object?)>.GetInstance(options.Count, out var changedOptions);
var persisters = GetOptionPersisters();

lock (_gate)
Expand Down
4 changes: 2 additions & 2 deletions src/Workspaces/CoreTest/UtilityTest/NameGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ public void EnsureUniquenessInPlaceCanUseNotIncludingFirst10(string[] names, str

private static void VerifyEnsureUniquenessInPlace(string[] names, bool[]? isFixed, Func<string, bool>? canUse, bool isCaseSensitive, string[] expectedResult)
{
var namesBuilder = new ArrayBuilder<string>();
using var _1 = ArrayBuilder<string>.GetInstance(out var namesBuilder);
namesBuilder.AddRange(names);

var isFixedBuilder = new ArrayBuilder<bool>();
using var _2 = ArrayBuilder<bool>.GetInstance(out var isFixedBuilder);
isFixedBuilder.AddRange(isFixed ?? Enumerable.Repeat(false, names.Length));

NameGenerator.EnsureUniquenessInPlace(namesBuilder, isFixedBuilder, canUse, isCaseSensitive);
Expand Down
Loading