Fix StackOverflow error on large inputs

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

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

Loading…
Cancel
Save