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

Switch Json's PooledByteBufferWriter to shared ArrayBuffer helper #111348

Merged
merged 7 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -76,11 +76,7 @@ public override Span<byte> GetSpan(int sizeHint = MinimumBufferSize)
public override async ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default)
{
Debug.Assert(_stream is not null);
#if NET
await _stream.WriteAsync(WrittenMemory, cancellationToken).ConfigureAwait(false);
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
#else
await _stream.WriteAsync(_buffer.DangerousGetUnderlyingBuffer(), _buffer.ActiveStartOffset, _buffer.ActiveLength, cancellationToken).ConfigureAwait(false);
#endif
Clear();

return new FlushResult(isCanceled: false, isCompleted: false);
Expand Down

This file was deleted.

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.

using System.Collections.Generic;
using System.IO;
using System.Security.Principal;
using System.Threading.Tasks;

Expand Down
35 changes: 0 additions & 35 deletions src/libraries/Common/tests/System/Net/StreamArrayExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ System.IO.Pipelines.PipeReader</PackageDescription>
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="System\IO\Pipelines\StreamExtensions.netstandard.cs" />
<Compile Include="System\IO\Pipelines\ThreadPoolScheduler.netstandard.cs" />
<Compile Include="System\IO\Pipelines\CancellationTokenExtensions.netstandard.cs" />

<Compile Include="$(CommonPath)System\IO\StreamExtensions.netstandard.cs" Link="Common\System\IO\StreamExtensions.netstandard.cs" />
<Compile Include="$(CommonPath)System\Threading\Tasks\TaskToAsyncResult.cs" Link="Common\System\Threading\Tasks\TaskToAsyncResult.cs" />
</ItemGroup>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<Compile Include="ServerCertificateTest.cs" />
<Compile Include="WinHttpHandlerTest.cs" />
<Compile Include="XunitTestAssemblyAttributes.cs" />
<Compile Include="$(CommonPath)System\IO\StreamExtensions.netstandard.cs"
Link="Common\System\IO\StreamExtensions.netstandard.cs" />
<Compile Include="$(CommonPath)\System\Net\Http\HttpHandlerDefaults.cs"
Link="Common\System\Net\Http\HttpHandlerDefaults.cs" />
<Compile Include="$(CommonTestPath)System\IO\DelegateStream.cs"
Expand All @@ -40,8 +42,6 @@
Link="Common\System\Net\VerboseTestLogging.cs" />
<Compile Include="$(CommonTestPath)System\Net\TestWebProxies.cs"
Link="Common\System\Net\TestWebProxies.cs" />
<Compile Include="$(CommonTestPath)System\Net\StreamArrayExtensions.cs"
Link="Common\System\Net\StreamArrayExtensions.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\ByteAtATimeContent.cs"
Link="Common\System\Net\Http\ByteAtATimeContent.cs" />
<Compile Include="$(CommonTestPath)System\Net\Http\ChannelBindingAwareContent.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ System.Net.ServerSentEvents.SseParser</PackageDescription>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="$(CoreLibSharedDir)System\Runtime\CompilerServices\IsExternalInit.cs" />
<Compile Include="$(CommonPath)System\IO\StreamExtensions.netstandard.cs" Link="Common\System\IO\StreamExtensions.netstandard.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
using System.Buffers;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.ServerSentEvents
{
Expand Down Expand Up @@ -67,34 +63,5 @@ public static unsafe void WriteUtf8String(this IBufferWriter<byte> writer, ReadO

public static bool ContainsLineBreaks(this ReadOnlySpan<char> text) =>
text.IndexOfAny('\r', '\n') >= 0;

#if !NET

public static ValueTask WriteAsync(this Stream stream, ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> segment))
{
return new ValueTask(stream.WriteAsync(segment.Array, segment.Offset, segment.Count, cancellationToken));
}
else
{
return WriteAsyncUsingPooledBuffer(stream, buffer, cancellationToken);

static async ValueTask WriteAsyncUsingPooledBuffer(Stream stream, ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
{
byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length);
buffer.Span.CopyTo(sharedBuffer);
try
{
await stream.WriteAsync(sharedBuffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
}
finally
{
ArrayPool<byte>.Shared.Return(sharedBuffer);
}
}
}
}
#endif
}
}
1 change: 1 addition & 0 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">
<Compile Include="$(CommonPath)System\IO\StreamExtensions.netstandard.cs" Link="Common\System\IO\StreamExtensions.netstandard.cs" />
<Compile Include="System\Collections\Generic\StackExtensions.netstandard.cs" />
<Compile Include="$(CommonPath)System\Buffers\ArrayBufferWriter.cs" Link="Common\System\Buffers\ArrayBufferWriter.cs" />
<Compile Include="$(CoreLibSharedDir)System\Diagnostics\CodeAnalysis\StringSyntaxAttribute.cs" />
Expand Down
Loading