Skip to content

Commit 83e3632

Browse files
authored
Merge pull request #37 from arjenve/cljr
port to cljr, tests succeeed but are 10x slower
2 parents bff3b3d + daf48c3 commit 83e3632

File tree

11 files changed

+125
-14
lines changed

11 files changed

+125
-14
lines changed

deps-clr.edn

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{:paths ["src"]
2+
:deps
3+
{io.github.clojure/clr.data.generators {:git/tag "v1.1.0" :git/sha "d25d292"}
4+
}
5+
6+
:aliases
7+
{:test
8+
{:extra-paths ["test"]
9+
:extra-deps {io.github.dmiller/test-runner {:git/tag "v0.5.1clr" :git/sha "814e06f"}
10+
io.github.clojure/clr.test.check {:git/tag "v1.1.2" :git/sha "26f34e6"}}
11+
:exec-fn cognitect.test-runner.api/test
12+
:exec-args {:dirs ["test"]}}}}

src/editscript/core.cljc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
[editscript.diff.quick :as q]
1616
[editscript.diff.a-star :as a])
1717
#?(:clj (:import [editscript.edit EditScript]
18+
[clojure.lang MapEntry])
19+
:cljr (:import [editscript.edit EditScript]
1820
[clojure.lang MapEntry])))
1921

2022
(defn diff

src/editscript/diff/a_star.cljc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
#?(:clj (:import [clojure.lang Keyword]
1818
[java.io Writer]
1919
[java.lang Comparable]
20+
[editscript.util.index Node])
21+
:cljr (:import [clojure.lang Keyword]
2022
[editscript.util.index Node])))
2123

2224
#?(:clj (set! *warn-on-reflection* true))
25+
#?(:cljr (set! *warn-on-reflection* true))
2326
#?(:clj (set! *unchecked-math* :warn-on-boxed))
2427

2528
;; diffing
@@ -42,6 +45,22 @@
4245
Comparable
4346
(compareTo [this that]
4447
(- (.hashCode this) (.hashCode that))))
48+
:cljr
49+
(deftype Coord [^Node a
50+
^Node b]
51+
;; Java's native hash is too slow,
52+
;; overriding hashCode significantly speeds things up
53+
Object
54+
(GetHashCode [_] (coord-hash a b))
55+
(Equals [_ that]
56+
(and (= (i/get-order a) (i/get-order (.-a ^Coord that)))
57+
(= (i/get-order b) (i/get-order (.-b ^Coord that)))))
58+
(ToString [_]
59+
(str "[" (i/get-value a) "," (i/get-value b) "]"))
60+
61+
IComparable
62+
(CompareTo [this that]
63+
(- (.GetHashCode this) (.GetHashCode that))))
4564

4665
:cljs
4766
(deftype Coord [^Node a
@@ -111,6 +130,7 @@
111130
(defn- access-g
112131
[g cur]
113132
(get g cur #?(:clj Long/MAX_VALUE
133+
:cljr Int64/MaxValue
114134
:cljs (getMaxValue))))
115135

116136
(declare diff*)

src/editscript/diff/quick.cljc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#?(:clj (set! *warn-on-reflection* true))
1818
#?(:clj (set! *unchecked-math* :warn-on-boxed))
19+
#?(:cljr (set! *warn-on-reflection* true))
1920

2021
(declare diff*)
2122

src/editscript/edit.cljc

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
(ns ^:no-doc editscript.edit
1212
#?(:clj (:import [clojure.lang PersistentVector IPersistentList IPersistentMap
1313
IPersistentSet IPersistentVector MapEntry]
14-
[java.util Map$Entry])))
14+
[java.util Map$Entry])
15+
:cljr (:import [clojure.lang PersistentVector IPersistentList IPersistentMap
16+
IPersistentSet IPersistentVector MapEntry]
17+
)))
1518

1619
(defprotocol IEdit
1720
(auto-sizing [this path value])
@@ -53,7 +56,7 @@
5356

5457
IPersistentSet
5558
(get-type [_] :set)
56-
59+
5760
Map$Entry
5861
(get-type [_] :val)
5962

@@ -68,6 +71,32 @@
6871

6972
Object
7073
(get-type [_] :val))
74+
75+
:cljr
76+
(extend-protocol IType
77+
IPersistentList
78+
(get-type [_] :lst)
79+
80+
IPersistentMap
81+
(get-type [_] :map)
82+
83+
IPersistentVector
84+
(get-type [_] :vec)
85+
86+
IPersistentSet
87+
(get-type [_] :set)
88+
89+
MapEntry
90+
(get-type [_] :val)
91+
92+
nil
93+
(get-type [_] :val)
94+
95+
String
96+
(get-type [_] :str)
97+
98+
Object
99+
(get-type [_] :val))
71100

72101
:cljs
73102
(extend-protocol IType

src/editscript/patch.cljc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
[clojure.string :as s]))
1616

1717
#?(:clj (set! *warn-on-reflection* true))
18+
#?(:cljr (set! *warn-on-reflection* true))
1819
#?(:clj (set! *unchecked-math* :warn-on-boxed))
1920

2021
(defn vget

src/editscript/util/common.cljc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232

3333
(defn current-time
3434
^long []
35-
#?(:clj (System/currentTimeMillis) :cljs (.getTime (js/Date.))))
35+
#?(:clj (System/currentTimeMillis)
36+
:cljr (.ToUnixTimeMilliseconds (DateTimeOffset/Now))
37+
:cljs (.getTime (js/Date.))))
3638

3739
(defn- vec-edits*
3840
"Based on 'Wu, S. et al., 1990, An O(NP) Sequence Comparison Algorithm,
@@ -106,7 +108,7 @@
106108
[ms ps] (split-with #(= % m) coll)
107109
mc (count ms)
108110
pc (count ps)
109-
delta (Math/abs (- mc pc))
111+
delta (#?(:cljr Math/Abs :default Math/abs) (- mc pc))
110112
rs (repeat (- (max mc pc) delta) :r)]
111113
(cond
112114
(< mc pc) (concat rs (repeat delta p))

src/editscript/util/index.cljc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
(:require [editscript.edit :as e]
1313
#?(:cljs [goog.math.Long :refer [getMaxValue]]))
1414
#?(:clj (:import [clojure.lang PersistentVector]
15-
[java.io Writer])) )
15+
[java.io Writer])
16+
:cljr (:import [clojure.lang PersistentVector])) )
1617

1718
;; indexing
1819

src/editscript/util/pairing.cljc

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
(ns ^:no-doc editscript.util.pairing
1212
#?(:clj
1313
(:import [clojure.lang IPersistentStack IPersistentMap IPersistentCollection]
14-
[java.io Writer])))
14+
[java.io Writer])
15+
:cljr
16+
(:import [clojure.lang IPersistentStack IPersistentMap IPersistentCollection]
17+
)))
1518

1619
#?(:clj (set! *warn-on-reflection* true))
1720

@@ -100,7 +103,45 @@
100103
(set! map (dissoc map (.-item heap)))
101104
(set! heap n)
102105
this)))
106+
:cljr
107+
(deftype PriorityMap [^:unsynchronized-mutable ^HeapNode heap
108+
^:unsynchronized-mutable map]
109+
IPersistentCollection
110+
(count [_] (count map))
111+
(^IPersistentCollection cons [this e]
112+
(let [[item priority] e]
113+
(set! map (assoc map item priority))
114+
(set! heap (insert heap item priority))
115+
this))
116+
(empty [this]
117+
(set! heap nil)
118+
(set! map {})
119+
this)
120+
(equiv [this o] (identical? this o))
121+
122+
IPersistentMap
123+
(^IPersistentMap assoc [this item priority]
124+
(set! map (assoc map item priority))
125+
(set! heap (insert heap item priority))
126+
this)
127+
(^clojure.lang.Associative assoc [this item priority]
128+
(set! map (assoc map item priority))
129+
(set! heap (insert heap item priority))
130+
this)
131+
(GetHashCode [_] (hash map))
132+
(Equals [this o] (identical? this o))
133+
(containsKey [_ item] (contains? map item))
134+
(entryAt [_ k] (find map k))
135+
(seq [_] (seq map))
136+
(without [this item] (dissoc map item) this)
103137

138+
IPersistentStack
139+
(peek [_] [(.-item heap) (.-priority heap)])
140+
(pop [this]
141+
(let [n (two-pass (get-left heap))]
142+
(set! map (dissoc map (.-item heap)))
143+
(set! heap n)
144+
this)))
104145
:cljs
105146
(deftype PriorityMap [^:mutable ^HeapNode heap
106147
^:mutable map]
@@ -115,6 +156,7 @@
115156
(set! heap (insert heap item priority))
116157
this))
117158

159+
IAssociative
118160
IAssociative
119161
(-assoc [this item priority]
120162
(set! map (assoc map item priority))

test/editscript/core_test.cljc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,17 @@
230230
;; sample data tests
231231

232232
(def data1 (-> "resources/drawing1.edn"
233-
#?(:clj slurp :cljs com/vslurp)
234-
#?(:clj read-string :cljs reader/read-string)))
233+
#?(:default slurp :cljs com/vslurp)
234+
#?(:default read-string :cljs reader/read-string)))
235235
(def data2 (-> "resources/drawing2.edn"
236-
#?(:clj slurp :cljs com/vslurp)
237-
#?(:clj read-string :cljs reader/read-string)))
236+
#?(:default slurp :cljs com/vslurp)
237+
#?(:default read-string :cljs reader/read-string)))
238238
(def data3 (-> "resources/drawing3.edn"
239-
#?(:clj slurp :cljs com/vslurp)
240-
#?(:clj read-string :cljs reader/read-string)))
239+
#?(:default slurp :cljs com/vslurp)
240+
#?(:default read-string :cljs reader/read-string)))
241241
(def data4 (-> "resources/drawing4.edn"
242-
#?(:clj slurp :cljs com/vslurp)
243-
#?(:clj read-string :cljs reader/read-string)))
242+
#?(:default slurp :cljs com/vslurp)
243+
#?(:default read-string :cljs reader/read-string)))
244244

245245
(deftest drawing-sample-test
246246
(testing "A sample JSON data of a drawing program from https://github.com/justsml/json-diff-performance, converted to edn using https://github.com/peterschwarz/json-to-edn"

test/editscript/edit_test.cljc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
(:require [editscript.edit :as e]
1313
[editscript.core :as c]
1414
#?(:clj [clojure.test :refer [is are deftest ]]
15+
:cljr [clojure.test :refer [is are deftest ]]
1516
:cljs [cljs.test :refer [is are deftest] :include-macros true])))
1617

1718
(deftest edits-equality-test

0 commit comments

Comments
 (0)