Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement optimised MySqlDataReader.GetX methods #721

Open
bgrainger opened this issue Oct 20, 2019 · 4 comments
Open

Implement optimised MySqlDataReader.GetX methods #721

bgrainger opened this issue Oct 20, 2019 · 4 comments

Comments

@bgrainger
Copy link
Member

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.

@bgrainger
Copy link
Member Author

@bgrainger
Copy link
Member Author

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.

It looks like naively extending the current code might result in quite a bit of duplication and potential inconsistency between different access methods. A different approach may be needed.

@ugavnholt
Copy link

Another related optimization, could be the ability to access the buffer of the reader, to be able to use JsonSerializer.DeserializeAsync directly on the buffer of the reader.

@bgrainger
Copy link
Member Author

access the buffer of the reader

If I understand correctly, that sounds like dotnet/runtime#24899. The current best workaround is to call GetStream, which will return a MemoryStream that reads directly from the buffer (#592).

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

No branches or pull requests

2 participants