Skip to content

Implement optimised MySqlDataReader.GetX methods #721

Open
@bgrainger

Description

@bgrainger

MySqlDataReader.GetInt32(int ordinal) is essentially implemented as return (int) GetValue(ordinal);

This boxes the int as an object then unboxes it. It would be more efficient to return the int value directly and would eliminate allocations. (Coupled with 00199a0 this would also optimise GetFieldValue<int>.)

For the text protocol, it might be possible to implement this without significant code duplication if each GetX method just needs to check for an integral column type, parse the text, and check for overflow.

A quick-and-dirty implementation yielded the following benchmark results:

.NET Core 3.0:

Method Mean Error StdDev Median Gen 0 Gen 1 Gen 2 Allocated
GetInt32 21.21 ns 0.4360 ns 0.6112 ns 21.06 ns - - - -
GetValue 30.96 ns 0.7450 ns 1.4878 ns 30.56 ns 0.0051 - - 24 B
GetFieldValue 26.04 ns 0.5446 ns 1.0228 ns 25.57 ns - - - -

Before, GetInt32 and GetFieldValue<int> were 31-37ns and also allocating.

.NET 4.8

Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
GetInt32 54.19 ns 1.0760 ns 1.1513 ns - - - -
GetValue 57.13 ns 1.1467 ns 1.6076 ns 0.0050 0.0001 - 24 B

The improvement is less pronounced here, with the method taking ~59ns before the change.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions