Skip to content

Commit 5ed850e

Browse files
committed
Trigger closure check only if query uses local collections
As I see local collections trigger ItemToTupleConverter to be built and closure type instance can be captured within ParameterizedQuery.
1 parent 709a083 commit 5ed850e

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

Orm/Xtensive.Orm/Orm/Internals/CompiledQueryProcessingScope.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2020 Xtensive LLC.
1+
// Copyright (C) 2009-2022 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: Alexis Kochetov
@@ -19,6 +19,7 @@ internal sealed class CompiledQueryProcessingScope
1919
public ExtendedExpressionReplacer QueryParameterReplacer { get; }
2020
public ParameterContext ParameterContext { get; }
2121
public bool Execute { get; }
22+
public bool CheckIfCacheble { get; set; } = false;
2223

2324
[field: ThreadStatic]
2425
internal static CompiledQueryProcessingScope Current { get; private set; }

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private ParameterizedQuery GetScalarQuery<TResult>(
128128
throw new NotSupportedException(Strings.ExNonLinqCallsAreNotSupportedWithinQueryExecuteDelayed);
129129
}
130130

131-
PutQueryToCacheIfAllowed(parameterizedQuery);
131+
PutQueryToCacheIfAllowed(parameterizedQuery, scope.CheckIfCacheble);
132132

133133
return parameterizedQuery;
134134
}
@@ -149,7 +149,7 @@ private ParameterizedQuery GetSequenceQuery<TElement>(
149149
parameterizedQuery = (ParameterizedQuery) translatedQuery;
150150
}
151151

152-
PutQueryToCacheIfAllowed(parameterizedQuery);
152+
PutQueryToCacheIfAllowed(parameterizedQuery, scope.CheckIfCacheble);
153153

154154
return parameterizedQuery;
155155
}
@@ -266,16 +266,15 @@ private static bool IsTypeCacheable(Type type, IReadOnlySet<Type> supportedTypes
266266
private ParameterizedQuery GetCachedQuery() =>
267267
domain.QueryCache.TryGetItem(queryKey, true, out var item) ? item.Second : null;
268268

269-
private void PutQueryToCacheIfAllowed(ParameterizedQuery parameterizedQuery) {
270-
if (IsQueryCacheable()) {
271-
domain.QueryCache.Add(new Pair<object, ParameterizedQuery>(queryKey, parameterizedQuery));
272-
}
273-
else {
269+
private void PutQueryToCacheIfAllowed(ParameterizedQuery parameterizedQuery, in bool checkIfCacheable) {
270+
if (checkIfCacheable && !IsQueryCacheable()) {
274271
// no .resx used because it is hot path.
275272
if (OrmLog.IsLogged(Logging.LogLevel.Info))
276273
OrmLog.Info("Query can't be cached because closure type it has references to captures reference" +
277274
" type instances. This will lead to long-living objects in memory.");
275+
return;
278276
}
277+
domain.QueryCache.Add(new Pair<object, ParameterizedQuery>(queryKey, parameterizedQuery));
279278
}
280279

281280
private ParameterContext CreateParameterContext(ParameterizedQuery query)

Orm/Xtensive.Orm/Orm/Linq/Translator.Queryable.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2009-2021 Xtensive LLC.
1+
// Copyright (C) 2009-2022 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: Alexis Kochetov
@@ -1696,6 +1696,7 @@ private ProjectionExpression VisitLocalCollectionSequence<TItem>(Expression sequ
16961696
if (compiledQueryScope != null) {
16971697
var replacer = compiledQueryScope.QueryParameterReplacer;
16981698
var replace = replacer.Replace(sequence);
1699+
compiledQueryScope.CheckIfCacheble = true;
16991700
var parameter = ParameterAccessorFactory.CreateAccessorExpression<IEnumerable<TItem>>(replace);
17001701
collectionGetter = parameter.CachingCompile();
17011702
}

0 commit comments

Comments
 (0)