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

Modify MergedNamespaceDeclaration.MakeChildren to to allocate less #74698

Merged
merged 2 commits into from
Aug 13, 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 @@ -4,6 +4,7 @@

#nullable disable

using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.PooledObjects;
Expand Down Expand Up @@ -139,43 +140,102 @@ private ImmutableArray<MergedNamespaceOrTypeDeclaration> MakeChildren()

var children = ArrayBuilder<MergedNamespaceOrTypeDeclaration>.GetInstance();

if (namespaces != null)
addNamespacesToChildren(namespaces, allNamespacesHaveSameName, children);
addTypesToChildren(types, allTypesHaveSameIdentity, children);

return children.ToImmutableAndFree();

static void addNamespacesToChildren(ArrayBuilder<SingleNamespaceDeclaration> namespaces, bool allNamespacesHaveSameName, ArrayBuilder<MergedNamespaceOrTypeDeclaration> children)
{
if (allNamespacesHaveSameName)
{
children.Add(MergedNamespaceDeclaration.Create(namespaces.ToImmutableAndFree()));
}
else
if (namespaces != null)
{
var namespaceGroups = namespaces.ToDictionary(n => n.Name, StringOrdinalComparer.Instance);
namespaces.Free();

foreach (var namespaceGroup in namespaceGroups.Values)
if (allNamespacesHaveSameName)
{
children.Add(MergedNamespaceDeclaration.Create(namespaces.ToImmutableAndFree()));
}
else
{
children.Add(MergedNamespaceDeclaration.Create(namespaceGroup));
// PERF: Don't use ArrayBuilder.ToDictionary directly as it requires an extra dictionary allocation. Other options such
// as MultiDictionary<string, SingleNamespaceDeclaration> and Dictionary<string, OneOrMany<SingleNamespaceDeclaration>>
// are even less appealing as they don't perform well when their value sets grow to contain a large number of items,
// as typically happens when processing the namespaces.
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
var namespaceGroups = new Dictionary<string, ArrayBuilder<SingleNamespaceDeclaration>>(StringOrdinalComparer.Instance);
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved

foreach (var n in namespaces)
{
var builder = namespaceGroups.GetOrAdd(n.Name, static () => ArrayBuilder<SingleNamespaceDeclaration>.GetInstance());

builder.Add(n);
}

namespaces.Free();

foreach (var (_, namespaceGroup) in namespaceGroups)
{
children.Add(MergedNamespaceDeclaration.Create(namespaceGroup.ToImmutableAndFree()));
}
}
}
}

if (types != null)
static void addTypesToChildren(ArrayBuilder<SingleTypeDeclaration> types, bool allTypesHaveSameIdentity, ArrayBuilder<MergedNamespaceOrTypeDeclaration> children)
{
if (allTypesHaveSameIdentity)
{
children.Add(new MergedTypeDeclaration(types.ToImmutableAndFree()));
}
else
if (types != null)
{
var typeGroups = types.ToDictionary(t => t.Identity);
types.Free();

foreach (var typeGroup in typeGroups.Values)
if (allTypesHaveSameIdentity)
{
children.Add(new MergedTypeDeclaration(types.ToImmutableAndFree()));
}
else
{
children.Add(new MergedTypeDeclaration(typeGroup));
// PERF: Use object as the value in this dictionary to efficiently represent single item collections.
// If only a single object has been seen with a given identity, the value will be a SingleTypeDeclaration,
// otherwise, the value will be an ArrayBuilder<SingleTypeDeclaration>. This code differs from
// addNamespacesToChildren intentionally as the vast majority of identities are represented by only a
// single item in the types collection.
var typeGroups = PooledDictionary<SingleTypeDeclaration.TypeDeclarationIdentity, object>.GetInstance();

foreach (var t in types)
{
var id = t.Identity;

if (typeGroups.TryGetValue(id, out var existingValue))
{
if (existingValue is not ArrayBuilder<SingleTypeDeclaration> builder)
{
builder = ArrayBuilder<SingleTypeDeclaration>.GetInstance();
builder.Add((SingleTypeDeclaration)existingValue);
typeGroups[id] = builder;
}

builder.Add(t);
}
else
{
typeGroups.Add(id, t);
}
}

foreach (var (_, typeGroup) in typeGroups)
{
if (typeGroup is SingleTypeDeclaration t)
{
children.Add(new MergedTypeDeclaration([t]));
}
else
{
var builder = (ArrayBuilder<SingleTypeDeclaration>)typeGroup;
children.Add(new MergedTypeDeclaration(builder.ToImmutableAndFree()));
}
}

types.Free();

// ArrayBuilder values were freed above.
typeGroups.Free();
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

return children.ToImmutableAndFree();
}

public new ImmutableArray<MergedNamespaceOrTypeDeclaration> Children
Expand Down
23 changes: 23 additions & 0 deletions src/Compilers/Core/Portable/Collections/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Collections;
Expand Down Expand Up @@ -36,6 +37,28 @@ public static TValue GetOrAdd<TKey, TValue>(
}
}

/// <summary>
/// If the given key is not found in the dictionary, add it with the result of invoking getValue and return the value.
/// Otherwise return the existing value associated with that key.
/// </summary>
public static TValue GetOrAdd<TKey, TValue>(
this Dictionary<TKey, TValue> dictionary,
TKey key,
Func<TValue> getValue)
where TKey : notnull
{
if (dictionary.TryGetValue(key, out var existingValue))
{
return existingValue;
}
else
{
var value = getValue();
dictionary.Add(key, value);
return value;
}
}

#if !NETCOREAPP
public static bool TryAdd<TKey, TValue>(
this Dictionary<TKey, TValue> dictionary,
Expand Down