-
Notifications
You must be signed in to change notification settings - Fork 90
Simplify mkCacheInt32/mkCacheGeneric to use ConcurrentDictionary #486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4586,29 +4586,25 @@ module internal AssemblyReader = | |
|
|
||
| let mkCacheInt32 lowMem _infile _nm _sz = | ||
| if lowMem then (fun f x -> f x) else | ||
| let cache = Dictionary<int32, _>() | ||
| let syncObj = obj() | ||
| let cache = ConcurrentDictionary<int32, _>() | ||
| fun f (idx:int32) -> | ||
| lock syncObj (fun () -> | ||
| let mutable res = Unchecked.defaultof<_> | ||
| if cache.TryGetValue(idx, &res) then res | ||
| else | ||
| let v = f idx | ||
| cache.[idx] <- v | ||
| v) | ||
| match cache.TryGetValue idx with | ||
| | true, v -> v | ||
| | false, _ -> | ||
| let v = f idx | ||
| cache.TryAdd(idx, v) |> ignore | ||
| cache.[idx] | ||
|
|
||
| let mkCacheGeneric lowMem _inbase _nm _sz = | ||
| if lowMem then (fun f x -> f x) else | ||
| let cache = Dictionary<'T, _>() | ||
| let syncObj = obj() | ||
| let cache = ConcurrentDictionary<'T, _>() | ||
| fun f (idx :'T) -> | ||
| lock syncObj (fun () -> | ||
| match cache.TryGetValue idx with | ||
| | true, cached -> cached | ||
| | false, _ -> | ||
| let v = f idx | ||
| cache.[idx] <- v | ||
| v) | ||
| match cache.TryGetValue idx with | ||
| | true, v -> v | ||
| | false, _ -> | ||
| let v = f idx | ||
| cache.TryAdd(idx, v) |> ignore | ||
| cache.[idx] | ||
|
Comment on lines
+4602
to
+4607
|
||
|
|
||
| let seekFindRow numRows rowChooser = | ||
| let mutable i = 1 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cache.TryGetValue idxuses the F# out-arg-to-tuple translation, which allocates a tuple on every cache access (including hits). Since this cache is likely hot, consider using the byref overload (let mutable res = Unchecked.defaultof<_>; if cache.TryGetValue(idx, &res) then res else ...) to avoid per-call allocations. Also, afterTryAddyou can returnvdirectly when the add succeeds to avoid a second dictionary lookup.