Serializable: simplify amortized cost impl.

This commit is contained in:
Peter Taoussanis 2020-09-12 16:59:39 +02:00
parent d77381378d
commit 23400e7735

View file

@ -416,7 +416,7 @@
(enc/defonce ^{:dynamic true :doc doc} *thaw-serializable-allowlist* (init-allowlist :thaw default-thaw-serializable-allowlist))) (enc/defonce ^{:dynamic true :doc doc} *thaw-serializable-allowlist* (init-allowlist :thaw default-thaw-serializable-allowlist)))
(let [nmax 1000 (let [nmax 1000
gc-rate (/ 1.0 16000) ngc 16000
state_ (atom {}) ; {<class-name> <frequency>} state_ (atom {}) ; {<class-name> <frequency>}
lock_ (atom nil) ; ?promise lock_ (atom nil) ; ?promise
trim (fn [nmax state] trim (fn [nmax state]
@ -472,13 +472,12 @@
;; Garbage collection (GC): may be serializing anonymous classes, etc. ;; Garbage collection (GC): may be serializing anonymous classes, etc.
;; so input domain could be infinite ;; so input domain could be infinite
(when (> n nmax) ; Too many classes recorded, uncommon (when (> n ngc) ; Too many classes recorded, uncommon
(when (< (java.lang.Math/random) gc-rate) ; Amortize GC cost (let [p (promise)]
(let [p (promise)] (when (compare-and-set! lock_ nil p) ; Acquired GC lock
(when (compare-and-set! lock_ nil p) ; Acquired gc lock (try
(try (do (reset! state_ (trim nmax @state_))) ; GC state
(do (reset! state_ (trim nmax @state_))) ; GC state (finally (reset! lock_ nil) (deliver p nil))))))
(finally (reset! lock_ nil) (deliver p nil)))))))
n)) n))