Skip to content

Commit

Permalink
Merge pull request #104 from sveinungf/dev/ilist
Browse files Browse the repository at this point in the history
Get span from general IList<T> in AddRowAsync
  • Loading branch information
sveinungf authored Feb 2, 2025
2 parents 7961af9 + ae3961d commit 8343da7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 79 deletions.
14 changes: 7 additions & 7 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@
<GlobalPackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.4" />
<GlobalPackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19" />
<GlobalPackageReference Include="PrimaryConstructorAnalyzer" Version="1.0.6" />
<GlobalPackageReference Include="Roslynator.Analyzers" Version="4.12.10" />
<GlobalPackageReference Include="SonarAnalyzer.CSharp" Version="10.5.0.109200" />
<GlobalPackageReference Include="Roslynator.Analyzers" Version="4.12.11" />
<GlobalPackageReference Include="SonarAnalyzer.CSharp" Version="10.6.0.109712" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="Bogus" Version="35.6.1" />
<PackageVersion Include="ClosedXML" Version="0.104.2" />
<PackageVersion Include="coverlet.collector" Version="6.0.3" />
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
<PackageVersion Include="DocumentFormat.OpenXml" Version="3.2.0" />
<PackageVersion Include="EPPlusFree" Version="4.5.3.8" />
<PackageVersion Include="ErrorProne.NET.Structs" Version="0.6.1-beta.1" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.1" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.9.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Polyfill" Version="7.12.0" />
<PackageVersion Include="PublicApiGenerator" Version="11.3.0" />
<PackageVersion Include="Polyfill" Version="7.13.0" />
<PackageVersion Include="PublicApiGenerator" Version="11.4.1" />
<PackageVersion Include="System.Collections.Immutable" Version="9.0.1" />
<PackageVersion Include="System.Drawing.Common" Version="9.0.1" />
<PackageVersion Include="System.Formats.Asn1" Version="9.0.1" />
<PackageVersion Include="System.IO.Compression" Version="4.3.0" />
<PackageVersion Include="System.Memory" Version="4.6.0" />
<PackageVersion Include="System.Reflection.Metadata" Version="9.0.1" />
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageVersion Include="TngTech.ArchUnitNET.xUnit" Version="0.11.1" />
<PackageVersion Include="TngTech.ArchUnitNET.xUnit" Version="0.11.2" />
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0" />
<PackageVersion Include="Verify.Xunit" Version="28.9.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ namespace SpreadCheetah.SourceGeneration
public sealed class WorksheetRowGenerationOptionsAttribute : System.Attribute
{
public WorksheetRowGenerationOptionsAttribute() { }
[System.Obsolete(("Configure rule severity in an .editorconfig file instead (e.g. \'dotnet_diagnostic" +
".SPCH1001.severity = none\')."))]
[System.Obsolete("Configure rule severity in an .editorconfig file instead (e.g. \'dotnet_diagnostic" +
".SPCH1001.severity = none\').")]
public bool SuppressWarnings { get; set; }
}
public static class WorksheetRowMetadataServices
Expand Down Expand Up @@ -401,8 +401,8 @@ namespace SpreadCheetah.Styling
public const string Text = "@";
[System.Obsolete("Use NumberFormat.Standard(StandardNumberFormat.ThousandsSeparator)")]
public const string ThousandsSeparator = "#,##0";
[System.Obsolete(("Use NumberFormat.Standard(StandardNumberFormat.ThousandsSeparatorTwoDecimalPlaces" +
")"))]
[System.Obsolete("Use NumberFormat.Standard(StandardNumberFormat.ThousandsSeparatorTwoDecimalPlaces" +
")")]
public const string ThousandsSeparatorTwoDecimalPlaces = "#,##0.00";
[System.Obsolete("Use NumberFormat.Standard(StandardNumberFormat.TwoDecimalPlaces)")]
public const string TwoDecimalPlaces = "0.00";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ namespace SpreadCheetah.SourceGeneration
public sealed class WorksheetRowGenerationOptionsAttribute : System.Attribute
{
public WorksheetRowGenerationOptionsAttribute() { }
[System.Obsolete(("Configure rule severity in an .editorconfig file instead (e.g. \'dotnet_diagnostic" +
".SPCH1001.severity = none\')."))]
[System.Obsolete("Configure rule severity in an .editorconfig file instead (e.g. \'dotnet_diagnostic" +
".SPCH1001.severity = none\').")]
public bool SuppressWarnings { get; set; }
}
public static class WorksheetRowMetadataServices
Expand Down Expand Up @@ -402,8 +402,8 @@ namespace SpreadCheetah.Styling
public const string Text = "@";
[System.Obsolete("Use NumberFormat.Standard(StandardNumberFormat.ThousandsSeparator)")]
public const string ThousandsSeparator = "#,##0";
[System.Obsolete(("Use NumberFormat.Standard(StandardNumberFormat.ThousandsSeparatorTwoDecimalPlaces" +
")"))]
[System.Obsolete("Use NumberFormat.Standard(StandardNumberFormat.ThousandsSeparatorTwoDecimalPlaces" +
")")]
public const string ThousandsSeparatorTwoDecimalPlaces = "#,##0.00";
[System.Obsolete("Use NumberFormat.Standard(StandardNumberFormat.TwoDecimalPlaces)")]
public const string TwoDecimalPlaces = "0.00";
Expand Down
59 changes: 11 additions & 48 deletions SpreadCheetah/CellWriters/BaseCellWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private bool TryAddRowCells(IList<T> cells)
#if NET5_0_OR_GREATER
List<T> cellList => TryAddRowCellsForSpan(System.Runtime.InteropServices.CollectionsMarshal.AsSpan(cellList)),
#endif
_ => TryAddRowCellsForList(cells)
_ => TryAddRowCellsForIList(cells)
};
}

Expand All @@ -93,32 +93,14 @@ private bool TryAddRowCellsForSpan(ReadOnlySpan<T> cells)
return TryWriteRowEnd();
}

private bool TryAddRowCellsForList(IList<T> cells)
private bool TryAddRowCellsForIList(IList<T> cells)
{
var writerState = State;
var column = writerState.Column;
while (column < cells.Count)
{
if (!TryWriteCell(cells[column]))
return false;

writerState.Column = ++column;
}

return TryWriteRowEnd();
using var pooledArray = cells.ToPooledArray();
return TryAddRowCellsForSpan(pooledArray.Span);
}

private bool TryWriteRowEnd() => Buffer.TryWrite("</row>"u8);

private async ValueTask WriteRowEndAsync(Stream stream, CancellationToken token)
{
if (TryWriteRowEnd())
return;

await Buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
TryWriteRowEnd();
}

public async ValueTask AddRowAsync(ReadOnlyMemory<T> cells, uint rowIndex, RowOptions? options, Stream stream, CancellationToken token)
{
// If we get here that means that whatever we tried to write didn't fit in the buffer, so just flush right away.
Expand All @@ -144,36 +126,17 @@ public async ValueTask AddRowAsync(ReadOnlyMemory<T> cells, uint rowIndex, RowOp
await Buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
}

await WriteRowEndAsync(stream, token).ConfigureAwait(false);
if (TryWriteRowEnd())
return;

await Buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
TryWriteRowEnd();
}

public async ValueTask AddRowAsync(IList<T> cells, uint rowIndex, RowOptions? options, Stream stream, CancellationToken token)
{
// If we get here that means that whatever we tried to write didn't fit in the buffer, so just flush right away.
await Buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);

EnsureRowStartIsWritten(rowIndex, options);

while (State.Column < cells.Count)
{
// Attempt to add row cells again
var beforeIndex = State.Column;
if (TryAddRowCells(cells))
return;

// If no cells were added, the next cell is larger than the buffer.
if (State.Column == beforeIndex)
{
var cell = cells[State.Column];
await WriteCellPieceByPieceAsync(cell, stream, token).ConfigureAwait(false);
++State.Column;
}

// One or more cells were added, repeat
await Buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
}

await WriteRowEndAsync(stream, token).ConfigureAwait(false);
using var pooledArray = cells.ToPooledArray();
await AddRowAsync(pooledArray.Memory, rowIndex, options, stream, token).ConfigureAwait(false);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
13 changes: 0 additions & 13 deletions SpreadCheetah/Helpers/Backporting/StringExtensions.cs

This file was deleted.

8 changes: 5 additions & 3 deletions SpreadCheetah/Helpers/PooledArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace SpreadCheetah.Helpers;
private readonly int _count;

public ReadOnlyMemory<T> Memory => _array.AsMemory(0, _count);
public ReadOnlySpan<T> Span => _array.AsSpan(0, _count);

private PooledArray(T[] array, int count)
{
Expand All @@ -17,15 +18,16 @@ private PooledArray(T[] array, int count)

public static PooledArray<T> Create(ICollection<T> collection)
{
if (collection.Count == 0)
var length = collection.Count;
if (length == 0)
return new PooledArray<T>([], 0);

var array = ArrayPool<T>.Shared.Rent(collection.Count);
var array = ArrayPool<T>.Shared.Rent(length);

try
{
collection.CopyTo(array, 0);
return new PooledArray<T>(array, collection.Count);
return new PooledArray<T>(array, length);
}
catch
{
Expand Down

0 comments on commit 8343da7

Please sign in to comment.