You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
An ExecuteReaderAsync that returns MySqlDataReader #822
MySqlCommand has its ExecuteReaderAsync methods inherited from DbCommand. These return DbDataReader which is fine for the most part, except sometimes you want the underlying MySqlDataReader.
For example MySqlDataReader has a GetTimeSpan method that DbDataReader does not provide.
Could there please be MySqlDataReader-returning equivalents of ExecuteReaderAsync? For now I am explicitly casting the return value.
I haven't added this in the past because it would add extra overhead to convert Task<DbDataReader> (that has to be returned from the overridden virtual method) to Task<MySqlDataReader> , because Task<T> is not covariant.
Then all code calling ExecuteReaderAsync on a MySqlCommand object would have an extra async state machine created (for this async method) just to implement a single cast. The overhead would dwarf the work that is performed.
Or, alternatively, if the default code path did return Task<MySqlDataReader>, then the virtual method would have to contain an extra "thunk" that upcast the result to DbDataReader, and consumers that use the DbCommand ADO.NET classes directly would suffer a performance penalty.
I think you could write an extension method to do this work (or continue casting the return value as you currently are) but I think the performance penalty that it would incur currently makes it infeasible to include on this API by default. (Hmm, maybe MySqlConnector could provide it as an extension method in a separate namespace so you have to explicit write using MySqlConnector.SlowConvenienceMethods to get it?)
It looks like Npgsql chooses to implement new Task<NpgsqlReader> ExecuteReaderAsync efficiently, and "penalise" the virtual DbCommand implementation with an extra await.
Microsoft.Data.SqlClient is similar (new Task<SqlDataReader> ExecuteReaderAsync is efficient) and the DbCommand method is implemented with ContinueWith to perform the cast.
It looks like generally an easier-to-use API is preferred over a slight performance boost. (I should benchmark the alternatives and see how they perform.)
Activity
bgrainger commentedon May 27, 2020
I haven't added this in the past because it would add extra overhead to convert
Task<DbDataReader>(that has to be returned from the overridden virtual method) toTask<MySqlDataReader>, becauseTask<T>is not covariant.That is, if this method were added:
Then all code calling
ExecuteReaderAsyncon aMySqlCommandobject would have an extra async state machine created (for thisasyncmethod) just to implement a single cast. The overhead would dwarf the work that is performed.Or, alternatively, if the default code path did return
Task<MySqlDataReader>, then the virtual method would have to contain an extra "thunk" that upcast the result toDbDataReader, and consumers that use theDbCommandADO.NET classes directly would suffer a performance penalty.I think you could write an extension method to do this work (or continue casting the return value as you currently are) but I think the performance penalty that it would incur currently makes it infeasible to include on this API by default. (Hmm, maybe MySqlConnector could provide it as an extension method in a separate namespace so you have to explicit write
using MySqlConnector.SlowConvenienceMethodsto get it?)I don't think C# 9.0 covariant return types (https://github.com/dotnet/csharplang/blob/master/proposals/covariant-returns.md) will help this situation either.
bgrainger commentedon May 27, 2020
It looks like Npgsql chooses to implement
new Task<NpgsqlReader> ExecuteReaderAsyncefficiently, and "penalise" the virtualDbCommandimplementation with an extraawait.Microsoft.Data.SqlClient is similar (
new Task<SqlDataReader> ExecuteReaderAsyncis efficient) and theDbCommandmethod is implemented withContinueWithto perform the cast.It looks like generally an easier-to-use API is preferred over a slight performance boost. (I should benchmark the alternatives and see how they perform.)
taspeotis commentedon May 27, 2020
Thanks for the commentary. And regarding
Microsoft.Data.SqlClientI was just looking at the exact same place as you :)For reference: https://github.com/dotnet/SqlClient/blob/df35d18f869ac2daa410ce12ee5264b7759944cd/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs#L2304-L2314
bgrainger commentedon May 30, 2020
Benchmarks show that
awaitshould be preferred overContinueWithon both platforms:Return MySqlDataReader from ExecuteReaderAsync. Fixes #822
Return MySqlDataReader from ExecuteReaderAsync. Fixes #822
taspeotis commentedon May 30, 2020
Thank you!
bgrainger commentedon May 30, 2020
This is implemented in 0.67.0.