-
Notifications
You must be signed in to change notification settings - Fork 4k
/
SyntaxNodeCache.cs
343 lines (292 loc) · 11.7 KB
/
SyntaxNodeCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// #define STATS
using System;
using System.Diagnostics;
using Roslyn.Utilities;
#if STATS
using System.Threading;
#endif
namespace Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax
{
/// <summary>
/// Provides caching functionality for green nonterminals with up to 3 children.
/// Example:
/// When constructing a node with given kind, flags, child1 and child2, we can look up
/// in the cache whether we already have a node that contains same kind, flags,
/// child1 and child2 and use that.
///
/// For the purpose of children comparison, reference equality is used as a much cheaper
/// alternative to the structural/recursive equality. This implies that in order to de-duplicate
/// a node to a cache node, the children of two nodes must be already de-duplicated.
/// When adding a node to the cache we verify that cache does contain node's children,
/// since otherwise there is no reason for the node to be used.
/// Tokens/nulls are for this purpose considered deduplicated. Indeed most of the tokens
/// are deduplicated via quick-scanner caching, so we just assume they all are.
///
/// As a result of above, "fat" nodes with 4 or more children or their recursive parents
/// will never be in the cache. This naturally limits the typical single cache item to be
/// a relatively simple expression. We do not want the cache to be completely unbounded
/// on the item size.
/// While it still may be possible to store a gigantic nested binary expression,
/// it should be a rare occurrence.
///
/// We only consider "normal" nodes to be cacheable.
/// Nodes with diagnostics/annotations/directives/skipped, etc... have more complicated identity
/// and are not likely to be repetitive.
///
/// </summary>
internal class GreenStats
{
// TODO: remove when done tweaking this cache.
#if STATS
private static GreenStats stats = new GreenStats();
private int greenNodes;
private int greenTokens;
private int nontermsAdded;
private int cacheableNodes;
private int cacheHits;
internal static void NoteGreen(GreenNode node)
{
Interlocked.Increment(ref stats.greenNodes);
if (node.IsToken)
{
Interlocked.Increment(ref stats.greenTokens);
}
}
internal static void ItemAdded()
{
Interlocked.Increment(ref stats.nontermsAdded);
}
internal static void ItemCacheable()
{
Interlocked.Increment(ref stats.cacheableNodes);
}
internal static void CacheHit()
{
Interlocked.Increment(ref stats.cacheHits);
}
~GreenStats()
{
Console.WriteLine("Green: " + greenNodes);
Console.WriteLine("GreenTk: " + greenTokens);
Console.WriteLine("Nonterminals added: " + nontermsAdded);
Console.WriteLine("Nonterminals cacheable: " + cacheableNodes);
Console.WriteLine("CacheHits: " + cacheHits);
Console.WriteLine("RateOfAll: " + (cacheHits * 100 / (cacheHits + greenNodes - greenTokens)) + "%");
Console.WriteLine("RateOfCacheable: " + (cacheHits * 100 / (cacheableNodes)) + "%");
}
#else
internal static void NoteGreen(GreenNode node)
{
}
[Conditional("DEBUG")]
internal static void ItemAdded()
{
}
[Conditional("DEBUG")]
internal static void ItemCacheable()
{
}
[Conditional("DEBUG")]
internal static void CacheHit()
{
}
#endif
}
internal class SyntaxNodeCache
{
private const int CacheSizeBits = 16;
private const int CacheSize = 1 << CacheSizeBits;
private const int CacheMask = CacheSize - 1;
private struct Entry
{
public readonly int hash;
public readonly GreenNode node;
internal Entry(int hash, GreenNode node)
{
this.hash = hash;
this.node = node;
}
}
private static readonly Entry[] s_cache = new Entry[CacheSize];
internal static void AddNode(GreenNode node, int hash)
{
if (AllChildrenInCache(node) && !node.IsMissing)
{
GreenStats.ItemAdded();
Debug.Assert(node.GetCacheHash() == hash);
var idx = hash & CacheMask;
s_cache[idx] = new Entry(hash, node);
}
}
private static bool CanBeCached(GreenNode child1)
{
return child1 == null || child1.IsCacheable;
}
private static bool CanBeCached(GreenNode child1, GreenNode child2)
{
return CanBeCached(child1) && CanBeCached(child2);
}
private static bool CanBeCached(GreenNode child1, GreenNode child2, GreenNode child3)
{
return CanBeCached(child1) && CanBeCached(child2) && CanBeCached(child3);
}
private static bool ChildInCache(GreenNode child)
{
// for the purpose of this function consider that
// null nodes, tokens and trivias are cached somewhere else.
// TODO: should use slotCount
if (child == null || child.SlotCount == 0) return true;
int hash = child.GetCacheHash();
int idx = hash & CacheMask;
return s_cache[idx].node == child;
}
private static bool AllChildrenInCache(GreenNode node)
{
// TODO: should use slotCount
var cnt = node.SlotCount;
for (int i = 0; i < cnt; i++)
{
if (!ChildInCache((GreenNode)node.GetSlot(i)))
{
return false;
}
}
return true;
}
internal static GreenNode TryGetNode(int kind, GreenNode child1, out int hash)
{
return TryGetNode(kind, child1, GetFlags(), out hash);
}
internal static GreenNode TryGetNode(int kind, GreenNode child1, SyntaxFactoryContext context, out int hash)
{
return TryGetNode(kind, child1, GetFlags(context), out hash);
}
private static GreenNode TryGetNode(int kind, GreenNode child1, GreenNode.NodeFlags flags, out int hash)
{
if (CanBeCached(child1))
{
GreenStats.ItemCacheable();
int h = hash = GetCacheHash(kind, flags, child1);
int idx = h & CacheMask;
var e = s_cache[idx];
if (e.hash == h && e.node != null && e.node.IsCacheEquivalent(kind, flags, child1))
{
GreenStats.CacheHit();
return e.node;
}
}
else
{
hash = -1;
}
return null;
}
internal static GreenNode TryGetNode(int kind, GreenNode child1, GreenNode child2, out int hash)
{
return TryGetNode(kind, child1, child2, GetFlags(), out hash);
}
internal static GreenNode TryGetNode(int kind, GreenNode child1, GreenNode child2, SyntaxFactoryContext context, out int hash)
{
return TryGetNode(kind, child1, child2, GetFlags(context), out hash);
}
private static GreenNode TryGetNode(int kind, GreenNode child1, GreenNode child2, GreenNode.NodeFlags flags, out int hash)
{
if (CanBeCached(child1, child2))
{
GreenStats.ItemCacheable();
int h = hash = GetCacheHash(kind, flags, child1, child2);
int idx = h & CacheMask;
var e = s_cache[idx];
if (e.hash == h && e.node != null && e.node.IsCacheEquivalent(kind, flags, child1, child2))
{
GreenStats.CacheHit();
return e.node;
}
}
else
{
hash = -1;
}
return null;
}
internal static GreenNode TryGetNode(int kind, GreenNode child1, GreenNode child2, GreenNode child3, out int hash)
{
return TryGetNode(kind, child1, child2, child3, GetFlags(), out hash);
}
internal static GreenNode TryGetNode(int kind, GreenNode child1, GreenNode child2, GreenNode child3, SyntaxFactoryContext context, out int hash)
{
return TryGetNode(kind, child1, child2, child3, GetFlags(context), out hash);
}
private static GreenNode TryGetNode(int kind, GreenNode child1, GreenNode child2, GreenNode child3, GreenNode.NodeFlags flags, out int hash)
{
if (CanBeCached(child1, child2, child3))
{
GreenStats.ItemCacheable();
int h = hash = GetCacheHash(kind, flags, child1, child2, child3);
int idx = h & CacheMask;
var e = s_cache[idx];
if (e.hash == h && e.node != null && e.node.IsCacheEquivalent(kind, flags, child1, child2, child3))
{
GreenStats.CacheHit();
return e.node;
}
}
else
{
hash = -1;
}
return null;
}
private static GreenNode.NodeFlags GetFlags()
{
return GreenNode.NodeFlags.IsNotMissing;
}
private static GreenNode.NodeFlags GetFlags(SyntaxFactoryContext context)
{
GreenNode.NodeFlags flags = GetFlags();
flags = CSharpSyntaxNode.SetFactoryContext(flags, context);
return flags;
}
private static int GetCacheHash(int kind, GreenNode.NodeFlags flags, GreenNode child1)
{
int code = (int)(flags) ^ kind;
// the only child is never null
code = Hash.Combine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(child1), code);
// ensure nonnegative hash
return code & Int32.MaxValue;
}
private static int GetCacheHash(int kind, GreenNode.NodeFlags flags, GreenNode child1, GreenNode child2)
{
int code = (int)(flags) ^ kind;
if (child1 != null)
{
code = Hash.Combine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(child1), code);
}
if (child2 != null)
{
code = Hash.Combine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(child2), code);
}
// ensure nonnegative hash
return code & Int32.MaxValue;
}
private static int GetCacheHash(int kind, GreenNode.NodeFlags flags, GreenNode child1, GreenNode child2, GreenNode child3)
{
int code = (int)(flags) ^ kind;
if (child1 != null)
{
code = Hash.Combine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(child1), code);
}
if (child2 != null)
{
code = Hash.Combine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(child2), code);
}
if (child3 != null)
{
code = Hash.Combine(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(child3), code);
}
// ensure nonnegative hash
return code & Int32.MaxValue;
}
}
}