Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Use System.Buffers #550

Closed
wants to merge 15 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.Http/BufferingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Http.Internal
{
public static class BufferingHelper
{
internal const int DefaultBufferThreshold = 1024 * 30;
internal const int DefaultBufferThreshold = 32768;

private readonly static Func<string> _getTempDirectory = () => TempDirectory;

Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.AspNetCore.Http/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"dependencies": {
"Microsoft.AspNetCore.Http.Abstractions": "1.0.0-*",
"Microsoft.AspNetCore.WebUtilities": "1.0.0-*",
"Microsoft.Net.Http.Headers": "1.0.0-*"
"Microsoft.Net.Http.Headers": "1.0.0-*",
"System.Buffers": "4.0.0-*"
},
"frameworks": {
"net451": {},
Expand Down
16 changes: 12 additions & 4 deletions src/Microsoft.AspNetCore.Owin/WebSockets/OwinWebSocketAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -126,11 +127,18 @@ public override async Task CloseAsync(WebSocketCloseStatus closeStatus, string s
await CloseOutputAsync(closeStatus, statusDescription, cancellationToken);
}

byte[] buffer = new byte[1024];
while (State == WebSocketState.CloseSent)
var buffer = ArrayPool<byte>.Shared.Rent(1024);
try
{
// Drain until close received
await ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);
while (State == WebSocketState.CloseSent)
{
// Drain until close received
await ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}

Expand Down
21 changes: 17 additions & 4 deletions src/Microsoft.AspNetCore.WebUtilities/BufferedReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Buffers;
using System.IO;
using System.Text;
using System.Threading;
Expand All @@ -16,19 +17,26 @@ internal class BufferedReadStream : Stream

private readonly Stream _inner;
private readonly byte[] _buffer;
private readonly ArrayPool<byte> _bytePool;
private int _bufferOffset = 0;
private int _bufferCount = 0;
private bool _disposed;

public BufferedReadStream(Stream inner, int bufferSize)
: this(inner, bufferSize, ArrayPool<byte>.Shared)
{
}

public BufferedReadStream(Stream inner, int bufferSize, ArrayPool<byte> bytePool)
{
if (inner == null)
{
throw new ArgumentNullException(nameof(inner));
}

_inner = inner;
_buffer = new byte[bufferSize];
_bytePool = bytePool;
_buffer = bytePool.Rent(bufferSize);
}

public ArraySegment<byte> BufferedData
Expand Down Expand Up @@ -128,10 +136,15 @@ public override void SetLength(long value)

protected override void Dispose(bool disposing)
{
_disposed = true;
if (disposing)
if (!_disposed)
{
_inner.Dispose();
_disposed = true;
_bytePool.Return(_buffer);

if (disposing)
{
_inner.Dispose();
}
}
}

Expand Down
91 changes: 75 additions & 16 deletions src/Microsoft.AspNetCore.WebUtilities/FileBufferingReadStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Buffers;
using System.Diagnostics;
using System.IO;
using System.Threading;
Expand All @@ -17,11 +18,14 @@ namespace Microsoft.AspNetCore.WebUtilities
public class FileBufferingReadStream : Stream
{
private readonly Stream _inner;
private readonly ArrayPool<byte> _bytePool;
private readonly int _memoryThreshold;
private string _tempFileDirectory;
private readonly Func<string> _tempFileDirectoryAccessor;

private Stream _buffer = new MemoryStream(); // TODO: We could have a more efficiently expanding buffer stream.
private Stream _buffer;
private byte[] _rentedBuffer;
private int _readIntoBuffer = 0;
private bool _inMemory = true;
private bool _completelyBuffered;

Expand All @@ -32,6 +36,15 @@ public FileBufferingReadStream(
Stream inner,
int memoryThreshold,
Func<string> tempFileDirectoryAccessor)
: this(inner, memoryThreshold, tempFileDirectoryAccessor, ArrayPool<byte>.Shared)
{
}

public FileBufferingReadStream(
Stream inner,
int memoryThreshold,
Func<string> tempFileDirectoryAccessor,
ArrayPool<byte> bytePool)
{
if (inner == null)
{
Expand All @@ -43,13 +56,33 @@ public FileBufferingReadStream(
throw new ArgumentNullException(nameof(tempFileDirectoryAccessor));
}

_bytePool = bytePool;
if (memoryThreshold < 1024 * 1024)
{
_rentedBuffer = bytePool.Rent(memoryThreshold);
_buffer = new MemoryStream(_rentedBuffer);
}
else
{
_buffer = new MemoryStream();
}

_inner = inner;
_memoryThreshold = memoryThreshold;
_tempFileDirectoryAccessor = tempFileDirectoryAccessor;
}

// TODO: allow for an optional buffer size limit to prevent filling hard disks. 1gb?
public FileBufferingReadStream(Stream inner, int memoryThreshold, string tempFileDirectory)
Copy link
Member

Choose a reason for hiding this comment

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

This ctor isn't chaining.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

eep!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is now

: this(inner, memoryThreshold, tempFileDirectory, ArrayPool<byte>.Shared)
{
}

public FileBufferingReadStream(
Stream inner,
int memoryThreshold,
string tempFileDirectory,
ArrayPool<byte> bytePool)
{
if (inner == null)
{
Expand All @@ -61,6 +94,17 @@ public FileBufferingReadStream(Stream inner, int memoryThreshold, string tempFil
throw new ArgumentNullException(nameof(tempFileDirectory));
}

_bytePool = bytePool;
if (memoryThreshold < 1024 * 1024)
{
_rentedBuffer = bytePool.Rent(memoryThreshold);
_buffer = new MemoryStream(_rentedBuffer);
}
else
{
_buffer = new MemoryStream();
}

_inner = inner;
_memoryThreshold = memoryThreshold;
_tempFileDirectory = tempFileDirectory;
Expand Down Expand Up @@ -135,22 +179,27 @@ private Stream CreateTempFile()
public override int Read(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
if (_buffer.Position < _buffer.Length || _completelyBuffered)
if (_buffer.Position < _readIntoBuffer || _completelyBuffered)
{
// Just read from the buffer
return _buffer.Read(buffer, offset, (int)Math.Min(count, _buffer.Length - _buffer.Position));
return _buffer.Read(buffer, offset, (int)Math.Min(count, _readIntoBuffer - _buffer.Position));
}

int read = _inner.Read(buffer, offset, count);

if (_inMemory && _buffer.Length + read > _memoryThreshold)
if (_inMemory && _readIntoBuffer + read > _memoryThreshold)
{
var oldBuffer = _buffer;
_buffer = CreateTempFile();
_inMemory = false;
oldBuffer.Position = 0;
oldBuffer.CopyTo(_buffer, 1024 * 16);
checked
{
int length = _readIntoBuffer;
_buffer = CreateTempFile();
_buffer.Write(_rentedBuffer, 0, length);
_bytePool.Return(_rentedBuffer);
_rentedBuffer = null;
}
}
_readIntoBuffer += read;

if (read > 0)
{
Expand Down Expand Up @@ -206,22 +255,27 @@ public override int EndRead(IAsyncResult asyncResult)
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
ThrowIfDisposed();
if (_buffer.Position < _buffer.Length || _completelyBuffered)
if (_buffer.Position < _readIntoBuffer || _completelyBuffered)
{
// Just read from the buffer
return await _buffer.ReadAsync(buffer, offset, (int)Math.Min(count, _buffer.Length - _buffer.Position), cancellationToken);
return await _buffer.ReadAsync(buffer, offset, (int)Math.Min(count, _readIntoBuffer - _buffer.Position), cancellationToken);
}

int read = await _inner.ReadAsync(buffer, offset, count, cancellationToken);

if (_inMemory && _buffer.Length + read > _memoryThreshold)
if (_inMemory && _readIntoBuffer + read > _memoryThreshold)
{
var oldBuffer = _buffer;
_buffer = CreateTempFile();
_inMemory = false;
oldBuffer.Position = 0;
await oldBuffer.CopyToAsync(_buffer, 1024 * 16, cancellationToken);
checked
{
int length = _readIntoBuffer;
_buffer = CreateTempFile();
await _buffer.WriteAsync(_rentedBuffer, 0, length, cancellationToken);
_bytePool.Return(_rentedBuffer);
_rentedBuffer = null;
}
}
_readIntoBuffer += read;

if (read > 0)
{
Expand Down Expand Up @@ -270,6 +324,11 @@ protected override void Dispose(bool disposing)
if (!_disposed)
{
_disposed = true;
if (_rentedBuffer != null)
{
_bytePool.Return(_rentedBuffer);
}

if (disposing)
{
_buffer.Dispose();
Expand All @@ -285,4 +344,4 @@ private void ThrowIfDisposed()
}
}
}
}
}
Loading