forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTypeInfoCache.cs
More file actions
169 lines (142 loc) · 6.95 KB
/
TypeInfoCache.cs
File metadata and controls
169 lines (142 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using Npgsql.Internal.Postgres;
namespace Npgsql.Internal;
sealed class TypeInfoCache<TPgTypeId> where TPgTypeId : struct
{
readonly PgSerializerOptions _options;
readonly bool _validatePgTypeIds;
// Mostly used for parameter writing, 8ns
readonly ConcurrentDictionary<Type, PgTypeInfo?> _cacheByClrType = new();
// Used for reading, occasionally for parameter writing where a db type was given.
// 8ns, about 10ns total to scan an array with 6, 7 different clr types under one pg type
readonly ConcurrentDictionary<TPgTypeId, (Type? Type, PgTypeInfo? Info)[]> _cacheByPgTypeId = new();
static TypeInfoCache()
{
if (typeof(TPgTypeId) != typeof(Oid) && typeof(TPgTypeId) != typeof(DataTypeName))
throw new InvalidOperationException("Cannot use this type argument.");
}
public TypeInfoCache(PgSerializerOptions options, bool validatePgTypeIds = true)
{
_options = options;
_validatePgTypeIds = validatePgTypeIds;
}
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="pgTypeId"></param>
/// <param name="defaultTypeFallback">
/// When this flag is true, and both type and pgTypeId are non null, a default info for the pgTypeId can be returned if an exact match
/// can't be found.
/// </param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public PgTypeInfo? GetOrAddInfo(Type? type, TPgTypeId? pgTypeId, bool defaultTypeFallback = false)
{
if (pgTypeId is { } id)
{
if (_cacheByPgTypeId.TryGetValue(id, out var infos))
if (FindMatch(type, infos, defaultTypeFallback) is { } info)
return info;
return AddEntryById(id, infos, defaultTypeFallback);
}
if (type is not null)
return _cacheByClrType.TryGetValue(type, out var info) ? info : AddByType(type);
return null;
PgTypeInfo? FindMatch(Type? type, (Type? Type, PgTypeInfo? Info)[] infos, bool defaultTypeFallback)
{
PgTypeInfo? defaultInfo = null;
var negativeExactMatch = false;
for (var i = 0; i < infos.Length; i++)
{
ref var item = ref infos[i];
if (item.Type == type)
{
if (item.Info is not null || !defaultTypeFallback)
return item.Info;
negativeExactMatch = true;
}
if (defaultTypeFallback && item.Type is null)
defaultInfo = item.Info;
}
// We can only return default info if we've seen a negative match (type: typeof(object), info: null)
// Otherwise we might return a previously requested default while the resolvers could produce the exact match.
return negativeExactMatch ? defaultInfo : null;
}
PgTypeInfo? AddByType(Type type)
{
// We don't pass PgTypeId as we're interested in default converters here.
var info = CreateInfo(type, null, _options, defaultTypeFallback: false, _validatePgTypeIds);
return info is null
? null
: _cacheByClrType.TryAdd(type, info) // We never remove entries so either of these branches will always succeed.
? info
: _cacheByClrType[type];
}
PgTypeInfo? AddEntryById(TPgTypeId pgTypeId, (Type? Type, PgTypeInfo? Info)[]? infos, bool defaultTypeFallback)
{
// We cache negatives (null info) to allow 'object or default' checks to never hit the resolvers after the first lookup.
var info = CreateInfo(type, pgTypeId, _options, defaultTypeFallback, _validatePgTypeIds);
var isDefaultInfo = type is null && info is not null;
if (infos is null)
{
// Also add defaults by their info type to save a future resolver lookup + resize.
infos = isDefaultInfo
? new [] { (type, info), (info!.Type, info) }
: new [] { (type, info) };
if (_cacheByPgTypeId.TryAdd(pgTypeId, infos))
return info;
}
// We have to update it instead.
while (true)
{
infos = _cacheByPgTypeId[pgTypeId];
if (FindMatch(type, infos, defaultTypeFallback) is { } racedInfo)
return racedInfo;
// Also add defaults by their info type to save a future resolver lookup + resize.
var oldInfos = infos;
var hasExactType = false;
if (isDefaultInfo)
{
foreach (var oldInfo in oldInfos)
if (oldInfo.Type == info!.Type)
hasExactType = true;
}
Array.Resize(ref infos, oldInfos.Length + (isDefaultInfo && !hasExactType ? 2 : 1));
infos[oldInfos.Length] = (type, info);
if (isDefaultInfo && !hasExactType)
infos[oldInfos.Length + 1] = (info!.Type, info);
if (_cacheByPgTypeId.TryUpdate(pgTypeId, infos, oldInfos))
return info;
}
}
static PgTypeInfo? CreateInfo(Type? type, TPgTypeId? typeId, PgSerializerOptions options, bool defaultTypeFallback, bool validatePgTypeIds)
{
var pgTypeId = AsPgTypeId(typeId);
// Validate that we only pass data types that are supported by the backend.
var dataTypeName = pgTypeId is { } id ? (DataTypeName?)options.DatabaseInfo.GetDataTypeName(id, validate: validatePgTypeIds) : null;
var info = options.TypeInfoResolver.GetTypeInfo(type, dataTypeName, options);
if (info is null && defaultTypeFallback)
{
type = null;
info = options.TypeInfoResolver.GetTypeInfo(type, dataTypeName, options);
}
if (info is null)
return null;
if (pgTypeId is not null && info.PgTypeId != pgTypeId)
throw new InvalidOperationException("A Postgres type was passed but the resolved PgTypeInfo does not have an equal PgTypeId.");
if (type is not null && !info.IsBoxing && info.Type != type)
throw new InvalidOperationException($"A CLR type '{type}' was passed but the resolved PgTypeInfo does not have an equal Type: {info.Type}.");
return info;
}
static PgTypeId? AsPgTypeId(TPgTypeId? pgTypeId)
=> pgTypeId switch
{
{ } id when typeof(TPgTypeId) == typeof(DataTypeName) => new PgTypeId(Unsafe.As<TPgTypeId, DataTypeName>(ref id)),
{ } id => new PgTypeId(Unsafe.As<TPgTypeId, Oid>(ref id)),
null => null
};
}
}