Skip to content
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 @@ -5,6 +5,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.PooledObjects;
Expand Down Expand Up @@ -45,32 +49,46 @@ internal struct FixedSizeArrayBuilder<T>(int capacity)
public void Add(T value)
=> _values[_index++] = value;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ThrowIfTrue([DoesNotReturnIf(parameterValue: true)] bool condition, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string? filePath = null)
{
#if MICROSOFT_CODEANALYSIS_CONTRACTS_NO_CONTRACT
if (condition)
{
var fileName = filePath is null ? null : Path.GetFileName(filePath);
throw new InvalidOperationException($"Unexpected true - file {fileName} line {lineNumber}");
}
#else
Contract.ThrowIfTrue(condition, lineNumber, filePath);
#endif
}

#region AddRange overloads. These allow us to add these collections directly, without allocating an enumerator.

public void AddRange(ImmutableArray<T> values)
{
Contract.ThrowIfTrue(_index + values.Length > _values.Length);
ThrowIfTrue(_index + values.Length > _values.Length);
Array.Copy(ImmutableCollectionsMarshal.AsArray(values)!, 0, _values, _index, values.Length);
_index += values.Length;
}

public void AddRange(List<T> values)
{
Contract.ThrowIfTrue(_index + values.Count > _values.Length);
ThrowIfTrue(_index + values.Count > _values.Length);
foreach (var v in values)
Add(v);
}

public void AddRange(HashSet<T> values)
{
Contract.ThrowIfTrue(_index + values.Count > _values.Length);
ThrowIfTrue(_index + values.Count > _values.Length);
foreach (var v in values)
Add(v);
}

public void AddRange(ArrayBuilder<T> values)
{
Contract.ThrowIfTrue(_index + values.Count > _values.Length);
ThrowIfTrue(_index + values.Count > _values.Length);
foreach (var v in values)
Add(v);
}
Expand Down Expand Up @@ -104,7 +122,7 @@ public ImmutableArray<T> MoveToImmutable()

public T[] MoveToArray()
{
Contract.ThrowIfTrue(_index != _values.Length);
ThrowIfTrue(_index != _values.Length);
var result = _values;
_values = [];
_index = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ public static ImmutableArray<TResult> SelectAsArray<TItem, TArg, TResult>(this I
return ImmutableArray.Create(map(items[0], 0, arg), map(items[1], 1, arg), map(items[2], 2, arg), map(items[3], 3, arg));

default:
var builder = ArrayBuilder<TResult>.GetInstance(items.Length);
var builder = new FixedSizeArrayBuilder<TResult>(items.Length);
for (int i = 0; i < items.Length; i++)
{
builder.Add(map(items[i], i, arg));
}

return builder.ToImmutableAndFree();
return builder.MoveToImmutable();
}
}

Expand Down Expand Up @@ -518,12 +518,13 @@ public static ImmutableArray<TResult> ZipAsArray<T1, T2, TArg, TResult>(this Imm
return ImmutableArray<TResult>.Empty;
}

var builder = ArrayBuilder<TResult>.GetInstance(self.Length);
var builder = new FixedSizeArrayBuilder<TResult>(self.Length);
for (int i = 0; i < self.Length; i++)
{
builder.Add(map(self[i], other[i], i, arg));
}
return builder.ToImmutableAndFree();

return builder.MoveToImmutable();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Import_RootNamespace>Microsoft.CodeAnalysis.Collections</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Extensions\FixedSizeArrayBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Segmented\ImmutableSegmentedDictionary.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Segmented\ImmutableSegmentedDictionary`2+Builder+KeyCollection.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Segmented\ImmutableSegmentedDictionary`2+Builder+PrivateMarshal.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
<Compile Include="..\..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\ObjectPools\Extensions.cs" Link="SharedUtilities\Extensions.cs" />
<Compile Include="..\..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\ObjectPools\PooledObject.cs" Link="SharedUtilities\PooledObject.cs" />
<Compile Include="..\..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\ObjectPools\SharedPools.cs" Link="SharedUtilities\SharedPools.cs" />
<Compile Include="..\..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\Utilities\FixedSizeArrayBuilder.cs" Link="SharedUtilities\FixedSizeArrayBuilder.cs" />
<Compile Include="..\..\..\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\Utilities\SerializableBytes.cs" Link="SharedUtilities\SerializableBytes.cs" />

</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Utilities\CancellableLazy`1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\CommonFormattingHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ComparerWithState.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\FixedSizeArrayBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\InterceptsLocationUtilities.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\ISpeculationAnalyzer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\OptionalExtensions.cs" />
Expand Down