Skip to content

Commit b8ba293

Browse files
committed
simplify
1 parent 59cb94e commit b8ba293

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

src/Compiler/Utilities/Caches.fs

+18-26
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ open System
55
open System.Collections.Generic
66
open System.Collections.Concurrent
77
open System.Threading
8-
open System.Threading.Tasks
98
open System.Diagnostics
109
open System.Diagnostics.Metrics
1110

12-
open FSharp.Compiler.Diagnostics
13-
1411
[<Struct; RequireQualifiedAccess; NoComparison; NoEquality>]
1512
type CacheOptions =
1613
{
@@ -54,25 +51,25 @@ type CachedEntity<'Key, 'Value> =
5451

5552
override this.ToString() = $"{this.Key}"
5653

57-
module CacheMetrics =
58-
let meter = new Meter("FSharp.Compiler.Cache")
59-
54+
// Currently the Cache itself exposes Metrics.Counters that count raw cache events: hits, misses, evictions etc.
55+
// This class observes those counters and keeps a snapshot of readings. For now this is used only to print cache stats in debug mode.
56+
// TODO: We could add some System.Diagnostics.Metrics.Gauge instruments to this class, to get computed stats also exposed as metrics.
6057
type CacheMetrics(cacheId) =
58+
static let meter = new Meter("FSharp.Compiler.Cache")
6159

6260
static let instrumentedCaches = ConcurrentDictionary<string, CacheMetrics>()
6361

6462
let readings = ConcurrentDictionary<string, int64 ref>()
6563

6664
#if DEBUG
67-
let listener =
68-
new MeterListener(
69-
InstrumentPublished =
70-
fun i l ->
71-
if i.Meter = CacheMetrics.meter && i.Description = cacheId then
72-
l.EnableMeasurementEvents(i)
73-
)
65+
let listener = new MeterListener()
7466

7567
do
68+
listener.InstrumentPublished <-
69+
fun i l ->
70+
if i.Meter = meter && i.Description = cacheId then
71+
l.EnableMeasurementEvents(i)
72+
7673
listener.SetMeasurementEventCallback<int64>(fun k v _ _ -> Interlocked.Add(readings.GetOrAdd(k.Name, ref 0L), v) |> ignore)
7774
listener.Start()
7875

@@ -83,6 +80,8 @@ type CacheMetrics(cacheId) =
8380

8481
member val CacheId = cacheId
8582

83+
static member val Meter = meter
84+
8685
member val RecentStats = "-" with get, set
8786

8887
member this.TryUpdateStats(clearCounts) =
@@ -139,7 +138,7 @@ type EntityPool<'Key, 'Value>(maximumCapacity, cacheId) =
139138
let mutable created = 0
140139

141140
let overCapacity =
142-
CacheMetrics.meter.CreateCounter<int64>("over-capacity", "count", cacheId)
141+
CacheMetrics.Meter.CreateCounter<int64>("over-capacity", "count", cacheId)
143142

144143
member _.Acquire(key, value) =
145144
match pool.TryTake() with
@@ -163,14 +162,14 @@ type Cache<'Key, 'Value when 'Key: not null and 'Key: equality>
163162

164163
let instanceId = defaultArg name $"cache-{Interlocked.Increment(&cacheId)}"
165164

166-
let hits = CacheMetrics.meter.CreateCounter<int64>("hits", "count", instanceId)
167-
let misses = CacheMetrics.meter.CreateCounter<int64>("misses", "count", instanceId)
165+
let hits = CacheMetrics.Meter.CreateCounter<int64>("hits", "count", instanceId)
166+
let misses = CacheMetrics.Meter.CreateCounter<int64>("misses", "count", instanceId)
168167

169168
let evictions =
170-
CacheMetrics.meter.CreateCounter<int64>("evictions", "count", instanceId)
169+
CacheMetrics.Meter.CreateCounter<int64>("evictions", "count", instanceId)
171170

172171
let evictionFails =
173-
CacheMetrics.meter.CreateCounter<int64>("eviction-fails", "count", instanceId)
172+
CacheMetrics.Meter.CreateCounter<int64>("eviction-fails", "count", instanceId)
174173

175174
let pool = EntityPool<'Key, 'Value>(capacity, instanceId)
176175

@@ -199,6 +198,7 @@ type Cache<'Key, 'Value when 'Key: not null and 'Key: equality>
199198
evictionQueue.Remove(node)
200199
evictionQueue.AddLast(node)
201200

201+
// If items count exceeds this, evictions ensue.
202202
let targetCount =
203203
options.MaximumCapacity
204204
- int (float options.MaximumCapacity * float options.PercentageToEvict / 100.0)
@@ -264,14 +264,6 @@ type Cache<'Key, 'Value when 'Key: not null and 'Key: equality>
264264
pool.Reclaim(cachedEntity)
265265
false
266266

267-
member this.GetOrCreate(key: 'Key, valueFactory: 'Key -> 'Value) =
268-
match this.TryGetValue(key) with
269-
| true, value -> value
270-
| _ ->
271-
let value = valueFactory key
272-
this.TryAdd(key, value) |> ignore
273-
value
274-
275267
interface IDisposable with
276268
member this.Dispose() =
277269
store.Clear()

src/Compiler/Utilities/Caches.fsi

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ type internal Cache<'Key, 'Value when 'Key: not null and 'Key: equality> =
2323
new: options: CacheOptions * capacity: int * cts: CancellationTokenSource * ?name: string -> Cache<'Key, 'Value>
2424
member TryGetValue: key: 'Key * value: outref<'Value> -> bool
2525
member TryAdd: key: 'Key * value: 'Value -> bool
26-
member GetOrCreate: key: 'Key * valueFactory: ('Key -> 'Value) -> 'Value
2726
member Dispose: unit -> unit
2827

2928
interface IDisposable

0 commit comments

Comments
 (0)