Repository navigation
Added support for nullable scalars - #1773
YohDeadfall wants to merge 14 commits into
Conversation
|
Thanks for submitting this, it's good to see contributions to Npgsql. Here are some general remarks. First, one of the main focuses of 3.3 is performance - I'm really working on optimizing things and make Npgsql into a "high-performance" database driver. Your solution an additional (concurrent) dictionary lookup when reading all reference values such as strings (the delegate lookup in In general, I'd want a PR like this to also include benchmark results to understand the impact on reading regular values (both non-nullables such as int and reference types such as string). I'm not 100% it's possible to add this feature without some impact (this depends on how aggressively the JIT can optimize away some checks in generic method code), but at the very least this impact should be minimal. I have quite a few other more specific remarks, but I'd rather we have those conversations over a PR whose general approach is more performance-oriented as it would probably look quite different. I'm going to leave this open for further discussion for now but feel free to close and open a 2nd PR. (Regardless of the above, I agree with you that there's little value in a separate |
|
Just as a general comment: in some cases I avoid a performance hit on the normal cases simply by executing the rarer/more exotic case only in case of a "failure" in the normal path. In other words, you can introduce the nullable handling code only after an attempt to handle the reading via the normal path fails, i.e. since the type handler does not implement |
|
Yep, agree. I think about two things which could improve performance:
I will try our ideas and write the benchmark to test them. Results will be posted here later so they could be compared. |
|
I don't think it's enough just to check if T is string - there are other reference types out there as well. I'd prefer a "fallback approach" as I wrote above:
|
EnvironmentNote. Executed on the virtual machine with Hyper-V hypervisor. Host OS is Windows Server 2012 without other virtual machines. Basic results (without nullable support)
Results with nullable support
Results with nullable support (when
|
| Method | Mean | Error | StdDev | Op/s |
|---|---|---|---|---|
| NullableField | 209.0 ns | 4.337 ns | 11.195 ns | 4,785,463.5 |
| ValueTypeField | 211.3 ns | 4.233 ns | 4.157 ns | 4,731,545.9 |
| ReferenceTypeField | 434.5 ns | 8.610 ns | 15.079 ns | 2,301,528.1 |
| ObjectField | 421.1 ns | 8.440 ns | 20.222 ns | 2,374,629.2 |
|
It makes sense to use delegates to handle all |
|
I'll try to find some time to review and comment ASAP. |
|
Rebased and waiting for your comments. |
|
@YohDeadfall thanks, I promise to try and review in the next few days... a lot of things on my plate. |
5151389 to
8d35b8a
Compare
349e870 to
0fcf927
Compare
|
@roji Review please. |
|
Am slowly getting to it... |
roji
left a comment
There was a problem hiding this comment.
Finally got around to this. This looks much better than before and is pretty much ready. Take a look at my suggestions and let me know what you think.
I'll try to react ASAP to your comments/changes so we can finally merge this soon.
| { | ||
| public static readonly ReadDelegate<T> Read; | ||
| public static readonly ReadAsyncDelegate<T> ReadAsync; | ||
| public static bool Exists = NullableHandler.Construct(out Read, out ReadAsync); |
There was a problem hiding this comment.
Just to make this a bit more readable, why not do the construction part in a static constructor on this class. which would set everything?
Another thought is to check whether the JIT knows to elide the check if (Nullable.GetUnderlyingType(typeof(T)) in general methods. If it is (not likely), it makes sense to move the checks to the call sites rather than checking on Exists, since they will be elided. Either way it doesn't block this PR (especially because of the proposal below to move the check from the readers to Read<TAny>).
I'm saying this because I know that the JIT knows to elide if (typeof(T) == typeof(string)), which allows for all sort of generics "specialization" with 0 runtime cost. For example, this should be happening in GetFieldValue<T>() when checking typeof(T) == typeof(object).
There was a problem hiding this comment.
Unfortunately, not. The JIT doesn't elide such things, but there is an issue (dotnet/coreclr#2591) in the CoreCLR repo.
There was a problem hiding this comment.
The non-generic NullableHandler class is a factory which holds everything it needs and exposes only the Construct<T> method, nothing more. If I move the construction part to the static constructor of NullableHandler<T> which is a holder, then I must change access modifiers of all factory members to internal or public to make them accessible from the generic class. Also it would mix purposes of these classes. So I did what I did. It's a common practice to split logic (see Nullable and Nullable<T> for example).
There was a problem hiding this comment.
Added lost readonly. It would improve performance because the JIT treats readonly fields as constants.
There was a problem hiding this comment.
My proposal was simpler I think... All I was thinking was to move the invocation of Construct<T> to a static constructor on the same class, not to change the relationship NullableHandler and NullableHandler<T>. It's just a minor readability suggestion (when I first looked at your code, I actually misread the assignment to Exists as a property getter...).
So instead of:
internal static class NullableHandler<T>
{
public static readonly ReadDelegate<T> Read;
public static readonly ReadAsyncDelegate<T> ReadAsync;
public static readonly bool Exists = NullableHandler.Construct(out Read, out ReadAsync);
}it would be:
internal static class NullableHandler<T>
{
static NullableHandler() {
Exists = NullableHandler.Construct(out Read, out ReadAsync);
}
public static readonly ReadDelegate<T> Read;
public static readonly ReadAsyncDelegate<T> ReadAsync;
public static readonly bool Exists;
}(or something similar).
If you don't like this for any reason we can skip it.
This is my last comment, after this I can merge, let me know.
There was a problem hiding this comment.
Ah so (: It's not a problem. Will change.
There was a problem hiding this comment.
Done. Let's merge.
|
|
||
| internal delegate ValueTask<T> ReadAsyncDelegate<T>(NpgsqlReadBuffer buffer, int columnLen, bool async, FieldDescription fieldDescription); | ||
|
|
||
| internal static class NullableHandler<T> |
There was a problem hiding this comment.
This approach is great, especially compared to the previous ConcurrentDictionary approach.
| var fieldDescription = RowDescription[column]; | ||
| try | ||
| { | ||
| if (NullableHandler<T>.Exists) |
There was a problem hiding this comment.
I'd still like to see this check happen only after a failure to read the value normally. Why not move this to https://github.com/npgsql/npgsql/blob/dev/src/Npgsql/TypeHandling/NpgsqlSimpleTypeHandler.cs#L122 (for simple) and to https://github.com/npgsql/npgsql/blob/dev/src/Npgsql/TypeHandling/NpgsqlTypeHandler%60.cs#L98 (for non-simple)? This would completely remove the nullable handling from the regular handling flow.
There was a problem hiding this comment.
Do you want to handle nulls in the catch block?
There was a problem hiding this comment.
No; definitely not (we shouldn't do normal flow via exception handling). But the ReadAny<TAny> methods on Npgsql{Simple,}TypeHandler are the place where we try to cast the handler to the interface which handles the user-requested type. If that cast fails, that's a good place for falling back and checking whether maybe the type is nullable etc. I'm still not sure if it's going to work well, but let's give it a shot.
There was a problem hiding this comment.
Does it make sense? If len equals to -1 then there is no reason to use a type handler. There are no data in the buffer to read.
There was a problem hiding this comment.
Let me try to explain: I only meant that these two lines of nullable-handling might be better in Read<Any>, the case where ColumnLen == -1 should probably stay exactly where it is... If it's not clear I can write some code.
There was a problem hiding this comment.
Understand you. You have said that plan to merge NpgsqlDefaultDataReader and NpgsqlSequentialDataReader. Therefore, it would be better to keep the code in the current places because after the merge the only one file will exist where NullableHandler<T> will be used. I want to say that it would be better to keep the class in a single file and use it only in it.
There was a problem hiding this comment.
OK, let's keep it as it is for now - we can always refactor it later.
| } | ||
|
|
||
| [Benchmark] | ||
| public void NullableField() |
There was a problem hiding this comment.
Expression-bodied methods (here and below)
|
|
||
| namespace Npgsql.Tests.Types | ||
| { | ||
| public class NullableTypeTests : TestBase |
There was a problem hiding this comment.
We can move this to ReaderTests, which is already parameterized on sequential and non-sequential (i.e. this test will automatically be executed twice).
|
Sqashed, rebased and merged via 85a6f51. Thanks @YohDeadfall! |
I've moved calls of
NpgsqlTypeHandler.Read<T>from theGetFieldValue<T>implementations into delegates to handle all values in the existingtry-catchblocks without additional type checks. An delegate cache is implemented via theNullableHandlerclass with is singleton, but I've found thatGetFieldValue<T>is used internally when opening a connection andNullableHandlergets instantiated even if theGetFieldValue<T>method isn't used in user code. Therefore I think that the class should be removed and all its fields should be moved intoNpgsqlDataReaderto eliminate additional JIT instructions.Fixes #1728