Skip to content

Commit

Permalink
don't reset capacity for pooled object
Browse files Browse the repository at this point in the history
  • Loading branch information
genlu committed Oct 23, 2020
1 parent 91cae8a commit 8ae23d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,6 @@ private FilteredCompletionModel UpdateCompletionList(
var initialListOfItemsToBeIncluded = s_listOfMatchResultPool.Allocate();
try
{
if (initialListOfItemsToBeIncluded.Capacity < data.InitialSortedList.Length)
{
// We never reduce the capacity so the pooled list is more likely
// be able to handle any subsequent session w/o calling resize.
initialListOfItemsToBeIncluded.Capacity = data.InitialSortedList.Length;
}

// Filter items based on the selected filters and matching.
foreach (var item in data.InitialSortedList)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,7 @@ private CompletionList MergeAndPruneCompletionLists(
// changed it 'wins' and picks the span that will be used for all items in the completion
// list. If no contexts changed it, then just use the default span provided by the service.
var finalCompletionListSpan = completionContexts.FirstOrDefault(c => c.CompletionListSpan != defaultSpan)?.CompletionListSpan ?? defaultSpan;
var itemsCount = completionContexts.Sum(context => context.Items.Count);
using var displayNameToItemsMap = new DisplayNameToItemsMap(this, itemsCount);
using var displayNameToItemsMap = new DisplayNameToItemsMap(this);
CompletionItem suggestionModeItem = null;

foreach (var context in completionContexts)
Expand All @@ -458,7 +457,7 @@ private CompletionList MergeAndPruneCompletionLists(
}

// TODO(DustinCa): Revisit performance of this.
using var _ = ArrayBuilder<CompletionItem>.GetInstance(itemsCount, out var builder);
using var _ = ArrayBuilder<CompletionItem>.GetInstance(displayNameToItemsMap.Count, out var builder);
builder.AddRange(displayNameToItemsMap);
builder.Sort();

Expand Down Expand Up @@ -636,13 +635,12 @@ private static readonly ObjectPool<Dictionary<string, object>> s_uniqueSourcesPo
private readonly Dictionary<string, object> _displayNameToItemsMap;
private readonly CompletionServiceWithProviders _service;

public DisplayNameToItemsMap(CompletionServiceWithProviders service, int capacity)
public int Count { get; private set; }

public DisplayNameToItemsMap(CompletionServiceWithProviders service)
{
_service = service;
_displayNameToItemsMap = s_uniqueSourcesPool.Allocate();
#if NETCOREAPP
_displayNameToItemsMap.EnsureCapacity(capacity);
#endif
}

public void Dispose()
Expand All @@ -659,6 +657,7 @@ public void Add(CompletionItem item)

if (!_displayNameToItemsMap.TryGetValue(entireDisplayText, out var value))
{
Count++;
_displayNameToItemsMap.Add(entireDisplayText, item);
return;
}
Expand All @@ -673,6 +672,8 @@ public void Add(CompletionItem item)
return;
}

Count++;
// Matching items should be rare, no need to use object pool for this.
_displayNameToItemsMap[entireDisplayText] = new List<CompletionItem>() { sameNamedItem, item };
}
else if (value is List<CompletionItem> sameNamedItems)
Expand All @@ -687,6 +688,7 @@ public void Add(CompletionItem item)
}
}

Count++;
sameNamedItems.Add(item);
}
}
Expand Down

0 comments on commit 8ae23d3

Please sign in to comment.