Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Unified OutputWriter and BufferWriter #2163

Merged
merged 2 commits into from
Mar 16, 2018

Conversation

KrzysztofCwalina
Copy link
Member

@KrzysztofCwalina KrzysztofCwalina commented Mar 16, 2018

cc: @ahsonkhan, @davidfowl, @joshfree

I used the implementation of basic writer logic (fields, Advance, Ensure, etc.) from https://github.com/aspnet/benchmarks/tree/dev/src/PlatformBenchmarks

Added the ability to write ints and IWritables from the removed BufferWriter.

The perf results are super close (probably within the margin of error). Once the writer and Utf8String are in corefx, I think we will be able to switch the ProtformBenchmark implementation to use these.

Benchmarks.dll Metric Unit Iterations Average STDEV.S Min Max
BufferWriterBench.BufferWriterCopyPlaintext Duration msec 100 49.239 3.057 44.772 66.541
BufferWriterBench.BufferWriterCopyPlaintext Instructions Retired count 100 6.473E+008 1.190E+006 6.460E+008 6.550E+008
BufferWriterBench.BufferWriterCopyPlaintext Cache Misses count 100 81633.280 14440.590 49152.000 1.270E+005
BufferWriterBench.BufferWriterPlaintext Duration msec 100 54.272 4.365 49.248 71.213
BufferWriterBench.BufferWriterPlaintext Instructions Retired count 100 6.531E+008 1.972E+006 6.500E+008 6.620E+008
BufferWriterBench.BufferWriterPlaintext Cache Misses count 100 96788.480 26961.004 53248.000 1.720E+005
BufferWriterBench.BufferWriterPlaintextUtf8 Duration msec 100 52.471 1.981 49.635 60.774
BufferWriterBench.BufferWriterPlaintextUtf8 Instructions Retired count 100 6.534E+008 1.017E+006 6.520E+008 6.560E+008
BufferWriterBench.BufferWriterPlaintextUtf8 Cache Misses count 100 86548.480 17207.879 57344.000 1.475E+005
BufferWriterBench.PlatfromBenchmarkPlaintext Duration msec 100 50.390 2.965 46.137 65.976
BufferWriterBench.PlatfromBenchmarkPlaintext Instructions Retired count 100 6.601E+008 1.348E+006 6.580E+008 6.650E+008
BufferWriterBench.PlatfromBenchmarkPlaintext Cache Misses count 100 86425.600 21449.999 53248.000 1.516E+005

}

public ref struct OutputWriter<T> where T : IBufferWriter<byte>
public ref partial struct BufferWriter<T> where T : IBufferWriter<byte>
Copy link
Contributor

Choose a reason for hiding this comment

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

Since now it's BufferWriter<T>, the file should be renamed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I will rename it in a subsequent change, otherwise github gets often confused about the change and treats files are deleted/added.

{
_buffered = 0;
_output = output;
_span = output.GetSpan();
}

public Span<byte> Span => _span;
public Span<byte> Buffer => _span;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why Buffer? It would be better to name the property Span like it done in Memory.

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought buffer communicates better that the write needs to be Flushed (and one of the fields is called _buffered).


namespace System.Buffers.Writer
{
public ref partial struct BufferWriter<T> where T : IBufferWriter<byte>
Copy link
Member

@ahsonkhan ahsonkhan Mar 16, 2018

Choose a reason for hiding this comment

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

Why does this struct need to be generic when the T is constrained to only a single type?

Copy link
Member Author

Choose a reason for hiding this comment

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

So that IBufferWriter implementations can be structs and be written to without boxing.


public void Write(string value)
{
var utf16Bytes = value.AsSpan().AsBytes();
Copy link
Member

@ahsonkhan ahsonkhan Mar 16, 2018

Choose a reason for hiding this comment

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

nit: remove use of var (here and elsewhere)


public class BufferWriterBench
{
private static AsciiString _crlf = "\r\n";
Copy link
Member

Choose a reason for hiding this comment

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

nit: static fields should be prefixed with s_

Copy link
Member Author

@KrzysztofCwalina KrzysztofCwalina Mar 16, 2018

Choose a reason for hiding this comment

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

will do


static Sink _sink = new Sink(4096);

const int InnerItterations = 1000000;
Copy link
Member

Choose a reason for hiding this comment

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

typo: InnerItterations

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks. will fix

}
}

// copy from System.Buffers.ReaderWriter to isolate cross-dll calls.
Copy link
Member

Choose a reason for hiding this comment

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

Why is this necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

For some reason source code compiled into the test is a bit faster. I wanted to have totally apples to apples comparison between BufferWriters and so I wanted to have them both in the same project.

@KrzysztofCwalina KrzysztofCwalina merged commit add1b01 into dotnet:master Mar 16, 2018
@KrzysztofCwalina KrzysztofCwalina deleted the UnifyWriters branch March 22, 2018 17:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants