Skip to content

Commit

Permalink
Merge pull request #17 from nubank/dont-run-leafs-twice
Browse files Browse the repository at this point in the history
We have a tool to run a body only once, use it
  • Loading branch information
aredington authored Dec 5, 2023
2 parents fffe430 + 40f9256 commit 8da600d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.14.1 / 2023-12-05
- Fix bug that allowed a leaf body to be run multiple times in engines which used the lazy environment.

## 1.14.0 / 2023-11-30
- Fix bug preventing nil values to the passed through channels on core-async applicative engine
- Create a blocking tag that allows leafs and sequences to be tagged with blocking to signal that they contain a blocking IO or other expensive blocking op. Engines can choose to optimize runs with this info
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject dev.nu/nodely "1.14.0"
(defproject dev.nu/nodely "1.14.1"
:description "Decoupling data fetching from data dependency declaration"
:url "https://github.com/nubank/nodely"
:license {:name "MIT"}
Expand Down
28 changes: 16 additions & 12 deletions src/nodely/engine/lazy_env.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns nodely.engine.lazy-env)

(deftype LazySchedulingEnvironment [env ref-map eval-fn opts]
(deftype LazySchedulingEnvironment [env delay-map opts]
clojure.lang.ILookup
(valAt [this k]
(or (.valAt this k nil)
Expand All @@ -10,23 +10,27 @@
(throw ex))))
(valAt [this k not-found]
(if (find env k)
(let [first-read @ref-map]
(if (find first-read k)
(get first-read k)
(-> (dosync
(let [second-read @ref-map]
(if-not (find second-read k)
(alter ref-map assoc k (eval-fn env k this opts))
second-read)))
(get k))))
@(get delay-map k)
not-found)))

(defn scheduled-nodes
"Return the current map of nodes to applicative contexts in the
LazySchedulingEnvironment `lazy-env`."
[lazy-env]
@(.-ref-map lazy-env))
(reduce-kv (fn [m k v]
(if (realized? v)
(assoc m k @v)
m))
{}
(.-delay-map lazy-env)))

(defn lazy-env
[env eval-fn opts]
(->LazySchedulingEnvironment env (ref {}) eval-fn opts))
(let [this-promise (promise)
delay-map (reduce-kv (fn [m k _]
(assoc m k (delay (eval-fn env k @this-promise opts))))
{}
env)
lookup (->LazySchedulingEnvironment env delay-map opts)]
(deliver this-promise lookup)
lookup))

0 comments on commit 8da600d

Please sign in to comment.