lcnr
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# cache dev guide chapter # Caching in the new trait solver Caching results of the trait solver is necessary for performance. We have to make sure that it is sound and does not cache in unstable query results. Caching is handled by the [`SearchGraph`]. [`SearchGraph`]: TODO ## The global cache At its core, the cache is fairly straightforward. When evaluating a goal, we check whether it's in the global cache. If so, we reuse that entry. If not, we compute the goal and then store its result in the cache. To handle incremental compilation the computation of a goal has to be tracked by the query system. This is done by wrapping the computation with [`Cx::with_cached_task`][with_cached_task]. This creates a new `DepNode` which depends on all queries used inside of this computation. When accessing the global cache we then read this `DepNode`, manually adding a dependency edge to all the queries used: [source][wdn]. Using a global cache entry must have the same effect as actually computing a goal. To make sure that's the case, [`fn update_parent_goal`][upg] is called when we finish an evaluation, but *also* when we use a global cache entry. [`fn update_parent_goal`][upg] lazily updates the state of the highest stack entry. ### Dealing with overflow Hitting the recursion limit is not fatal in the new trait solver but instead simply causes it to return ambiguity: [source][overflow]. Whether we hit the recursion limit can therefore change the result without resulting in a compilation failure. These results are still cached. However, we must be careful when accessing these cached results later as whether we use a global cache entry must not be observable. We do this by storing additional information in the global cache entry. For goals whose evaluation did not reach the recursion limit, we simply store its reached depth: [source][req-depth]. These results can freely be used as long as the current `available_depth` is higher than its `reached_depth`: [source][req-depth-ck]. We then update the reached depth of the current goal to make sure that whether we've used the global cache entry is not observable: [source][update-depth]. We can only use a cached result that overflowed if its stored depth *exactly matches* current available depth. This is necessary because even if a nested goal hit the recursion limit, its parent can still fail to evaluate or succeed by using a different candidate. The cache entry for each goal therefore contains a separate result for each remaining depth: [source][rem-depth].[^1] ## Handling cycles The trait solver handles cycles as explained [in this separate chapter][TODO]. Doing so efficiently and correctly greatly complicates caching. The used terminology is taken directly from that chapter. TODO: cycle, cycle head, cycle root, cycle participant, path, productive, unproductive ### Cycles and query stability We cannot move the result of any cycle participant to the global cache until we've finished evaluating the cycle root. However, even after we've completely evaluated the cycle, we are still forced to discard the result of all participants apart from the root itself. As explained in the [chapter about cycle handling][TODO], the result of a cycle can change depending on which goal is the root: [example][unstable-result-ex]. This means that we must not use a global cache entry if its evaluation would end up depending on a goal already on the stack. We do this by storing the nested goals of each global cache entry and not using that entry if a nested goal is currently on the stack: [source][TODO]. ### The provisional cache We use a separate local cache while inside of cycles. This cache still needs to be sound. However, unlike the global cache it is allowed to impact behavior without resulting in query instability as its usage is deterministic. While possible, globally caching cycle participants would require us to track a lot of additional information. Even more importantly, the provisional cache needs to be able to impact the behavior of the trait solver to enable a necessary optimization. - basic idea: move cycle participants to the provisional cache, storing the highest cycle head and the path from that head to the cycle participant - when popping the cycle head from the stack or when updating its provisional result, discard provisional entries which depend on it - also need to check whether reevaluating a global cache entry would end up depending on a provisional cache entry - issue: hangs with complex auto trait cycles: https://hackmd.io/Vzr-q5h8T_-0dQWeTQb4ig#perfect-derive-auto-traits---oh-my - idea: keep provisional cache entries around even after popping the cycle heads they depend on - this changes the paths of any cycles going through the popped cycle head. can think of it as "rotating part of the proof tree", need to make sure they all remain either inductive or coinductive - only correct when computing the cycle head reached a fixpoint. When failing to do so, we force all provisional cache entries which depend on the cycle head to also be ambiguous. This results in sus ambiguity https://gist.github.com/lcnr/67ded915a8ce43cf6faa7e97eb59e8de#fun-general-tests ## tracking `nested_goals` - tracking all nested goals may be too expensive (actually never tested) - only track all nested goals after encountering the first cycle. Cycle participant are expected to definitely cycle, while the behavior of any goal which depends on a cycle changes depending of the result of the cycle (which depends on the root/provisional cache) - overflow also changes the dependencies of goals, and given that any arbitrary goal could overflow, do we need to track all of the after all? - no. By only using provisional cache entries which encountered overflow while the current goal on the stack is already a member of their cycle (so the caching behavior is deterministic), we can weaken caching to reduce the required tracking. - TODO: could we instead track the required depth of provisional cache entries as well? examples: https://gist.github.com/lcnr/291c3a7e1491ea33c1333c83cb594ca0 ## Fuzzing support The caching implementation is highly involved and supports fuzzing by providing a generic interface via the [Cx][TODO] and [Delegate][TODO] traits. The actual fuzzer is in a separate repository https://github.com/lcnr/search_graph_fuzz. It currently supports testing whether the global cache impacts behavior to avoid query instability, and simpler graphs which simply check the soundness of the cycle behavior to test the provisional cache. <!-- unused--> [`with_anon_task`]: https://github.com/rust-lang/rust/blob/59d4114b2d1aaac9a6dfe770997f2e79ccfd28ab/compiler/rustc_query_system/src/dep_graph/graph.rs#L295 [wdn]: TODO [upg]: TODO [overflow]: TODO [req-depth]: TODO [req-depth-ck]: TODO [rem-depth]: https://github.com/rust-lang/rust/blob/59d4114b2d1aaac9a6dfe770997f2e79ccfd28ab/compiler/rustc_type_ir/src/search_graph/global_cache.rs#L25 [^1]: This is overly restrictive: if all nested goal return the overflow response with some available depth `n`, then their result should be the same for any depths smaller than `n`. We can implement this optimization in the future. [chapter on coinduction]: ./coinduction.md [`provisional_result`]: https://github.com/rust-lang/rust/blob/7606c13961ddc1174b70638e934df0439b7dc515/compiler/rustc_trait_selection/src/solve/search_graph.rs#L57 [initial-prov-result]: https://github.com/rust-lang/rust/blob/7606c13961ddc1174b70638e934df0439b7dc515/compiler/rustc_trait_selection/src/solve/search_graph.rs#L366-L370 [fixpoint]: https://github.com/rust-lang/rust/blob/7606c13961ddc1174b70638e934df0439b7dc515/compiler/rustc_trait_selection/src/solve/search_graph.rs#L425-L446 [^2]: summarizing the relevant [zulip thread] [zulip thread]: https://rust-lang.zulipchat.com/#narrow/stream/364551-t-types.2Ftrait-system-refactor/topic/global.20cache [unstable-result-ex]: https://github.com/rust-lang/rust/blob/7606c13961ddc1174b70638e934df0439b7dc515/tests/ui/traits/next-solver/cycles/coinduction/incompleteness-unstable-result.rs#L4-L16 [cycle-participants]: https://github.com/rust-lang/rust/blob/7606c13961ddc1174b70638e934df0439b7dc515/compiler/rustc_middle/src/traits/solve/cache.rs#L72-L74

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully