Skip to content

Commit 0d81c61

Browse files
committed
Avoid rebuilding the _relevantIdentifiers set for every document
1 parent 46c2035 commit 0d81c61

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

sources/SilkTouch/SilkTouch/Mods/LocationTransformation/LocationTransformationRewriter.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,38 @@ List<LocationTransformer> transformers
5151

5252
// Used to skip symbol lookups
5353
// Does not handle the omission of the "-Attribute" suffix, but generally, we don't need to transform attributes
54-
_relevantIdentifiers = _symbols.Select(s => s.Name).ToHashSet();
54+
_relevantIdentifiers = new HashSet<string>(_symbols.Count);
55+
foreach (var symbol in _symbols)
56+
{
57+
_relevantIdentifiers.Add(symbol.Name);
58+
}
59+
}
60+
61+
private LocationTransformationRewriter(
62+
HashSet<ISymbol> symbols,
63+
List<LocationTransformer> transformers,
64+
HashSet<string> relevantIdentifiers
65+
)
66+
{
67+
_symbols = symbols;
68+
_transformers = transformers;
69+
_relevantIdentifiers = relevantIdentifiers;
5570
}
5671

5772
/// <summary>
5873
/// Initializes the renamer to work for a new document. Must be called before visiting any nodes.
5974
/// </summary>
6075
public void Initialize(SemanticModel semanticModel) => _semanticModel = semanticModel;
6176

77+
/// <summary>
78+
/// Clone this rewriter for purposes of thread safety.
79+
/// </summary>
80+
/// <remarks>
81+
/// This is allowed to return the current instance and share data.
82+
/// </remarks>
83+
public LocationTransformationRewriter GetThreadSafeCopy() =>
84+
new(_symbols, [.. _transformers.Select(t => t.GetThreadSafeCopy())], _relevantIdentifiers);
85+
6286
/// <inheritdoc />
6387
[return: NotNullIfNotNull("unmodifiedNode")]
6488
public override SyntaxNode? Visit(SyntaxNode? unmodifiedNode)

sources/SilkTouch/SilkTouch/Mods/LocationTransformation/LocationTransformationUtils.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static async Task ModifyAllReferencesAsync(
5353

5454
var newDocuments = new ConcurrentDictionary<DocumentId, SyntaxNode>();
5555
var symbolSet = new HashSet<ISymbol>(symbols, SymbolEqualityComparer.Default);
56+
var baseRewriter = new LocationTransformationRewriter(symbolSet, transformers.ToList());
5657
await Parallel.ForEachAsync(
5758
documentIds,
5859
ct,
@@ -73,10 +74,7 @@ await Parallel.ForEachAsync(
7374
var semanticModel = compilation.GetSemanticModel(originalRoot.SyntaxTree);
7475

7576
// Since this is multithreaded, each thread needs their own copy of the rewriter and transformers
76-
var rewriter = new LocationTransformationRewriter(
77-
symbolSet,
78-
[.. transformers.Select(t => t.GetThreadSafeCopy())]
79-
);
77+
var rewriter = baseRewriter.GetThreadSafeCopy();
8078

8179
rewriter.Initialize(semanticModel);
8280
var newRoot = rewriter.Visit(originalRoot);

sources/SilkTouch/SilkTouch/Mods/LocationTransformation/LocationTransformer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ ISymbol symbol
3131

3232
/// <summary>
3333
/// Clone this location transformer for purposes of thread safety.
34-
/// If the location transformer is already thread safe, the location transformer
35-
/// does not need to be cloned.
3634
/// </summary>
35+
/// <remarks>
36+
/// This is allowed to return the current instance and share data.
37+
/// </remarks>
3738
public abstract LocationTransformer GetThreadSafeCopy();
3839
}

0 commit comments

Comments
 (0)