-
Notifications
You must be signed in to change notification settings - Fork 874
Improve performance of buffered string write #5985
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -271,13 +271,15 @@ public void WriteDouble(double value) | |
| Advance(sizeof(double)); | ||
| } | ||
|
|
||
| public void WriteChars(ReadOnlySpan<char> data, Encoding encoding) | ||
| public void WriteChars(ReadOnlySpan<char> data, Encoding encoding, int? encodedByteLength = null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should make these error-prone methods public. Instead we can apply the optimization to the internal converters. I'm fine having these signatures accessible internally. |
||
| { | ||
| // If we have more chars than bytes remaining we can immediately go to the slow path. | ||
| if (data.Length <= Remaining) | ||
| { | ||
| // If not, it's worth a shot to see if we can convert in one go. | ||
| var encodedLength = encoding.GetByteCount(data); | ||
| Debug.Assert(encodedByteLength is null || encodedByteLength == encoding.GetByteCount(data)); | ||
| var encodedLength = encodedByteLength ?? encoding.GetByteCount(data); | ||
|
|
||
| if (!ShouldFlush(encodedLength)) | ||
| { | ||
| var count = encoding.GetBytes(data, Span); | ||
|
|
@@ -305,14 +307,16 @@ void Core(ReadOnlySpan<char> data, Encoding encoding) | |
| } | ||
| } | ||
|
|
||
| public ValueTask WriteCharsAsync(ReadOnlyMemory<char> data, Encoding encoding, CancellationToken cancellationToken = default) | ||
| public ValueTask WriteCharsAsync(ReadOnlyMemory<char> data, Encoding encoding, int? encodedByteLength = null, CancellationToken cancellationToken = default) | ||
| { | ||
| var dataSpan = data.Span; | ||
| // If we have more chars than bytes remaining we can immediately go to the slow path. | ||
| if (data.Length <= Remaining) | ||
| { | ||
| // If not, it's worth a shot to see if we can convert in one go. | ||
| var encodedLength = encoding.GetByteCount(dataSpan); | ||
| Debug.Assert(encodedByteLength is null || encodedByteLength == encoding.GetByteCount(dataSpan)); | ||
| var encodedLength = encodedByteLength ?? encoding.GetByteCount(dataSpan); | ||
|
|
||
| if (!ShouldFlush(encodedLength)) | ||
| { | ||
| var count = encoding.GetBytes(dataSpan, Span); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,9 +76,9 @@ static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings) | |
| // Text | ||
| // Update PgSerializerOptions.IsWellKnownTextType(Type) after any changes to this list. | ||
| mappings.AddType<string>(DataTypeNames.Text, | ||
| static (options, mapping, _) => mapping.CreateInfo(options, new StringTextConverter(options.TextEncoding), preferredFormat: DataFormat.Text), isDefault: true); | ||
| static (options, mapping, _) => mapping.CreateInfo(options, new StringTextConverter(options.TextEncoding, nested: false), preferredFormat: DataFormat.Text), isDefault: true); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nestedness is not a mapping invariant. For instance this converter will be composed over by the array converter (via AddArrayType) and it would do the wrong thing. You'd have to look at the writer (we could maybe have some state there) to know whether the converter is called in a top-level context.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I'll take this PR back to draft for a while. I'll have to get a better understanding of the mapping to know what the next steps are. |
||
| mappings.AddStructType<char>(DataTypeNames.Text, | ||
| static (options, mapping, _) => mapping.CreateInfo(options, new CharTextConverter(options.TextEncoding), preferredFormat: DataFormat.Text)); | ||
| static (options, mapping, _) => mapping.CreateInfo(options, new CharTextConverter(options.TextEncoding, nested: false), preferredFormat: DataFormat.Text)); | ||
| // Uses the bytea converters, as neither type has a header. | ||
| mappings.AddType<byte[]>(DataTypeNames.Text, | ||
| static (options, mapping, _) => mapping.CreateInfo(options, new ArrayByteaConverter()), | ||
|
|
@@ -103,9 +103,9 @@ static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings) | |
| DataTypeNames.Xml, DataTypeNames.Name, DataTypeNames.RefCursor }) | ||
| { | ||
| mappings.AddType<string>(dataTypeName, | ||
| static (options, mapping, _) => mapping.CreateInfo(options, new StringTextConverter(options.TextEncoding), preferredFormat: DataFormat.Text), isDefault: true); | ||
| static (options, mapping, _) => mapping.CreateInfo(options, new StringTextConverter(options.TextEncoding, nested: false), preferredFormat: DataFormat.Text), isDefault: true); | ||
| mappings.AddStructType<char>(dataTypeName, | ||
| static (options, mapping, _) => mapping.CreateInfo(options, new CharTextConverter(options.TextEncoding), preferredFormat: DataFormat.Text)); | ||
| static (options, mapping, _) => mapping.CreateInfo(options, new CharTextConverter(options.TextEncoding, nested: false), preferredFormat: DataFormat.Text)); | ||
| // Uses the bytea converters, as neither type has a header. | ||
| mappings.AddType<byte[]>(dataTypeName, | ||
| static (options, mapping, _) => mapping.CreateInfo(options, new ArrayByteaConverter()), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| using BenchmarkDotNet.Attributes; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using Npgsql.Internal; | ||
| using Npgsql.Internal.Converters; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Npgsql.Benchmarks.TypeHandlers; | ||
|
|
||
| [Config(typeof(Config))] | ||
| public class Text() : TypeHandlerBenchmarks<string>(new StringTextConverter(Encoding.UTF8)) | ||
| public class Text() : TypeHandlerBenchmarks<string>(new StringTextConverter(NpgsqlWriteBuffer.UTF8Encoding, nested: false)) | ||
| { | ||
| protected override IEnumerable<string> ValuesOverride() | ||
| { | ||
| for (var i = 1; i <= 10000; i *= 10) | ||
| for (var i = 1; i <= 1000; i *= 10) | ||
| yield return new string('x', i); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.