-
Notifications
You must be signed in to change notification settings - Fork 824
Use new Cache api in various places #18872
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
base: main
Are you sure you want to change the base?
Conversation
❗ Release notes requiredCaution No release notes found for the changed paths (see table below). Please make sure to add an entry with an informative description of the change as well as link to this pull request, issue and language suggestion if applicable. Release notes for this repository are based on Keep A Changelog format. The following format is recommended for this repository:
If you believe that release notes are not necessary for this PR, please add NO_RELEASE_NOTES label to the pull request. You can open this PR in browser to add release notes: open in github.dev
|
src/Compiler/Optimize/Optimizer.fs
Outdated
// Is it quadratic or quasi-quadratic? | ||
if ValueIsUsedOrHasEffect cenv (fun () -> (freeInExpr (CollectLocalsWithStackGuard()) bodyR).FreeLocals) (bindR, bindingInfo) then | ||
let collect expr = (freeInExpr (CollectLocalsWithStackGuard()) expr).FreeLocals | ||
if ValueIsUsedOrHasEffect cenv (fun () -> (getFreeLocalsCache cenv).GetOrAdd(bodyR, collect)) (bindR, bindingInfo) then |
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.
This gives a modest 20% hit ratio in tests. May or may not help somewhat IRL but needs benchmarking.
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.
Looking at Optimization times when building FCS I don't see any speedup.
/// Usage: | ||
/// let getValueFor = WeakMap.getOrCreate (fun () -> expensiveInit()) | ||
/// let v = getValueFor someKey | ||
let getOrCreate valueFactory = |
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.
Allows to quickly and and-hoc attach a cache instance in various places. Currently we thread various caches along with context objects / records which is not that convenient when testing things out.
let getArgInfoCache = | ||
let options = Caches.CacheOptions.getDefault() |> Caches.CacheOptions.withNoEviction | ||
let factory _ = new Caches.Cache<_, ArgReprInfo>(options, "argInfoCache") | ||
WeakMap.getOrCreate factory |
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.
Are these caches going to be cleared when asking FCS to clear its caches?
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.
Currently not, I think since they're weakly attached to higher lever stuff, they'll just get GC'd with it. I captured some telemetry and the caches get disposed quite often during the test run.
Ahh, there's that memory leak in vs tests, again 😔 |
env.eCachedImplicitYieldExpressions.Add(synExpr1.Range, (synExpr1, expr1Ty, cachedExpr)) | ||
try TcExpr cenv overallTy env tpenv otherExpr | ||
finally env.eCachedImplicitYieldExpressions.Remove synExpr1.Range | ||
(getImplicitYieldExpressionsCache cenv).AddOrUpdate(synExpr1, (expr1Ty, cachedExpr)) |
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.
The semantics w.r.t. to error handling is different now, aren't they?
Also it feels like the previous version had a race condition (FindAll
and later Add
), possibly the Multi
map was chosen to workaround it by storing a list and not a single value?
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.
Previous version had some concurrency problems, because Vlad at some point added ConcurrentDictionary
as HashMultiMap
backing store to fix them.
Semantics are different. Originally this just always removed the value in finally
. I have to revisit this, I remember the change made sense to me but I forgot why, oh lol. I guess my thinking was eviction will be sufficient here. Now I also notice this was attached to env
, not cenv
.
This is not really tested in the test suite but there is a benchmark. I'll run it later to see if this still works.
I'm trying to identify places that could benefit from memoization or better caching. Here are some suggestions by Copilot:
I also notice the existing caching in |
With aditional type relations caches there seem to be a slight speedup against main:
|
implicitYieldExpressions, argInfoCache, typeDefinitelySubsumesNoCoerceCache, typeFeasibleEquivCache