Skip to content

Draft: Update types to parse with ReadOnlySpan<char> - #1974

Closed
austindrenski wants to merge 2 commits into
npgsql:mainfrom
austindrenski:range-readonly-memory
Closed

austindrenski wants to merge 2 commits into
npgsql:mainfrom
austindrenski:range-readonly-memory

Conversation

@austindrenski

@austindrenski austindrenski commented Jun 3, 2018 •

Copy link
Copy Markdown
Contributor

Work in progress:

NpgsqlRange<T>

BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134
Intel Core i7-6650U CPU 2.20GHz (Skylake), 1 CPU, 4 logical and 2 physical cores
Frequency=2156252 Hz, Resolution=463.7677 ns, Timer=TSC
.NET Core SDK=2.1.300
  [Host]     : .NET Core 2.1.0 (CoreCLR 4.6.26515.07, CoreFX 4.6.26515.06), 64bit RyuJIT
  DefaultJob : .NET Core 2.1.0 (CoreCLR 4.6.26515.07, CoreFX 4.6.26515.06), 64bit RyuJIT


               Method |        Mean |     StdDev | Scaled | ScaledSD |  Gen 0 | Allocated |
--------------------- |------------:|-----------:|-------:|---------:|-------:|----------:|
   ParseEmptyIntRange |    43.67 ns |   2.481 ns |   1.00 |     0.00 |      - |       0 B |
        ParseIntRange |   689.76 ns |  45.454 ns |  15.85 |     1.36 | 0.0525 |     112 B |
                      |             |            |        |          |        |           |
ParseEmptyDoubleRange |    45.12 ns |   4.063 ns |   1.00 |     0.00 |      - |       0 B |
     ParseDoubleRange |   815.08 ns |  53.029 ns |  18.21 |     2.03 | 0.0525 |     112 B |
                      |             |            |        |          |        |           |
  ParseEmptyDateRange |    58.74 ns |   9.547 ns |   1.00 |     0.00 |      - |       0 B |
       ParseDateRange | 1,558.95 ns | 278.341 ns |  27.34 |     7.00 | 0.0687 |     144 B |

NpgsqlDate

BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134
Intel Core i7-6650U CPU 2.20GHz (Skylake), 1 CPU, 4 logical and 2 physical cores
Frequency=2156252 Hz, Resolution=463.7677 ns, Timer=TSC
.NET Core SDK=2.1.300
  [Host]     : .NET Core 2.1.0 (CoreCLR 4.6.26515.07, CoreFX 4.6.26515.06), 64bit RyuJIT
  DefaultJob : .NET Core 2.1.0 (CoreCLR 4.6.26515.07, CoreFX 4.6.26515.06), 64bit RyuJIT


            Method |      Mean |    StdDev | Scaled | ScaledSD |  Gen 0 | Allocated |
------------------ |----------:|----------:|-------:|---------:|-------:|----------:|
 ParseInfiniteDate |  12.90 ns | 0.1784 ns |   1.00 |     0.00 |      - |       0 B |
         ParseDate | 370.42 ns | 4.7614 ns |  28.72 |     0.52 | 0.0491 |     104 B |

Related:

Also adds a simple benchmark to test parsing allocations.

Related:
- 1939
- 1943
@austindrenski
austindrenski force-pushed the range-readonly-memory branch from 08dac60 to 9417b59 Compare June 3, 2018 21:11
- Adds parsing tests
- Adds benchmark tests
@austindrenski austindrenski changed the title Updates NpgsqlRange<T> to use ReadOnlySpan<char> Update types to parse with ReadOnlySpan<char> Jun 3, 2018

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there a reason for leaving this commented-out code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure of the value of having this test... The issue seems to be enough for tracking this no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As you wish, I don't have a strong opinion either way either :)

@roji

roji commented Jun 17, 2018

Copy link
Copy Markdown
Member

OK, ping me when this is ready for another review.

@austindrenski austindrenski self-assigned this Jun 24, 2018
@roji
roji force-pushed the dev branch 2 times, most recently from fe46200 to 1f156c8 Compare July 20, 2018 12:57
@austindrenski austindrenski changed the title Update types to parse with ReadOnlySpan<char> Draft: Update types to parse with ReadOnlySpan<char> Jul 22, 2018
@roji

roji commented Oct 15, 2019

Copy link
Copy Markdown
Member

@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.

@NinoFloris

Copy link
Copy Markdown
Member

@austindrenski I'm going over old PRs to close some of them out, are you still interested in this work?

@NinoFloris

Copy link
Copy Markdown
Member

I've opened #5306 and will keep the branch around. Thanks again for the initial work @austindrenski. Sorry it didn't get in.

@NinoFloris NinoFloris closed this Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants