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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
@@ -1,2 +1,5 @@
M:System.IO.Path.GetTempPath(); Cannot be used safely in APIs or compiler server as underlying environment variables can change during build.
P:System.Environment.CurrentDirectory; Cannot be used safely in APIs or compiler server as underlying environment variables can change during build.
P:System.Environment.CurrentDirectory; Cannot be used safely in APIs or compiler server as underlying environment variables can change during build.
M:Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder`1.GetInstance(Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder{`0}@)"; Explicitly free ArrayBuilder instance instead. Implicit try-finally blocks might negatively affect performance.
M:Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder`1.GetInstance(System.Int32,Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder{`0}@)"; Explicitly free ArrayBuilder instance instead. Implicit try-finally blocks might negatively affect performance.
M:Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder`1.GetInstance(System.Int32,`0,Microsoft.CodeAnalysis.PooledObjects.ArrayBuilder{`0}@)"; Explicitly free ArrayBuilder instance instead. Implicit try-finally blocks might negatively affect performance.
21 changes: 20 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,24 @@ public ImmutableArray<S> SelectDistinct<S>(Func<T, S> selector)
set.Free();
return result.ToImmutableAndFree();
}

public static PooledDisposer<ArrayBuilder<T>> GetInstance(out ArrayBuilder<T> instance)
Copy link
Member

Choose a reason for hiding this comment

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

Definitely need compiler sign off here. This will maje some unusable members show up in completion for a very common type.

Copy link
Member

Choose a reason for hiding this comment

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

Why is this an unusable member?

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.

Perhaps instead of banning the API we can wrap these in #if !COMPILERCORE . If we do, it'd be better to rename the symbol to something more descriptive, e.g. ROSLYN_COMPILER_CORE as it would appear in any repo consuming the source package.
@dotnet/roslyn-compiler Which approach do you prefer?

{
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);
}
}

}
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
14 changes: 14 additions & 0 deletions src/Dependencies/PooledObjects/PooledDisposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.

using System;

namespace Microsoft.CodeAnalysis.PooledObjects;

internal readonly partial struct PooledDisposer<TPoolable>(TPoolable instance) : IDisposable
where TPoolable : class, IPooled
{
void IDisposable.Dispose()
=> instance?.Free();
}
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();
}