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))] 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,57 +267,56 @@
;; 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!
(fn [[key offset]] (fn [[key offset]]
(.write (.write
child-index-baos child-index-baos
(encoding/encode-key-to-tightly-packed-trie-index key)) (encoding/encode-key-to-tightly-packed-trie-index key))
(.write (.write
child-index-baos child-index-baos
(encoding/encode-offset-to-tightly-packed-trie-index (encoding/encode-offset-to-tightly-packed-trie-index
(- 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)
(recur (rest nodes) (recur (rest nodes)
(+ current-offset (+ current-offset
(count value) (count value)
(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))
[(last k) (conj current-index
current-offset]) [(last k)
(drop 2 child-indexes))))) current-offset]))))
;; 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)
(+ current-offset (+ current-offset
(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)
[(last k) (conj current-child-index
current-offset]) [(last k)
(rest child-indexes)))))))))) current-offset])))))))))
;; 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