Skip to content

Commit dba217a

Browse files
committed
Optimization: Use BitFaster.Caching FastConcurrentLru for QueryCache
1 parent 7b23854 commit dba217a

File tree

3 files changed

+10
-22
lines changed

3 files changed

+10
-22
lines changed

Orm/Xtensive.Orm/Orm/Domain.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2007-2020 Xtensive LLC.
1+
// Copyright (C) 2007-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
@@ -9,6 +9,7 @@
99
using System.Collections.Generic;
1010
using System.Threading;
1111
using System.Threading.Tasks;
12+
using BitFaster.Caching.Lru;
1213
using JetBrains.Annotations;
1314
using Xtensive.Caching;
1415
using Xtensive.Collections;
@@ -121,7 +122,7 @@ public static Domain Demand()
121122

122123
internal ConcurrentDictionary<TypeInfo, ReadOnlyList<PrefetchFieldDescriptor>> PrefetchFieldDescriptorCache { get; private set; }
123124

124-
internal ICache<object, Pair<object, TranslatedQuery>> QueryCache { get; private set; }
125+
internal FastConcurrentLru<object, ParameterizedQuery> QueryCache { get; private set; }
125126

126127
internal ICache<Key, Key> KeyCache { get; private set; }
127128

@@ -422,7 +423,7 @@ internal Domain(DomainConfiguration configuration, object upgradeContextCookie,
422423
KeyGenerators = new KeyGeneratorRegistry();
423424
PrefetchFieldDescriptorCache = new ConcurrentDictionary<TypeInfo, ReadOnlyList<PrefetchFieldDescriptor>>();
424425
KeyCache = new LruCache<Key, Key>(Configuration.KeyCacheSize, k => k);
425-
QueryCache = new LruCache<object, Pair<object, TranslatedQuery>>(Configuration.QueryCacheSize, k => k.First);
426+
QueryCache = new FastConcurrentLru<object, ParameterizedQuery>(Configuration.QueryCacheSize);
426427
PrefetchActionMap = new Dictionary<TypeInfo, Action<SessionHandler, IEnumerable<Key>>>();
427428
Extensions = new ExtensionCollection();
428429
UpgradeContextCookie = upgradeContextCookie;

Orm/Xtensive.Orm/Orm/Internals/CompiledQueryRunner.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2012-2020 Xtensive LLC.
1+
// Copyright (C) 2012-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
@@ -196,25 +196,11 @@ private void AllocateParameterAndReplacer()
196196
});
197197
}
198198

199-
private ParameterizedQuery GetCachedQuery()
200-
{
201-
var cache = domain.QueryCache;
202-
lock (cache) {
203-
return cache.TryGetItem(queryKey, true, out var item)
204-
? (ParameterizedQuery) item.Second
205-
: null;
206-
}
207-
}
199+
private ParameterizedQuery GetCachedQuery() =>
200+
domain.QueryCache.TryGet(queryKey, out var query) ? query : null;
208201

209-
private void PutCachedQuery(ParameterizedQuery parameterizedQuery)
210-
{
211-
var cache = domain.QueryCache;
212-
lock (cache) {
213-
if (!cache.TryGetItem(queryKey, false, out _)) {
214-
cache.Add(new Pair<object, TranslatedQuery>(queryKey, parameterizedQuery));
215-
}
216-
}
217-
}
202+
private void PutCachedQuery(ParameterizedQuery parameterizedQuery) =>
203+
domain.QueryCache.AddOrUpdate(queryKey, parameterizedQuery);
218204

219205
private ParameterContext CreateParameterContext(ParameterizedQuery query)
220206
{

Orm/Xtensive.Orm/Xtensive.Orm.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
</ItemGroup>
4242
<ItemGroup Label="Packages">
4343
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
44+
<PackageReference Include="BitFaster.Caching" Version="1.0.3" />
4445
</ItemGroup>
4546
<ItemGroup Label="T4GeneratorsUpdaters">
4647
<None Update="Arithmetic\Internal\PrimitiveArithmetics.tt">

0 commit comments

Comments
 (0)