Repository navigation
Draft: Update types to parse with ReadOnlySpan<char> - #1974
austindrenski wants to merge 2 commits into
Conversation
Also adds a simple benchmark to test parsing allocations. Related: - 1939 - 1943
08dac60 to
9417b59
Compare
- Adds parsing tests - Adds benchmark tests
roji
left a comment
There was a problem hiding this comment.
It's nice to see the first appearance of Span in the codebase. However, see my comments about avoiding string allocations - either we go all the way and eliminate all allocations, or I'm not sure it's worth the effort.
|
|
||
| #endregion | ||
|
|
||
| // #region String Conversions |
There was a problem hiding this comment.
Is there a reason for leaving this commented-out code?
There was a problem hiding this comment.
Should have tagged this as still being a work in progress. I left commented the original code for reference while drafting. I'll make sure to cut it before this is finished.
| ? str.Slice(yearSeparator + monthSeparator + 2) | ||
| : str.Slice(yearSeparator + monthSeparator + 2, daySeparator); | ||
|
|
||
| var year = int.Parse(yearSegment.ToString()); |
There was a problem hiding this comment.
Doing ToString() here kind of defeats the purpose of using Span - we're allocating.
.NET Core 2.1 actually adds overloads that allow you to parse directly from a ReadOnlySpan<char> (see https://github.com/dotnet/corefx/issues/22403), but we don't have them in anything other than .NET Core 2.1. So if we want to do this right, we need to have Npgsql target netcoreapp21 and add an #if here, using the new overloads. This would provide the optimal zero-alloc perf for .NET Core 2.1, while still working for older targets.
Otherwise I'm simply not sure that this PR is really worth it...
There was a problem hiding this comment.
Ah. I figured I had something misconfigured on my end that was hiding those overloads, and went with ToString() as a bandaid at the time. I'll add the #if blocks in the next push.
| } catch (Exception) { | ||
| throw new FormatException(); | ||
| } | ||
| catch (Exception) |
There was a problem hiding this comment.
I hate how exception handling is being done here (and also that TryParse() calls Parse() and catches any exceptions to return false). Any chance you want to make this better?
IMHO the parsing code should be in TryParse(), which itself calls only the int.TryParse(). Then Parse() wraps TryParse() and throws a FormatException if it gets a false.
There was a problem hiding this comment.
Very much agree on the exception handling. I'll see what I can do WRT reorganizing TryParse/Parse.
|
|
||
| T lower = lowerInfinite ? default : (T)BoundConverter.ConvertFromString(lowerSegment); | ||
| T upper = upperInfinite ? default : (T)BoundConverter.ConvertFromString(upperSegment); | ||
| T lower = lowerInfinite ? default : (T)BoundConverter.ConvertFromString(lowerSegment.ToString()); |
There was a problem hiding this comment.
Same as above - we should avoid all allocations in this parsing code.
|
|
||
| [Test] | ||
| [IssueLink("https://github.com/npgsql/npgsql/issues/1943")] | ||
| public void Roundtrip_canonicalization_causes_issues() |
There was a problem hiding this comment.
I'm not sure of the value of having this test... The issue seems to be enough for tracking this no?
There was a problem hiding this comment.
I had this test in mind when I added it:
[Fact(Skip="https://github.com/aspnet/EntityFramework/issues/9242")]
public void Length_on_EF_Property()
{
using (var ctx = CreateContext())
{
// TODO: This fails
var x = ctx.SomeEntities.Single(e => EF.Property<int[]>(e, nameof(SomeArrayEntity.SomeArray)).Length == 2);
Assert.Equal(new[] { 3, 4 }, x.SomeArray);
AssertContainsInSql(@"WHERE array_length(e.""SomeArray"", 1) = 2");
}
}I don't have a strong opinion either way. I can drop this in the next commit, just let me know.
There was a problem hiding this comment.
As you wish, I don't have a strong opinion either way either :)
|
OK, ping me when this is ready for another review. |
fe46200 to
1f156c8
Compare
|
@austindrenski are you still interested in working on this and bringing this up to date? If not we should probably close as it's getting quite old. |
|
@austindrenski I'm going over old PRs to close some of them out, are you still interested in this work? |
|
I've opened #5306 and will keep the branch around. Thanks again for the initial work @austindrenski. Sorry it didn't get in. |
Work in progress:
NpgsqlRange<T>NpgsqlDateRelated: