Fix StackOverflow error on large inputs

Use non-lazy collections.
main
Eric Ihli 3 years ago
parent fb46bc5a27
commit 2d684be874

@ -233,7 +233,7 @@
current-depth (count (first current-node))] current-depth (count (first current-node))]
(cond (cond
(empty? nodes) (empty? nodes)
(let [child-index (first child-indexes) (let [child-index (last child-indexes)
child-index-baos (ByteArrayOutputStream.) child-index-baos (ByteArrayOutputStream.)
_ (->> child-index _ (->> child-index
(run! (run!
@ -267,9 +267,9 @@
;; Gone up from depth to a parent. ;; Gone up from depth to a parent.
;; Process index of children. ;; Process index of children.
(> previous-depth current-depth) (> previous-depth current-depth)
(do (let [[k v] (first nodes) (let [[k v] (first nodes)
value (value-encode-fn v) value (value-encode-fn v)
child-index (first child-indexes) child-index (last child-indexes)
child-index-baos (ByteArrayOutputStream.) child-index-baos (ByteArrayOutputStream.)
_ (->> child-index _ (->> child-index
(run! (run!
@ -283,7 +283,7 @@
(- current-offset offset)))))) (- current-offset offset))))))
child-index-byte-array (.toByteArray child-index-baos) child-index-byte-array (.toByteArray child-index-baos)
size-of-child-index (encoding/encode (count child-index-byte-array)) size-of-child-index (encoding/encode (count child-index-byte-array))
current-index (second child-indexes)] current-index (last (pop child-indexes))]
(.write baos value) (.write baos value)
(.write baos size-of-child-index) (.write baos size-of-child-index)
(.write baos child-index-byte-array) (.write baos child-index-byte-array)
@ -293,20 +293,19 @@
(count size-of-child-index) (count size-of-child-index)
(count child-index-byte-array)) (count child-index-byte-array))
current-depth current-depth
(cons (conj current-index (conj (pop (pop child-indexes))
(conj current-index
[(last k) [(last k)
current-offset]) current-offset]))))
(drop 2 child-indexes)))))
;; Down or even in depth to children ;; Down or even in depth to children
;; Start keeping track of new children index ;; Start keeping track of new children index
:else :else
(do (let [[k v] (first nodes) (let [[k v] (first nodes)
value (value-encode-fn v) value (value-encode-fn v)
size-of-child-index (encoding/encode 0) size-of-child-index (encoding/encode 0)
child-indexes (concat (repeat (- current-depth previous-depth) []) child-indexes (into child-indexes
child-indexes) (vec (repeat (- current-depth previous-depth) [])))
current-child-index (first child-indexes)] current-child-index (last child-indexes)]
(.write baos value) (.write baos value)
(.write baos size-of-child-index) (.write baos size-of-child-index)
(recur (rest nodes) (recur (rest nodes)
@ -314,10 +313,10 @@
(count value) (count value)
(count size-of-child-index)) (count size-of-child-index))
current-depth current-depth
(cons (conj current-child-index (conj (pop child-indexes)
(conj current-child-index
[(last k) [(last k)
current-offset]) current-offset])))))))))
(rest child-indexes))))))))))
;; TODO: Shared "save" interface for Trie? ;; TODO: Shared "save" interface for Trie?
(defn save-tightly-packed-trie-to-file (defn save-tightly-packed-trie-to-file

Loading…
Cancel
Save