Skip to content

Commit

Permalink
Change Http2Connection.StartWriteAsync to use a callback model (#37353)
Browse files Browse the repository at this point in the history
* Change Http2Connection.StartWriteAsync to use a callback model

In all of the places we use StartWriteAsync, it's followed by some amount of synchronous work and then releasing the lock.  We can instead pass that synchronous work into StartWriteAsync as a callback.  This has a few benefits:
1. If/when StartWriteAsync completes asynchronously, in most of the call sites we don't incur another async method then completing asynchronously and allocating its state machine, or needing to call through another layer of state machines.
2. We can have spans as locals inside of the callbacks as they're not async methods, which lets us reduce the number of times we access Memory.Span.
3. We can more easily experiment with different execution models around invoking the work waiting for the lock.

* Tweak Active/AvailableMemory properties/methods

* Address PR feedback
  • Loading branch information
stephentoub committed Jun 4, 2020
1 parent 6ed13fe commit b2eaf5c
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 169 deletions.
8 changes: 5 additions & 3 deletions src/libraries/Common/src/System/Net/ArrayBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public void Dispose()
public int ActiveLength => _availableStart - _activeStart;
public Span<byte> ActiveSpan => new Span<byte>(_bytes, _activeStart, _availableStart - _activeStart);
public ReadOnlySpan<byte> ActiveReadOnlySpan => new ReadOnlySpan<byte>(_bytes, _activeStart, _availableStart - _activeStart);
public int AvailableLength => _bytes.Length - _availableStart;
public Span<byte> AvailableSpan => new Span<byte>(_bytes, _availableStart, AvailableLength);
public Memory<byte> ActiveMemory => new Memory<byte>(_bytes, _activeStart, _availableStart - _activeStart);
public Memory<byte> AvailableMemory => new Memory<byte>(_bytes, _availableStart, _bytes.Length - _availableStart);

public int AvailableLength => _bytes.Length - _availableStart;
public Span<byte> AvailableSpan => _bytes.AsSpan(_availableStart);
public Memory<byte> AvailableMemory => _bytes.AsMemory(_availableStart);
public Memory<byte> AvailableMemorySliced(int length) => new Memory<byte>(_bytes, _availableStart, length);

public int Capacity => _bytes.Length;

Expand Down

0 comments on commit b2eaf5c

Please sign in to comment.