Skip to content

Removed decimal math from the numeric handler - #1959

Merged
YohDeadfall merged 2 commits into
npgsql:devfrom
YohDeadfall:NumericHandler
May 29, 2018
Merged

YohDeadfall merged 2 commits into
npgsql:devfrom
YohDeadfall:NumericHandler

Conversation

@YohDeadfall

@YohDeadfall YohDeadfall commented May 28, 2018 •

Copy link
Copy Markdown
Contributor
[Config(typeof(Config))]
public class Numeric
{
    class Config : ManualConfig
    {
        public Config()
        {
            Add(StatisticColumn.OperationsPerSecond);
            Add(MemoryDiagnoser.Default);
        }
    }

    class EndlessStream : Stream
    {
        public override bool CanRead => true;
        public override bool CanSeek => true;
        public override bool CanWrite => true;
        public override long Length => long.MaxValue;
        public override long Position { get => 0L; set { } }
        public override void Flush() { }
        public override int Read(byte[] buffer, int offset, int count) => count;
        public override long Seek(long offset, SeekOrigin origin) => 0L;
        public override void SetLength(long value) { }
        public override void Write(byte[] buffer, int offset, int count) { }
    }

    readonly EndlessStream _stream;
    readonly NumericHandler _handler;
    readonly NpgsqlReadBuffer _readBuffer;
    readonly NpgsqlWriteBuffer _writeBuffer;
    decimal _value;
    int _elementSize;

    public Numeric()
    {
        _stream = new EndlessStream();
        _handler = new NumericHandler();
        _readBuffer = new NpgsqlReadBuffer(null, _stream, NpgsqlReadBuffer.MinimumSize, Encoding.UTF8);
        _writeBuffer = new NpgsqlWriteBuffer(null, _stream, NpgsqlWriteBuffer.MinimumSize, Encoding.UTF8);
    }

    [ParamsSource(nameof(Values))]
    public decimal Value
    {
        get => _value;
        set
        {
            _value = value;
            _elementSize = _handler.ValidateAndGetLength(value, null);
            _handler.Write(_value, _writeBuffer, null);

            Buffer.BlockCopy(_writeBuffer.Buffer, 0, _readBuffer.Buffer, 0, _elementSize);

            _readBuffer.FilledBytes = _elementSize;
            _writeBuffer.WritePosition = 0;
        }
    }

    public static decimal[] Values => new decimal[]
    {
        0.0000000000000000000000000001M,
        0.000000000000000000000001M,
        0.00000000000000000001M,
        0.0000000000000001M,
        0.000000000001M,
        0.00000001M,
        0.0001M,
        1M,
        10000M,
        100000000M,
        1000000000000M,
        10000000000000000M,
        100000000000000000000M,
        1000000000000000000000000M,
        10000000000000000000000000000M,
    };

    [Benchmark]
    public void Read()
    {
        _readBuffer.ReadPosition = 0;
        _handler.Read(_readBuffer, _elementSize);
    }

    [Benchmark]
    public void Write()
    {
        _writeBuffer.WritePosition = 0;
        _handler.Write(_value, _writeBuffer, null);
    }
}

Environment

BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17672
Intel Xeon CPU E5-2687W 0 3.10GHz, 2 CPU, 32 logical and 16 physical cores
.NET Core SDK=2.1.104
  [Host]     : .NET Core 2.0.6 (CoreCLR 4.6.26212.01, CoreFX 4.6.26212.01), 64bit RyuJIT
  DefaultJob : .NET Core 2.0.6 (CoreCLR 4.6.26212.01, CoreFX 4.6.26212.01), 64bit RyuJIT

Before

Method Value Mean Error StdDev Median Op/s Gen 0 Allocated
Read 0.0000000000000000000000000001 98.99 ns 9.486 ns 27.97 ns 110.44 ns 10,102,469.6 - 0 B
Write 0.0000000000000000000000000001 734.31 ns 79.518 ns 234.46 ns 718.50 ns 1,361,828.0 0.0134 88 B
Read 0.000000000000000000000001 92.39 ns 10.263 ns 30.26 ns 84.28 ns 10,823,863.3 - 0 B
Write 0.000000000000000000000001 790.37 ns 67.241 ns 198.26 ns 928.73 ns 1,265,231.1 0.0114 80 B
Read 0.00000000000000000001 98.86 ns 10.972 ns 32.35 ns 91.39 ns 10,115,392.6 - 0 B
Write 0.00000000000000000001 793.41 ns 75.319 ns 222.08 ns 763.46 ns 1,260,382.7 0.0095 72 B
Read 0.0000000000000001 113.19 ns 12.903 ns 38.04 ns 127.59 ns 8,834,640.6 - 0 B
Write 0.0000000000000001 874.61 ns 84.949 ns 250.47 ns 885.01 ns 1,143,362.6 0.0076 64 B
Read 0.000000000001 81.90 ns 9.731 ns 28.69 ns 71.12 ns 12,210,468.5 - 0 B
Write 0.000000000001 759.12 ns 92.137 ns 271.67 ns 662.23 ns 1,317,321.9 0.0076 56 B
Read 0.00000001 81.22 ns 9.960 ns 29.05 ns 68.07 ns 12,312,992.2 - 0 B
Write 0.00000001 814.97 ns 91.525 ns 269.86 ns 763.97 ns 1,227,033.5 0.0067 48 B
Read 0.0001 104.32 ns 12.743 ns 37.57 ns 91.25 ns 9,586,296.6 - 0 B
Write 0.0001 837.73 ns 103.731 ns 305.85 ns 706.02 ns 1,193,700.5 0.0038 40 B
Read 1 79.77 ns 9.350 ns 27.57 ns 79.66 ns 12,535,779.0 - 0 B
Write 1 642.54 ns 74.040 ns 218.31 ns 520.86 ns 1,556,332.6 - 0 B
Read 10000 136.32 ns 16.113 ns 47.51 ns 124.49 ns 7,335,526.0 - 0 B
Write 10000 790.09 ns 92.178 ns 271.79 ns 654.75 ns 1,265,684.8 - 0 B
Read 100000000 221.51 ns 23.777 ns 70.11 ns 209.03 ns 4,514,523.1 - 0 B
Write 100000000 1,263.84 ns 132.749 ns 391.41 ns 1,216.22 ns 791,237.2 - 0 B
Read 1000000000000 307.70 ns 32.525 ns 95.90 ns 282.76 ns 3,249,926.1 - 0 B
Write 1000000000000 1,369.17 ns 174.742 ns 515.23 ns 1,105.62 ns 730,370.6 - 0 B
Read 10000000000000000 392.89 ns 42.986 ns 126.75 ns 319.50 ns 2,545,243.4 - 0 B
Write 10000000000000000 2,073.42 ns 265.549 ns 782.98 ns 1,890.99 ns 482,295.4 - 0 B
Read 100000000000000000000 391.71 ns 50.781 ns 149.73 ns 318.86 ns 2,552,940.3 - 0 B
Write 100000000000000000000 2,522.98 ns 286.452 ns 844.61 ns 2,337.18 ns 396,357.1 - 0 B
Read 1000000000000000000000000 608.60 ns 57.450 ns 169.39 ns 590.60 ns 1,643,106.4 - 0 B
Write 1000000000000000000000000 3,054.67 ns 356.590 ns 1,051.41 ns 2,695.77 ns 327,367.5 - 0 B
Read 10000000000000000000000000000 725.25 ns 74.806 ns 220.57 ns 686.55 ns 1,378,839.0 - 0 B
Write 10000000000000000000000000000 4,207.68 ns 493.067 ns 1,453.82 ns 4,253.90 ns 237,660.6 - 0 B

After

Method Value Mean Error StdDev Median Op/s Allocated
Read 0.0000000000000000000000000001 34.13 ns 4.1215 ns 12.1525 ns 28.32 ns 29,298,953.3 0 B
Write 0.0000000000000000000000000001 82.23 ns 8.2659 ns 24.3722 ns 89.97 ns 12,160,483.4 0 B
Read 0.000000000000000000000001 34.57 ns 4.3373 ns 12.7887 ns 29.58 ns 28,924,761.0 0 B
Write 0.000000000000000000000001 69.45 ns 8.3836 ns 24.7192 ns 54.83 ns 14,398,622.3 0 B
Read 0.00000000000000000001 54.03 ns 0.7285 ns 0.5687 ns 54.04 ns 18,507,692.0 0 B
Write 0.00000000000000000001 49.65 ns 1.0226 ns 1.0044 ns 49.25 ns 20,141,092.3 0 B
Read 0.0000000000000001 39.38 ns 4.5970 ns 13.5544 ns 37.83 ns 25,394,069.4 0 B
Write 0.0000000000000001 68.35 ns 8.4075 ns 24.7897 ns 53.02 ns 14,630,254.2 0 B
Read 0.000000000001 33.19 ns 4.3642 ns 12.8680 ns 26.67 ns 30,133,555.6 0 B
Write 0.000000000001 69.04 ns 9.0738 ns 26.7542 ns 54.27 ns 14,484,515.8 0 B
Read 0.00000001 31.30 ns 3.6484 ns 10.7573 ns 24.85 ns 31,945,470.0 0 B
Write 0.00000001 67.44 ns 7.7882 ns 22.9636 ns 54.67 ns 14,828,948.9 0 B
Read 0.0001 36.90 ns 4.3775 ns 12.9072 ns 36.36 ns 27,100,565.7 0 B
Write 0.0001 60.32 ns 7.9759 ns 23.5171 ns 50.10 ns 16,578,543.3 0 B
Read 1 35.72 ns 4.5044 ns 13.2813 ns 31.57 ns 27,996,855.1 0 B
Write 1 71.37 ns 8.7275 ns 25.7334 ns 63.88 ns 14,010,552.4 0 B
Read 10000 38.27 ns 4.8691 ns 14.3567 ns 32.49 ns 26,131,149.6 0 B
Write 10000 102.38 ns 12.7620 ns 37.6292 ns 88.53 ns 9,767,686.3 0 B
Read 100000000 46.39 ns 5.7433 ns 16.9343 ns 41.55 ns 21,556,982.5 0 B
Write 100000000 198.07 ns 3.2469 ns 3.1889 ns 197.37 ns 5,048,792.6 0 B
Read 1000000000000 52.40 ns 6.3624 ns 18.7598 ns 40.63 ns 19,082,363.1 0 B
Write 1000000000000 169.06 ns 19.4160 ns 57.2484 ns 163.33 ns 5,915,198.8 0 B
Read 10000000000000000 34.57 ns 0.4344 ns 0.3627 ns 34.56 ns 28,928,517.0 0 B
Write 10000000000000000 199.33 ns 23.7493 ns 70.0254 ns 168.47 ns 5,016,716.9 0 B
Read 100000000000000000000 50.65 ns 3.4758 ns 9.3374 ns 47.59 ns 19,743,409.2 0 B
Write 100000000000000000000 309.56 ns 33.7850 ns 99.6159 ns 301.16 ns 3,230,404.0 0 B
Read 1000000000000000000000000 62.99 ns 8.1658 ns 24.0771 ns 49.25 ns 15,874,742.7 0 B
Write 1000000000000000000000000 363.08 ns 36.3023 ns 107.0380 ns 331.64 ns 2,754,213.5 0 B
Read 10000000000000000000000000000 83.80 ns 8.8570 ns 26.1151 ns 80.40 ns 11,933,130.4 0 B
Write 10000000000000000000000000000 415.18 ns 48.1699 ns 142.0300 ns 357.05 ns 2,408,582.4 0 B

/cc @StillLearnin @Brar

@YohDeadfall YohDeadfall added this to the 4.0 milestone May 28, 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.

No time for a deep review, although the approach is very nice, with the unsafe casting of decimal to and from DecimalRaw.

Just to extra paranoid, I'm assuming that the internal memory structure of .NET numeric guaranteed by the runtime specs, so that we know for sure it will never change?

Aside from that LGTM, modulu the two minor comments below.

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.

Any reason not to make this a regular, non-static method (i.e. Add(uint addend) which modifies this)? Same for the methods below.

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.

You are right here because the struct is used only for local variables, but as you know if it is used as parameter, then an instance method is called on a copy. Therefore, I prefer to follow the guidelines and do not implement instance methods in a mutable struct.

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.

Yeah, I know about "mutable structs are evil", but I think sometimes it goes a little too far - as long as everyone is aware it's not that bad. Also as long as the struct itself is passed by ref (as should be the case for big structs) it should be fine. But anyway as you wish.

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.

It's not a ref struct anymore.

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 didn't mean that it's a ref struct, only that it gets passed around by reference (via ref parameters). With a large struct, passing by ref reduces the copying and also makes it less error-prone for the class to be mutable...

Comment thread test/Npgsql.Tests/TestUtil.cs Outdated

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.

Probably a good idea to assert that Read() returned true

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.

A good idea, will do.

@roji

roji commented May 29, 2018

Copy link
Copy Markdown
Member

@YohDeadfall, @StillLearnin and anyone else, for extra credit we also have #448, which would allow reading/writing PostgreSQL numeric as .NET BigInteger, allowing handling values with an arbitrary number of digits.

@YohDeadfall

Copy link
Copy Markdown
Contributor Author

No time for a deep review, although the approach is very nice, with the unsafe casting of decimal to and from DecimalRaw.

Okay, I'm going to merge the PR. You can review it later, and I will push requested changes as a separate pull request. By the way, yesterday I realized how to make the new implementation a bit faster, but will do it for 4.0.1.

Just to extra paranoid, I'm assuming that the internal memory structure of .NET numeric guaranteed by the runtime specs, so that we know for sure it will never change?

Yes, we can be sure that it will never change. It's a breaking change for .NET.

@YohDeadfall
YohDeadfall merged commit 58367c7 into npgsql:dev May 29, 2018
@YohDeadfall
YohDeadfall deleted the NumericHandler branch May 29, 2018 09:16
@StillLearnin

Copy link
Copy Markdown
Contributor

Looks good to me! Thanks for checking.

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