Skip to content

Removed reflection from composite read and write - #2402

Merged
YohDeadfall merged 3 commits into
npgsql:devfrom
YohDeadfall:composite-perf
Apr 11, 2019
Merged

YohDeadfall merged 3 commits into
npgsql:devfrom
YohDeadfall:composite-perf

Conversation

@YohDeadfall

Copy link
Copy Markdown
Contributor

Fixes #2377.

@YohDeadfall YohDeadfall self-assigned this Mar 27, 2019

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

Great to see this @YohDeadfall - but where are the benchmark results that show how much faster it is? :) Anyway, here's a first review - this isn't trivial code so it may take more than one back and forth... Hopefully I've understood everything correctly and haven't written anything silly.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

There are no benchmarks because I had no time, but am planning to write them and tests too.

@austindrenski austindrenski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is some exciting work. Looking forward to seeing the benchmarks too.

A few questions and suggestions, but otherwise looking good.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This isn't my area of expertise, so please forgive what may actually be a silly question:

Is there a perf reason to avoid checking pgFields[pgFieldIndex].Name == name directly? Is repeatedly reassigning the local cheaper than the one additional index access?

For example, assuming that we're dealing with a composite of 4 fields, (a, b, c, d) and we want to get field a:

The current version (pgField.Name == name):

  • indexes pgFields 4 times
  • accesses .Name 4 times
  • writes pgField 4 times

But if it checked pgFields[pgFieldIndex].Name == name directly, it would need to:

  • indexes pgFields 5 times
  • accesses .Name 4 times
  • writes pgField 1 time.

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.

Don't know and will check.

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.

Checked and verified that a local variable could improve perf when an iteration goes over an array. Probably it's the right behavior because there could be side effects and the JIT takes that into account. The last two tests (without direct return) demonstrate the current scenario when the result is used after a cycle.

One small note before you will see the test results. This isn't applicable to the commented code since pgFields isn't an array but an IReadOnlyList<T>. Therefore, the JIT must call the indexer each time.

BenchmarkDotNet=v0.11.5, OS=Windows 10.0.16299.1029 (1709/FallCreatorsUpdate/Redstone3)
Intel Core i7-8650U CPU 1.90GHz (Kaby Lake R), 1 CPU, 8 logical and 4 physical cores
Frequency=2062499 Hz, Resolution=484.8487 ns, Timer=TSC
.NET Core SDK=2.2.104
  [Host]     : .NET Core 2.2.2 (CoreCLR 4.6.27317.07, CoreFX 4.6.27318.02), 64bit RyuJIT
  DefaultJob : .NET Core 2.2.2 (CoreCLR 4.6.27317.07, CoreFX 4.6.27318.02), 64bit RyuJIT
Method Mean Error StdDev
Iteration_WithLocal_DirectReturn 1.972 ns 0.1022 ns 0.1177 ns
Iteration_WithoutLocal_DirectReturn 2.138 ns 0.1012 ns 0.1039 ns
Iteration_WithLocal_NoDirectReturn 1.713 ns 0.0952 ns 0.0978 ns
Iteration_WithoutLocal_NoDirectReturn 1.956 ns 0.1051 ns 0.1438 ns
private readonly Field[] pgFields = new[]
{
    new Field("one"),
    new Field("two"),
    new Field("three"),
};

[Benchmark]
public Field Iteration_WithLocal_DirectReturn()
{
    for (var pgFieldIndex = pgFields.Length - 1; pgFieldIndex >= 0; --pgFieldIndex)
    {
        var pgField = pgFields[pgFieldIndex];
        if (pgField.Name == "three")
        {
            return pgField;
        }
    }

    return null;
}

[Benchmark]
public Field Iteration_WithoutLocal_DirectReturn()
{
    for (var pgFieldIndex = pgFields.Length - 1; pgFieldIndex >= 0; --pgFieldIndex)
    {
        if (pgFields[pgFieldIndex].Name == "three")
        {
            return pgFields[pgFieldIndex];
        }
    }

    return null;
}

[Benchmark]
public Field Iteration_WithLocal_NoDirectReturn()
{
    Field pgField = null;

    for (var pgFieldIndex = pgFields.Length - 1; pgFieldIndex >= 0; --pgFieldIndex)
    {
        pgField = pgFields[pgFieldIndex];

        if (pgField.Name == "three")
        {
            break;
        }
    }

    return pgField;
}

[Benchmark]
public Field Iteration_WithoutLocal_NoDirectReturn()
{
    Field pgField = null;

    for (var pgFieldIndex = pgFields.Length - 1; pgFieldIndex >= 0; --pgFieldIndex)
    {
        if (pgFields[pgFieldIndex].Name == "three")
        {
            pgField = pgFields[pgFieldIndex];
            break;
        }
    }

    return pgField;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very interesting, thanks for taking a look at it!

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

See the nits and suggestions but otherwise look pretty good to me!

@YohDeadfall I'd like to see what things would look like without boxing structs in the sync path, if you feel this is too much we can merge and then maybe I'll give it a try (or not, depending if I have time...).

@YohDeadfall

Copy link
Copy Markdown
Contributor Author
BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362
Intel Xeon CPU E5-2687W 0 3.10GHz, 2 CPU, 32 logical and 16 physical cores
.NET Core SDK=3.0.100-preview3-010431
  [Host]     : .NET Core 3.0.0-preview3-27503-5 (CoreCLR 4.6.27422.72, CoreFX 4.7.19.12807), 64bit RyuJIT
  DefaultJob : .NET Core 3.0.0-preview3-27503-5 (CoreCLR 4.6.27422.72, CoreFX 4.7.19.12807), 64bit RyuJIT

Mapped composites

Method Value Mean Error StdDev Median Op/s Gen 0 Gen 1 Gen 2 Allocated
Read Npgsq(...)Class [50] 2.161 us 0.0536 us 0.1580 us 2.184 us 462,655.1 0.0515 - - 328 B
Write Npgsq(...)Class [50] 2.575 us 0.0744 us 0.2169 us 2.641 us 388,298.9 0.0916 - - 576 B

Unmapped composites

Method Value Mean Error StdDev Median Op/s Gen 0 Gen 1 Gen 2 Allocated
Read Npgsq(...)Class [50] 3.836 us 0.0840 us 0.2476 us 3.862 us 260,720.3 0.0839 - - 544 B
Write Npgsq(...)Class [50] 4.004 us 0.2896 us 0.8540 us 4.272 us 249,735.3 0.0992 - - 624 B

@roji

roji commented Apr 6, 2019

Copy link
Copy Markdown
Member

Really nice to see the perf improvement @YohDeadfall! Will try to review this and approve ASAP.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

Changed composite materialization to avoid Activator.CreateInstance<T> and added tests for struct handling. Ready for final review.

BenchmarkDotNet=v0.11.5, OS=Windows 10.0.18362
Intel Xeon CPU E5-2687W 0 3.10GHz, 2 CPU, 32 logical and 16 physical cores
.NET Core SDK=3.0.100-preview3-010431
  [Host]     : .NET Core 3.0.0-preview3-27503-5 (CoreCLR 4.6.27422.72, CoreFX 4.7.19.12807), 64bit RyuJIT
  DefaultJob : .NET Core 3.0.0-preview3-27503-5 (CoreCLR 4.6.27422.72, CoreFX 4.7.19.12807), 64bit RyuJIT

Mapped class

Method Mean Error StdDev Median Op/s Gen 0 Gen 1 Gen 2 Allocated
Read 1.889 us 0.1177 us 0.3471 us 2.063 us 529,328.0 0.0496 - - 328 B
Write 2.323 us 0.1592 us 0.4694 us 2.535 us 430,461.0 0.0916 - - 576 B

Mapped struct

Method Mean Error StdDev Median Op/s Gen 0 Gen 1 Gen 2 Allocated
Read 1,285.7 ns 25.96 ns 72.37 ns 1,299.1 ns 777,814.3 0.0305 - - 192 B
Write 838.7 ns 52.80 ns 155.69 ns 898.2 ns 1,192,331.4 0.0458 - - 288 B

Unmapped class

Method Mean Error StdDev Median Op/s Gen 0 Gen 1 Gen 2 Allocated
Read 3.962 us 0.1122 us 0.3308 us 4.011 us 252,423.5 0.0839 - - 544 B
Write 3.524 us 0.3166 us 0.9334 us 3.980 us 283,806.9 0.0992 - - 624 B

Unmapped struct

Method Mean Error StdDev Median Op/s Gen 0 Gen 1 Gen 2 Allocated
Read 3.134 us 0.0627 us 0.1377 us 3.137 us 319,079.4 0.0572 - - 368 B
Write 2.390 us 0.0741 us 0.2184 us 2.451 us 418,380.1 0.0610 - - 384 B

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

LGTM modulu the comments below, thanks @YohDeadfall!

@roji

roji commented Apr 10, 2019

Copy link
Copy Markdown
Member

Feel free to merge @YohDeadfall (don't forget to squash all those commits...).

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

Will merge when answer on #2402 (comment).

@YohDeadfall
YohDeadfall merged commit f52e1a5 into npgsql:dev Apr 11, 2019
@YohDeadfall
YohDeadfall deleted the composite-perf branch April 11, 2019 17:46
@roji

roji commented Apr 11, 2019

Copy link
Copy Markdown
Member

🍾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants