From de35dadc99b82b15502e332cbbadf5faee15bbed Mon Sep 17 00:00:00 2001 From: David Walker Date: Mon, 25 Nov 2024 13:03:09 +0000 Subject: [PATCH] Use cached value in derived atom watcher Avoids recalculating the previous value using selector if the cache already contains the correct value. Uses the same code as -deref to establish validity of cache value. --- src/okulary/core.cljs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/okulary/core.cljs b/src/okulary/core.cljs index a103ebb..580c5a9 100644 --- a/src/okulary/core.cljs +++ b/src/okulary/core.cljs @@ -100,11 +100,13 @@ ;; As first step we apply the selector to ;; the new source value. new-value (selector-fn new-source-value) - old-value (selector-fn old-source-value)] + old-value + (if (and (identical? srccache old-source-value) + (not (identical? cache EMPTY))) + cache + (selector-fn old-source-value))] - ;; Store the new source value in the instance; - ;; this is mainly used by the deref, so this is - ;; just a small performance improvement for it. + ;; Store the new source value in the instance cache (set! srccache new-source-value) ;; Cache the new value in the instance.