|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using Microsoft.CodeAnalysis.Internal.Log; |
| 7 | +using Roslyn.Utilities; |
| 8 | + |
| 9 | +namespace Microsoft.CodeAnalysis.Remote; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// LRU cache of checksum+solution pairs. Used to keep track of the last few solutions the remote server knows about, |
| 13 | +/// helping to avoid unnecessary syncs/recreations of those solutions while many requests are coming into the server. |
| 14 | +/// Not threadsafe. Only use while under a lock. |
| 15 | +/// </summary> |
| 16 | +internal sealed class RemoteSolutionCache<TChecksum, TSolution> |
| 17 | + where TChecksum : struct |
| 18 | + where TSolution : class |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// The max number of solution instances we'll hold onto at a time. |
| 22 | + /// </summary> |
| 23 | + private readonly int _maxCapacity; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The total history kept. Used to record telemetry about how useful it would be to increase the max capacity. |
| 27 | + /// </summary> |
| 28 | + private readonly int _totalHistory; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Keep track of what index we found find a checksum at in the history. This will help us tell both if the cache |
| 32 | + /// is too large, or if it's too small. |
| 33 | + /// </summary> |
| 34 | + private readonly HistogramLogAggregator<int>.HistogramCounter _cacheHitIndexHistogram; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// The number of times we successfully found a solution. When this happens we'll increment <see |
| 38 | + /// cref="_cacheHitIndexHistogram"/>. |
| 39 | + /// </summary> |
| 40 | + private int _cacheHits; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// The number of times we failed to find a solution, but could have found it if we cached more items (up to |
| 44 | + /// TotalHistory). When this happens we'll increment <see cref="_cacheHitIndexHistogram"/>. |
| 45 | + /// </summary> |
| 46 | + private int _cacheMissesInHistory; |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// The number of times we failed to find a solution, and would not have found it even if we didn't cache more items. |
| 50 | + /// </summary> |
| 51 | + private int _cacheMissesNotInHistory; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// The list of checksum+solution pairs. Note: only the first <see cref="_maxCapacity"/> items will actually point |
| 55 | + /// at a non-null solution. The ones after that will point at <see langword="null"/>. We store both so that we can |
| 56 | + /// collect useful telemetry on how much benefit we would get by having a larger history. |
| 57 | + /// </summary> |
| 58 | + private readonly LinkedList<CacheNode> _cacheNodes = new(); |
| 59 | + |
| 60 | + public RemoteSolutionCache(int maxCapacity = 4, int totalHistory = 16) |
| 61 | + { |
| 62 | + _maxCapacity = maxCapacity; |
| 63 | + _totalHistory = totalHistory; |
| 64 | + _cacheHitIndexHistogram = new(bucketSize: 1, maxBucketValue: int.MaxValue, bucketCount: _totalHistory + 1); |
| 65 | + } |
| 66 | + |
| 67 | + private void FindAndMoveNodeToFront(TChecksum checksum) |
| 68 | + { |
| 69 | + var index = 0; |
| 70 | + for (var current = _cacheNodes.First; current != null; current = current.Next, index++) |
| 71 | + { |
| 72 | + if (current.Value.Checksum.Equals(checksum)) |
| 73 | + { |
| 74 | + // Found the item. |
| 75 | + if (index > 0) |
| 76 | + { |
| 77 | + // If it's not already at the front, move it there. |
| 78 | + _cacheNodes.Remove(current); |
| 79 | + _cacheNodes.AddFirst(current); |
| 80 | + } |
| 81 | + |
| 82 | + return; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Didn't find the item at all. Just add to the front. |
| 87 | + _cacheNodes.AddFirst(new CacheNode(checksum)); |
| 88 | + } |
| 89 | + |
| 90 | + public void Add(TChecksum checksum, TSolution solution) |
| 91 | + { |
| 92 | + Contract.ThrowIfTrue(_cacheNodes.Count > _totalHistory); |
| 93 | + |
| 94 | + FindAndMoveNodeToFront(checksum); |
| 95 | + |
| 96 | + Contract.ThrowIfTrue(_cacheNodes.Count > _totalHistory + 1); |
| 97 | + Contract.ThrowIfNull(_cacheNodes.First); |
| 98 | + Contract.ThrowIfFalse(_cacheNodes.First.Value.Checksum.Equals(checksum)); |
| 99 | + |
| 100 | + // Ensure we're holding onto the solution. |
| 101 | + _cacheNodes.First.Value.Solution = solution; |
| 102 | + |
| 103 | + // Now, if our history is too long, remove the last item. |
| 104 | + if (_cacheNodes.Count == _totalHistory + 1) |
| 105 | + _cacheNodes.RemoveLast(); |
| 106 | + |
| 107 | + // Finally, ensure that only the first `MaxCapacity` are pointing at solutions and the rest are not. |
| 108 | + var index = 0; |
| 109 | + for (var current = _cacheNodes.First; current != null; current = current.Next, index++) |
| 110 | + { |
| 111 | + if (index >= _maxCapacity) |
| 112 | + { |
| 113 | + current.Value.Solution = null; |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public TSolution? Find(TChecksum checksum) |
| 120 | + { |
| 121 | + // Note: we intentionally do not move an item when we find it. That's because our caller will always call 'Add' |
| 122 | + // with the found solution afterwards. This will ensure that that solution makes it to the front of the line. |
| 123 | + var index = 0; |
| 124 | + for (var current = _cacheNodes.First; current != null; current = current.Next, index++) |
| 125 | + { |
| 126 | + if (current.Value.Checksum.Equals(checksum)) |
| 127 | + { |
| 128 | + // Found it! |
| 129 | + if (current.Value.Solution is null) |
| 130 | + { |
| 131 | + // Track that we would have been able to return this if our history was longer. |
| 132 | + _cacheMissesInHistory++; |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + // Success! |
| 137 | + _cacheHits++; |
| 138 | + } |
| 139 | + |
| 140 | + _cacheHitIndexHistogram.IncreaseCount(index); |
| 141 | + return current.Value.Solution; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Couldn't find it at all, even in the history. |
| 146 | + _cacheMissesNotInHistory++; |
| 147 | + return null; |
| 148 | + } |
| 149 | + |
| 150 | + public void ReportTelemetry() |
| 151 | + { |
| 152 | + Logger.Log(FunctionId.RemoteWorkspace_SolutionCachingStatistics, KeyValueLogMessage.Create(m => |
| 153 | + { |
| 154 | + m.Add(nameof(_cacheHits), _cacheHits); |
| 155 | + m.Add(nameof(_cacheMissesInHistory), _cacheMissesInHistory); |
| 156 | + m.Add(nameof(_cacheMissesNotInHistory), _cacheMissesNotInHistory); |
| 157 | + _cacheHitIndexHistogram.WriteTelemetryPropertiesTo(m, prefix: nameof(_cacheHitIndexHistogram)); |
| 158 | + })); |
| 159 | + } |
| 160 | + |
| 161 | + private sealed class CacheNode(TChecksum checksum) |
| 162 | + { |
| 163 | + public readonly TChecksum Checksum = checksum; |
| 164 | + public TSolution? Solution; |
| 165 | + } |
| 166 | +} |
0 commit comments