Skip to content

Commit

Permalink
Update sequence buffer writer / reader (Azure#41560)
Browse files Browse the repository at this point in the history
* update sequence buffer writer / reader

* renames and pr fb

* remove attempts at thread safety and mark buffer sequence as unsafe

* more pr fb as well as adding dispose while copy tests to validate that ony a partial copy is done
  • Loading branch information
m-nash authored Jan 26, 2024
1 parent 492a325 commit 60b5bd2
Show file tree
Hide file tree
Showing 12 changed files with 670 additions and 681 deletions.
29 changes: 14 additions & 15 deletions sdk/core/System.ClientModel/src/Message/BinaryContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private sealed class ModelBinaryContent<T> : BinaryContent where T : IPersistabl
private readonly ModelReaderWriterOptions _options;

// Used when _model is an IJsonModel
private ModelWriter? _writer;
private UnsafeBufferSequence.Reader? _sequenceReader;

// Used when _model is an IModel
private BinaryData? _data;
Expand All @@ -96,7 +96,7 @@ public ModelBinaryContent(T model, ModelReaderWriterOptions options)
_options = options;
}

private ModelWriter Writer
private UnsafeBufferSequence.Reader SequenceReader
{
get
{
Expand All @@ -105,8 +105,11 @@ private ModelWriter Writer
throw new InvalidOperationException("Cannot use Writer with non-IJsonModel model type.");
}

_writer ??= new ModelWriter((IJsonModel<object>)jsonModel, _options);
return _writer;
if (_sequenceReader == null)
{
_sequenceReader = new ModelWriter<T>(jsonModel, _options).ExtractReader();
}
return _sequenceReader;
}
}

Expand All @@ -126,12 +129,8 @@ private BinaryData Data

public override bool TryComputeLength(out long length)
{
if (ModelReaderWriter.ShouldWriteAsJson(_model, _options))
{
return Writer.TryComputeLength(out length);
}
length = ModelReaderWriter.ShouldWriteAsJson(_model, _options) ? SequenceReader.Length : Data.ToMemory().Length;

length = Data.ToMemory().Length;
return true;
}

Expand All @@ -144,7 +143,7 @@ public override void WriteTo(Stream stream, CancellationToken cancellation)
{
if (ModelReaderWriter.ShouldWriteAsJson(_model, _options))
{
Writer.CopyTo(stream, cancellation);
SequenceReader.CopyTo(stream, cancellation);
return;
}

Expand All @@ -159,7 +158,7 @@ public override async Task WriteToAsync(Stream stream, CancellationToken cancell
{
if (ModelReaderWriter.ShouldWriteAsJson(_model, _options))
{
await Writer.CopyToAsync(stream, cancellation).ConfigureAwait(false);
await SequenceReader.CopyToAsync(stream, cancellation).ConfigureAwait(false);
return;
}

Expand All @@ -168,11 +167,11 @@ public override async Task WriteToAsync(Stream stream, CancellationToken cancell

public override void Dispose()
{
var writer = _writer;
if (writer != null)
var sequenceReader = _sequenceReader;
if (sequenceReader != null)
{
_writer = null;
writer.Dispose();
_sequenceReader = null;
sequenceReader.Dispose();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public static BinaryData Write<T>(T model, ModelReaderWriterOptions? options = d

if (ShouldWriteAsJson(model, options, out IJsonModel<T>? jsonModel))
{
using (ModelWriter<T> writer = new ModelWriter<T>(jsonModel, options))
using (UnsafeBufferSequence.Reader reader = new ModelWriter<T>(jsonModel, options).ExtractReader())
{
return writer.ToBinaryData();
return reader.ToBinaryData();
}
}
else
Expand Down Expand Up @@ -70,9 +70,9 @@ public static BinaryData Write(object model, ModelReaderWriterOptions? options =

if (ShouldWriteAsJson(iModel, options, out IJsonModel<object>? jsonModel))
{
using (ModelWriter<object> writer = new ModelWriter<object>(jsonModel, options))
using (UnsafeBufferSequence.Reader reader = new ModelWriter<object>(jsonModel, options).ExtractReader())
{
return writer.ToBinaryData();
return reader.ToBinaryData();
}
}
else
Expand Down

This file was deleted.

Loading

0 comments on commit 60b5bd2

Please sign in to comment.