taoensso / tempura

Simple text localization library for Clojure/Script
https://www.taoensso.com/tempura
Eclipse Public License 1.0
260 stars 16 forks source link

Update dict value result in performance drop #18

Closed kawas44 closed 2 years ago

kawas44 commented 6 years ago

Hello,

I am using tempura with a dictionary hash-map stored in an atom. An during my application lifetime it happens that I update the atom value.

Everything works fine, but I observe a drop in performance:

I have tried to invalidate the memoized cache using special args but it does nothing. (See code below)

Code example I used to bench tr calls:

;; assume existence of those vars:
;;   make-tempura-dict: function to load a new edn dictionary

;; pick n keys from the dict for successive benchs in a row
(defn pick-keys [dict lang n]
  (take n (random-sample 0.5 (keys (get dict lang)))))

;; dummy function to bench something
(defn sum-all-trad-count [tr langs ks]
  (reduce (fn [acc-a l]
            (reduce (fn [acc-b k]
                      (+ acc-b (count (tr [l] [k]))))
                    acc-a
                    ks))
          0
          langs))

(defn bench-tr [langs n]
  (let [dict (atom (make-tempura-dict))
        ks (pick-keys @dict :en n)
        tr (partial tempura/tr {:dict @dict :cache-dict? true :cache-locales? false})
        _ (cr/quick-bench
            (sum-all-trad-count tr langs ks))

        ;; try to clear tr cache
        _ (tr [:mem/del :mem/all] [:mem/del :mem/all])

        new-dict-value (make-tempura-dict)
        _ (swap! dict (constantly new-dict-value))
        tr (partial tempura/tr {:dict @dict :cache-dict? true :cache-locales? false})
        _ (cr/quick-bench
            (sum-all-trad-count tr langs ks))]
    (println "done.")))

;; run benchmarks
#_ (bench-tr [:en :es :fr] 10)

Do you have any insights on how to use an updated dict value and keep a good performance ?

Thanks

ptaoussanis commented 2 years ago

Closing old question as part of issue triage, but please feel free to reopen if this is still a concern. In which case a ready-to-run example project would be super helpful - thanks!