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

can we inline delays and then read the cached value instead of looking up from the bindings map? #577

Closed
borkdude opened this issue Jun 24, 2021 · 0 comments

Comments

@borkdude
Copy link
Collaborator

borkdude commented Jun 24, 2021

Inlining bindings doesn't work, as their values are usually not constants (see #485). Instead we are looking up values at runtime from the bindings map. But this is an expensive operation. Could we instead inline delays or promises so when the value is needed, it's a direct access to a delay or promise instead of a map lookup?
We may have to switch to SSA (unique names per binding regardless of their surface form) which is already done in the mutable-bindings map, as each delay/promise must be already created at analysis time and shadowed bindings would mean overwriting it.
For storing values we could use a delay, promise or maybe simply a volatile!.

Note that special attention is needed with respect to concurrency, if we are going to reuse and set a volatile! multiple times in a loop. We must preserve the following behavior:

(def log-obj (Object.))
(defn log [& strs]
  (locking log-obj
    (apply println strs)))
(loop [x 1
       future? true]
  (when future?
    (future (log :future x)
            (Thread/sleep 1000)
            (recur)))
  (log :loop x)
  (Thread/sleep 1000)
  (recur (inc x) false))
:future 1
:loop 1
:future 1
:loop 2
:loop 3
:future 1
:future 1
:loop 4

This might be relevant too: #416 (comment)

Also see step C and D here, also see code.

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

No branches or pull requests

1 participant