Skip to content

Commit d2822c2

Browse files
committed
Rework semantic model caching
1 parent 704a5e4 commit d2822c2

File tree

1 file changed

+15
-7
lines changed
  • csharp/extractor/Semmle.Extraction.CSharp/Extractor

1 file changed

+15
-7
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Context.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,26 @@ internal class Context : Extraction.Context
1818
/// </summary>
1919
public SemanticModel GetModel(SyntaxNode node)
2020
{
21-
// todo: when this context belongs to a SourceScope, the syntax tree can be retrieved from the scope, and
22-
// the node parameter could be removed. Is there any case when we pass in a node that's not from the current
23-
// tree?
24-
if (cachedModel is null || node.SyntaxTree != cachedModel.SyntaxTree)
21+
if (node.SyntaxTree == SourceTree)
2522
{
26-
cachedModel = Compilation.GetSemanticModel(node.SyntaxTree);
23+
if (cachedModelForTree is null)
24+
{
25+
cachedModelForTree = Compilation.GetSemanticModel(node.SyntaxTree);
26+
}
27+
28+
return cachedModelForTree;
29+
}
30+
31+
if (cachedModelForOtherTrees is null || node.SyntaxTree != cachedModelForOtherTrees.SyntaxTree)
32+
{
33+
cachedModelForOtherTrees = Compilation.GetSemanticModel(node.SyntaxTree);
2734
}
2835

29-
return cachedModel;
36+
return cachedModelForOtherTrees;
3037
}
3138

32-
private SemanticModel? cachedModel;
39+
private SemanticModel? cachedModelForTree;
40+
private SemanticModel? cachedModelForOtherTrees;
3341

3442
/// <summary>
3543
/// The current compilation unit.

0 commit comments

Comments
 (0)