Skip to content

Commit 0bd54fb

Browse files
[stacktrace] Use a more efficient pmap
1 parent 68bc615 commit 0bd54fb

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/orchard/misc.clj

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns orchard.misc
22
;; These will be added in clojure 1.11:
3-
(:refer-clojure :exclude [update-keys update-vals])
3+
(:refer-clojure :exclude [update-keys update-vals pmap])
44
(:require
55
[clojure.java.io :as io]
66
[clojure.string :as str]
@@ -161,3 +161,22 @@
161161
(fn [& args]
162162
(when resolved-fn
163163
(apply resolved-fn args)))))
164+
165+
(defn- into! [transient-coll1 transient-coll2]
166+
(reduce conj! transient-coll1 (persistent! transient-coll2)))
167+
168+
(defn pmap
169+
"Like `clojure.core/pmap`, but uses parallel streams for better efficiency."
170+
[f, ^java.util.Collection coll]
171+
(-> (.parallelStream coll)
172+
(.map (reify java.util.function.Function
173+
(apply [_ x] (f x))))
174+
(.collect (reify java.util.function.Supplier
175+
(get [_] (volatile! (transient []))))
176+
(reify java.util.function.BiConsumer
177+
(accept [_ acc x] (vswap! acc conj! x)))
178+
(reify java.util.function.BiConsumer
179+
(accept [_ acc1 acc2]
180+
(vswap! acc1 into! @acc2))))
181+
deref
182+
persistent!))

src/orchard/stacktrace.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[clojure.string :as str]
1313
[orchard.info :as info]
1414
[orchard.java.resource :as resource]
15-
[orchard.misc :refer [assoc-some]])
15+
[orchard.misc :as misc :refer [assoc-some]])
1616
(:import
1717
(java.io StringWriter)
1818
(java.net URL)
@@ -252,7 +252,7 @@
252252
"Return the stacktrace as a sequence of maps, each describing a stack frame."
253253
[trace]
254254
(when (seq trace)
255-
(-> (pmap analyze-frame trace)
255+
(-> (misc/pmap analyze-frame trace)
256256
(flag-duplicates)
257257
(flag-tooling))))
258258

0 commit comments

Comments
 (0)