Skip to content

Commit 3cca10c

Browse files
committed
cache has sqlite attribute
1 parent 2929788 commit 3cca10c

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

nuget/Sqlite_net.SourceGenerator/SQLiteFastColumnSetterGenerator.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using Microsoft.CodeAnalysis;
34
using Microsoft.CodeAnalysis.CSharp;
45
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -14,6 +15,8 @@
1415
[Generator]
1516
public class SQLiteFastColumnSetterGenerator : IIncrementalGenerator
1617
{
18+
private static ConcurrentDictionary<INamedTypeSymbol, bool> cachedHasSqliteAttribute = new ();
19+
private static ConcurrentDictionary<INamedTypeSymbol, bool> cachedHasTableAttribute = new ();
1720
private static List<string> SQLitePropertyAttributes = default!;
1821
private static ImmutableHashSet<string> SQLitePropertyFullAttributes = default!;
1922

@@ -166,15 +169,23 @@ static bool HasBaseClass (ClassDeclarationSyntax classDecl)
166169

167170
private static bool HasSQLiteAttribute (INamedTypeSymbol? classSymbol)
168171
{
172+
if (classSymbol != null && cachedHasSqliteAttribute.TryGetValue(classSymbol, out var result)) {
173+
return result;
174+
}
175+
169176
while (true) {
170177
if (classSymbol == null || classSymbol.SpecialType == SpecialType.System_Object) {
178+
if (classSymbol != null) {
179+
cachedHasSqliteAttribute[classSymbol] = false;
180+
}
171181
return false;
172182
}
173183

174184
var members = classSymbol.GetMembers();
175185
foreach (var member in members) {
176186
if (member.GetAttributes().Any(attr => IsSQLiteAttribute (attr.AttributeClass)))
177187
{
188+
cachedHasSqliteAttribute[classSymbol] = true;
178189
return true;
179190
}
180191
}
@@ -185,14 +196,24 @@ private static bool HasSQLiteAttribute (INamedTypeSymbol? classSymbol)
185196

186197
private static bool HasTableAttribute (INamedTypeSymbol? classSymbol)
187198
{
188-
while (true) {
199+
if (classSymbol != null && cachedHasTableAttribute.TryGetValue (classSymbol, out var result)) {
200+
return result;
201+
}
202+
203+
while (true) {
189204
if (classSymbol == null || classSymbol.SpecialType == SpecialType.System_Object) {
205+
if (classSymbol != null) {
206+
cachedHasTableAttribute[classSymbol] = false;
207+
}
190208
return false;
191209
}
192210

193211
var hasTableAttribute = classSymbol.GetAttributes ()
194212
.Any (attr => IsTableAttribute (attr.AttributeClass));
195-
if (hasTableAttribute) return true;
213+
if (hasTableAttribute) {
214+
cachedHasTableAttribute[classSymbol] = true;
215+
return true;
216+
}
196217

197218
classSymbol = classSymbol.BaseType;
198219
}

0 commit comments

Comments
 (0)