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

Move ArrayBuilder.GetInstance helpers to Microsoft.CodeAnalysis.PooledObjects package #76971

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -9,6 +9,9 @@
<GenerateMicrosoftCodeAnalysisCommitHashAttribute>true</GenerateMicrosoftCodeAnalysisCommitHashAttribute>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>

<!-- Do not expose PooledDisposer APIs to core compiler libraries as widespread implicit try-finally blocks might negatively affect performance. -->
<DefineConstants>$(DefineConstants);MICROSOFT_CODEANALYSIS_POOLEDOBJECTS_NO_POOLED_DISPOSER</DefineConstants>

<!-- NuGet -->
<IsPackable>true</IsPackable>
<PackageDescription>
Expand Down
4 changes: 4 additions & 0 deletions src/Compilers/Core/Portable/Microsoft.CodeAnalysis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetRoslynSourceBuild);netstandard2.0</TargetFrameworks>
<DefineConstants>$(DefineConstants);COMPILERCORE</DefineConstants>

<!-- Do not expose PooledDisposer APIs to core compiler libraries as widespread implicit try-finally blocks might negatively affect performance. -->
<DefineConstants>$(DefineConstants);MICROSOFT_CODEANALYSIS_POOLEDOBJECTS_NO_POOLED_DISPOSER</DefineConstants>

<NoStdLib>true</NoStdLib>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>
<GeneratePerformanceSensitiveAttribute>true</GeneratePerformanceSensitiveAttribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<RootNamespace></RootNamespace>
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization>

<!-- Do not expose PooledDisposer APIs to core compiler libraries as widespread implicit try-finally blocks might negatively affect performance. -->
<DefineConstants>$(DefineConstants);MICROSOFT_CODEANALYSIS_POOLEDOBJECTS_NO_POOLED_DISPOSER</DefineConstants>

<!-- NuGet -->
<IsPackable>true</IsPackable>
<PackageDescription>
Expand Down
22 changes: 21 additions & 1 deletion src/Dependencies/PooledObjects/ArrayBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.CodeAnalysis.PooledObjects
{
[DebuggerDisplay("Count = {Count,nq}")]
[DebuggerTypeProxy(typeof(ArrayBuilder<>.DebuggerProxy))]
internal sealed partial class ArrayBuilder<T> : IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection<T>
internal sealed partial class ArrayBuilder<T> : IReadOnlyCollection<T>, IReadOnlyList<T>, ICollection<T>, IPooled
{
/// <summary>
/// See <see cref="Free()"/> for an explanation of this constant value.
Expand Down Expand Up @@ -720,5 +720,25 @@ public ImmutableArray<S> SelectDistinct<S>(Func<T, S> selector)
set.Free();
return result.ToImmutableAndFree();
}

#if !MICROSOFT_CODEANALYSIS_POOLEDOBJECTS_NO_POOLED_DISPOSER
public static PooledDisposer<ArrayBuilder<T>> GetInstance(out ArrayBuilder<T> instance)
{
instance = GetInstance();
return new PooledDisposer<ArrayBuilder<T>>(instance);
}

public static PooledDisposer<ArrayBuilder<T>> GetInstance(int capacity, out ArrayBuilder<T> instance)
{
instance = GetInstance(capacity);
return new PooledDisposer<ArrayBuilder<T>>(instance);
}

public static PooledDisposer<ArrayBuilder<T>> GetInstance(int capacity, T fillWithValue, out ArrayBuilder<T> instance)
{
instance = GetInstance(capacity, fillWithValue);
return new PooledDisposer<ArrayBuilder<T>>(instance);
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ArrayBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ArrayBuilder.Enumerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IPooled.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObjectPool`1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PooledDelegates.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PooledDictionary.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PooledDisposer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PooledHashSet.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PooledStringBuilder.cs" />
</ItemGroup>
Expand Down
16 changes: 16 additions & 0 deletions src/Dependencies/PooledObjects/PooledDisposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if !MICROSOFT_CODEANALYSIS_POOLEDOBJECTS_NO_POOLED_DISPOSER
using System;

namespace Microsoft.CodeAnalysis.PooledObjects;

internal readonly partial struct PooledDisposer<TPoolable>(TPoolable instance) : IDisposable
where TPoolable : class, IPooled
{
void IDisposable.Dispose()
=> instance?.Free();
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
<Compile Include="..\..\Compilers\Core\Portable\FileSystem\PathUtilities.cs" Link="Utilities\PathUtilities.cs" />
<Compile Include="..\..\Compilers\Core\Portable\FileSystem\PathKind.cs" Link="Utilities\PathKind.cs" />
<Compile Include="..\..\Compilers\Core\Portable\Collections\ArrayBuilderExtensions.cs" Link="Utilities\ArrayBuilderExtensions.cs" />
<Compile Include="..\..\Dependencies\PooledObjects\PooledDisposer.cs" Link="Utilities\PooledDisposer.cs" />
<Compile Include="..\..\Dependencies\PooledObjects\IPooled.cs" Link="Utilities\IPooled.cs" />
<Compile Include="..\..\Dependencies\PooledObjects\ArrayBuilder.cs" Link="Utilities\ArrayBuilder.cs" />
<Compile Include="..\..\Dependencies\PooledObjects\ArrayBuilder.Enumerator.cs" Link="Utilities\ArrayBuilder.Enumerator.cs" />
<Compile Include="..\..\Dependencies\PooledObjects\ObjectPool`1.cs" Link="Utilities\ObjectPool`1.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
<Compile Include="..\..\..\Compilers\Core\Portable\CaseInsensitiveComparison.cs" Link="Compiler\CaseInsensitiveComparison.cs" />
<Compile Include="..\..\..\Compilers\Shared\NamedPipeUtil.cs" Link="SharedUtilities\NamedPipeUtil.cs" />
<Compile Include="..\..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\Extensions\ImmutableArrayExtensions.cs" Link="SharedUtilities\ImmutableArrayExtensions.cs" />
<Compile Include="..\..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\ObjectPools\IPooled.cs" Link="SharedUtilities\IPooled.cs" />
<Compile Include="..\..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\ObjectPools\Extensions.cs" Link="SharedUtilities\Extensions.cs" />
<Compile Include="..\..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\ObjectPools\PooledDisposer.cs" Link="SharedUtilities\PooledDisposer.cs" />
<Compile Include="..\..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\ObjectPools\PooledHashSet.cs" Link="SharedUtilities\PooledHashSet.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SymbolUsageInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\TypeOrNamespaceUsageInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ValueUsageInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObjectPools\ArrayBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObjectPools\Extensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObjectPools\IPooled.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObjectPools\PooledDisposer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObjectPools\PooledHashSet.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ObjectPools\PooledObject.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
namespace Microsoft.CodeAnalysis.PooledObjects;

[NonCopyable]
Copy link
Member Author

Choose a reason for hiding this comment

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

Adds [NonCopyable] attribute to the type definition.

Copy link
Member

Choose a reason for hiding this comment

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

What about just requiring the consumer to provide this attribute in all cases?

Copy link
Member Author

@tmat tmat Feb 1, 2025

Choose a reason for hiding this comment

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

Isn't the analyzer Roslyn specific? I would rather not impose more requirements on consumers of this source package.

That said, I'm working on another source package (say Microsoft.CodeAnalysis.Contracts) with basic contracts and exception utilities, which are used by our threading utilities (extracted to source package #76937).

We can include NonCopyable there. It's a contract after all, so it would fit.

Copy link
Member Author

@tmat tmat Feb 1, 2025

Choose a reason for hiding this comment

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

#76997 (Add Microsoft.CodeAnalysis.Contracts source package)

internal readonly struct PooledDisposer<TPoolable>(TPoolable instance) : IDisposable
internal partial struct PooledDisposer<TPoolable>
where TPoolable : class, IPooled
{
void IDisposable.Dispose()
=> instance?.Free();
}
Loading