Niko Matsakis
    • 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
    • Invite by email
      Invitee

      This note has no invitees

    • 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
    • Note Insights
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
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
  • Invite by email
    Invitee

    This note has no invitees

  • 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
    # Salsa and Recursive Solver Ideas In salsa, we could change how we handle multithreading and recursion. Currently, we guarantee that a single query instance only executes on one thread at a time. We do this by updating the state of the query (atomically) to "in-progress" and then again to "complete", and tracking cross-thread dependencies. ## Step one: idempotent queries by default In the newer model, we would just have a HashMap that tracks Key -> Value. When executing a query, we would do: 1. Check if there is a Key present and return the Value if so. 2. If not, check if the Query(Key) instance is being computed on the current thread. If so, return a cycle error (but see below). 3. If not, push onto the stack and start computing. 4. Upon completion, store the Key -> Value result into the map -- note that it may also have been stored by some other thread concurrently, but the two results should be equivalent (we can decide which one to keep, doesn't really matter). We are effectively requiring that salsa queries are idempotent (i.e., may be executed multiple times) by default. (This does require us to find a way to contain side-effects like error reporting, but we need that anyway.) ## Required: some way to declare locked queries We do probably want some way to declare a salsa query as "locked" or "at most once". There are two reasons you might do that: * The query is really expensive and you would rather not have two threads executing it (e.g., perhaps type-check in rustc? unclear). * The query may not always produce exactly the same result and if you allow two different results to co-mingle, you can get internal errors and other similar problems (e.g., procedural macros). To accommodate **locked** queries, we would add a **separate** mechaism whereby the query can update its state. i.e., once it is pushed onto the stack, it can also check for other threads. This would basically be the same as the existing logic but separated and (I believe) clearer as a result. I'm going to ignore this for now. ## Fixed point queries Building on the above, we could extend the system with "fixed-point" queries (which also must be idempotent). The idea of a fixed-point query is that, when it is first executed, it has an "intermediate" result R0. We execute the query body once. If there is a recursive call, and the stack consists only of fixed-point queries (or perhaps even only of the same fixed-point query, that would suffice for now I think), then we can return this intermediate result. But we also track the dependency (much as chalk's recursive solver does today). When the fixed point query completes, it will give back a new value R1. If there were recursive calls (i.e., if that result R1 depended on the intermediate result of R0) then we will re-execute the fixed point query. But this time, if there are recursive calls, we will yield R1. eventually this second iteration will complete, yielding R2. If R1 == R2, we are done, otherwise we keep going. Obviously, this could go forever: part of the contract of a fixed point query is that eventually the results will stabilize (reach a fixed point). Furthermore, in terms of dependency tracking, we will accumulate dependencies such that, when we do reach the fixed point, the result depends on all the queries that were accessed over all the iterations. (And all elements in the SCC will have the same dependency set.) ### API possibilities The precise API and interface to fixed-point queries will require some experimentation. I am imagining a few possibilities. One is a callback like ``` salsa_runtime.fixed_point(v0, || ...) ``` that can be invoked. The downside of this is that it ought to be the "only thing" you do in the query implementation, and I'm not sure how to guarantee that exactly, but I'm not sure how important that is. Another is that we tag the query as `#[fixed_point]` (well that may be desirable regardless) and we then require that the result of the query implements `Default` or some such trait. The query can then be invoked as normal but `Default::default` will be used for recusive results on the first round. One final possibility is that we may want to provide the previous result (initially, the default) to the query function as an input. For chalk at least, there is a "merging of results" that happens, and having the previous round's input would be helpful there. I lean towards this rough design at present: * tag query as `#[fixed_point]` * for a fixed-point query, we generate an initial result using `FixedPointDefault::default` * the query function takes `(db, key, value)` where `value` is the result from the previous round ## Refactoring chalk's recursive solver To integrate these changes into chalk's recursive solver will require some work. In particular, it pushes some of the responsibility out from chalk to salsa, which means we need to update chalk's `RustIrDatabase` trait to manage that.

    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