From 23400e77358a615fccd03aec03d759e824b1d44f Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Sat, 12 Sep 2020 16:59:39 +0200 Subject: [PATCH] Serializable: simplify amortized cost impl. --- src/taoensso/nippy.clj | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index 01921f2c..ff759780 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -416,7 +416,7 @@ (enc/defonce ^{:dynamic true :doc doc} *thaw-serializable-allowlist* (init-allowlist :thaw default-thaw-serializable-allowlist))) (let [nmax 1000 - gc-rate (/ 1.0 16000) + ngc 16000 state_ (atom {}) ; { } lock_ (atom nil) ; ?promise trim (fn [nmax state] @@ -472,13 +472,12 @@ ;; Garbage collection (GC): may be serializing anonymous classes, etc. ;; so input domain could be infinite - (when (> n nmax) ; Too many classes recorded, uncommon - (when (< (java.lang.Math/random) gc-rate) ; Amortize GC cost - (let [p (promise)] - (when (compare-and-set! lock_ nil p) ; Acquired gc lock - (try - (do (reset! state_ (trim nmax @state_))) ; GC state - (finally (reset! lock_ nil) (deliver p nil))))))) + (when (> n ngc) ; Too many classes recorded, uncommon + (let [p (promise)] + (when (compare-and-set! lock_ nil p) ; Acquired GC lock + (try + (do (reset! state_ (trim nmax @state_))) ; GC state + (finally (reset! lock_ nil) (deliver p nil)))))) n))