Skip to content
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

PoC: Compiler caches #18190

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft

Conversation

vzarytovskii
Copy link
Member

@vzarytovskii vzarytovskii commented Dec 30, 2024

This is a very first naive draft of universal cache for compiler internals.

Currently it's very dumb straightforward - have an underlying concurrent dictionary, evict on every insert.

Things it lacks:

  • I pretty much YOLO'd the eviction in about 30 minutes or so, and I don't expect it to be efficient.
  • Weakness handling.
  • Fair concurrent access (now everything is race-y, which is not inherently bad).
  • Shortcuts for eviction.
  • Eviction tasks+cancellation (currently the default strategy is to block for each eviction).
  • It's not the most memory-efficient soution there is, pretty much same efficiency as ConcurrentDictionaries we use currently.
  • Probably something else i've forgotten.

It currently solves the only issue versus using ConcurrentDictionarry - eviction. It can evict things, though not in the most efficient way possible.

Personally, I think, that first it has to be at least as good as ConcurrentDictionary+eviction, so we can enable some caching for tooling as well as for compiler.

Copy link
Contributor

❗ Release notes required

@vzarytovskii,

Caution

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:

* <Informative description>. ([PR #XXXXX](https://github.com/dotnet/fsharp/pull/XXXXX))

See examples in the files, listed in the table below or in th full documentation at https://fsharp.github.io/fsharp-compiler-docs/release-notes/About.html.

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

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/9.0.200.md No release notes found or release notes format is not correct

@vzarytovskii
Copy link
Member Author

I have added a CWT-based cache as well (optional, off by default), but it really needs more testing to make sure nothing is leaking.

while not cts.Token.IsCancellationRequested do
if this.GetEvictCount() > 0 then
this.TryEvictItems ()
// do! Task.Delay(100, cts.Token)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exponential backoff based on number of evicted items?

Did I evict 0 - can afford a longer delay.
A lot of stuff evicted - try again shortly after.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was my idea as well

if options.Weak then
let weak = Weak<'Key>(key)
this.ConditionalWeakTable.TryAdd(key :> obj, weak) |> ignore
weak.Collected.Add(this.RemoveCollected)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not be collected, because there is another reference to key from the inner storage of this.Store

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see them getting collected if I trigger the GC. I.e. I see finalizer getting called on them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, disregard, I thought I've pushed updated version which wraps key in a weak reference. Will push it on Monday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New
Development

Successfully merging this pull request may close these issues.

2 participants