Skip to content

Commit

Permalink
Serializable: simplify amortized cost impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Sep 12, 2020
1 parent d773813 commit 23400e7
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/taoensso/nippy.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}) ; {<class-name> <frequency>}
lock_ (atom nil) ; ?promise
trim (fn [nmax state]
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 23400e7

Please sign in to comment.