Skip to content

port to cljr, tests succeeed but are 10x slower #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions deps-clr.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{:paths ["src"]
:deps
{io.github.clojure/clr.data.generators {:git/tag "v1.1.0" :git/sha "d25d292"}
}

:aliases
{:test
{:extra-paths ["test"]
:extra-deps {io.github.dmiller/test-runner {:git/tag "v0.5.1clr" :git/sha "814e06f"}
io.github.clojure/clr.test.check {:git/tag "v1.1.2" :git/sha "26f34e6"}}
:exec-fn cognitect.test-runner.api/test
:exec-args {:dirs ["test"]}}}}
2 changes: 2 additions & 0 deletions src/editscript/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
[editscript.diff.quick :as q]
[editscript.diff.a-star :as a])
#?(:clj (:import [editscript.edit EditScript]
[clojure.lang MapEntry])
:cljr (:import [editscript.edit EditScript]
[clojure.lang MapEntry])))

(defn diff
Expand Down
20 changes: 20 additions & 0 deletions src/editscript/diff/a_star.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
#?(:clj (:import [clojure.lang Keyword]
[java.io Writer]
[java.lang Comparable]
[editscript.util.index Node])
:cljr (:import [clojure.lang Keyword]
[editscript.util.index Node])))

#?(:clj (set! *warn-on-reflection* true))
#?(:cljr (set! *warn-on-reflection* true))
#?(:clj (set! *unchecked-math* :warn-on-boxed))

;; diffing
Expand All @@ -42,6 +45,22 @@
Comparable
(compareTo [this that]
(- (.hashCode this) (.hashCode that))))
:cljr
(deftype Coord [^Node a
^Node b]
;; Java's native hash is too slow,
;; overriding hashCode significantly speeds things up
Object
(GetHashCode [_] (coord-hash a b))
(Equals [_ that]
(and (= (i/get-order a) (i/get-order (.-a ^Coord that)))
(= (i/get-order b) (i/get-order (.-b ^Coord that)))))
(ToString [_]
(str "[" (i/get-value a) "," (i/get-value b) "]"))

IComparable
(CompareTo [this that]
(- (.GetHashCode this) (.GetHashCode that))))

:cljs
(deftype Coord [^Node a
Expand Down Expand Up @@ -111,6 +130,7 @@
(defn- access-g
[g cur]
(get g cur #?(:clj Long/MAX_VALUE
:cljr Int64/MaxValue
:cljs (getMaxValue))))

(declare diff*)
Expand Down
1 change: 1 addition & 0 deletions src/editscript/diff/quick.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#?(:clj (set! *warn-on-reflection* true))
#?(:clj (set! *unchecked-math* :warn-on-boxed))
#?(:cljr (set! *warn-on-reflection* true))

(declare diff*)

Expand Down
33 changes: 31 additions & 2 deletions src/editscript/edit.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
(ns ^:no-doc editscript.edit
#?(:clj (:import [clojure.lang PersistentVector IPersistentList IPersistentMap
IPersistentSet IPersistentVector MapEntry]
[java.util Map$Entry])))
[java.util Map$Entry])
:cljr (:import [clojure.lang PersistentVector IPersistentList IPersistentMap
IPersistentSet IPersistentVector MapEntry]
)))

(defprotocol IEdit
(auto-sizing [this path value])
Expand Down Expand Up @@ -53,7 +56,7 @@

IPersistentSet
(get-type [_] :set)

Map$Entry
(get-type [_] :val)

Expand All @@ -68,6 +71,32 @@

Object
(get-type [_] :val))

:cljr
(extend-protocol IType
IPersistentList
(get-type [_] :lst)

IPersistentMap
(get-type [_] :map)

IPersistentVector
(get-type [_] :vec)

IPersistentSet
(get-type [_] :set)

MapEntry
(get-type [_] :val)

nil
(get-type [_] :val)

String
(get-type [_] :str)

Object
(get-type [_] :val))

:cljs
(extend-protocol IType
Expand Down
1 change: 1 addition & 0 deletions src/editscript/patch.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
[clojure.string :as s]))

#?(:clj (set! *warn-on-reflection* true))
#?(:cljr (set! *warn-on-reflection* true))
#?(:clj (set! *unchecked-math* :warn-on-boxed))

(defn vget
Expand Down
6 changes: 4 additions & 2 deletions src/editscript/util/common.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@

(defn current-time
^long []
#?(:clj (System/currentTimeMillis) :cljs (.getTime (js/Date.))))
#?(:clj (System/currentTimeMillis)
:cljr (.ToUnixTimeMilliseconds (DateTimeOffset/Now))
:cljs (.getTime (js/Date.))))

(defn- vec-edits*
"Based on 'Wu, S. et al., 1990, An O(NP) Sequence Comparison Algorithm,
Expand Down Expand Up @@ -106,7 +108,7 @@
[ms ps] (split-with #(= % m) coll)
mc (count ms)
pc (count ps)
delta (Math/abs (- mc pc))
delta (#?(:cljr Math/Abs :default Math/abs) (- mc pc))
rs (repeat (- (max mc pc) delta) :r)]
(cond
(< mc pc) (concat rs (repeat delta p))
Expand Down
3 changes: 2 additions & 1 deletion src/editscript/util/index.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
(:require [editscript.edit :as e]
#?(:cljs [goog.math.Long :refer [getMaxValue]]))
#?(:clj (:import [clojure.lang PersistentVector]
[java.io Writer])) )
[java.io Writer])
:cljr (:import [clojure.lang PersistentVector])) )

;; indexing

Expand Down
44 changes: 43 additions & 1 deletion src/editscript/util/pairing.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
(ns ^:no-doc editscript.util.pairing
#?(:clj
(:import [clojure.lang IPersistentStack IPersistentMap IPersistentCollection]
[java.io Writer])))
[java.io Writer])
:cljr
(:import [clojure.lang IPersistentStack IPersistentMap IPersistentCollection]
)))

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

Expand Down Expand Up @@ -100,7 +103,45 @@
(set! map (dissoc map (.-item heap)))
(set! heap n)
this)))
:cljr
(deftype PriorityMap [^:unsynchronized-mutable ^HeapNode heap
^:unsynchronized-mutable map]
IPersistentCollection
(count [_] (count map))
(^IPersistentCollection cons [this e]
(let [[item priority] e]
(set! map (assoc map item priority))
(set! heap (insert heap item priority))
this))
(empty [this]
(set! heap nil)
(set! map {})
this)
(equiv [this o] (identical? this o))

IPersistentMap
(^IPersistentMap assoc [this item priority]
(set! map (assoc map item priority))
(set! heap (insert heap item priority))
this)
(^clojure.lang.Associative assoc [this item priority]
(set! map (assoc map item priority))
(set! heap (insert heap item priority))
this)
(GetHashCode [_] (hash map))
(Equals [this o] (identical? this o))
(containsKey [_ item] (contains? map item))
(entryAt [_ k] (find map k))
(seq [_] (seq map))
(without [this item] (dissoc map item) this)

IPersistentStack
(peek [_] [(.-item heap) (.-priority heap)])
(pop [this]
(let [n (two-pass (get-left heap))]
(set! map (dissoc map (.-item heap)))
(set! heap n)
this)))
:cljs
(deftype PriorityMap [^:mutable ^HeapNode heap
^:mutable map]
Expand All @@ -115,6 +156,7 @@
(set! heap (insert heap item priority))
this))

IAssociative
IAssociative
(-assoc [this item priority]
(set! map (assoc map item priority))
Expand Down
16 changes: 8 additions & 8 deletions test/editscript/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,17 @@
;; sample data tests

(def data1 (-> "resources/drawing1.edn"
#?(:clj slurp :cljs com/vslurp)
#?(:clj read-string :cljs reader/read-string)))
#?(:default slurp :cljs com/vslurp)
#?(:default read-string :cljs reader/read-string)))
(def data2 (-> "resources/drawing2.edn"
#?(:clj slurp :cljs com/vslurp)
#?(:clj read-string :cljs reader/read-string)))
#?(:default slurp :cljs com/vslurp)
#?(:default read-string :cljs reader/read-string)))
(def data3 (-> "resources/drawing3.edn"
#?(:clj slurp :cljs com/vslurp)
#?(:clj read-string :cljs reader/read-string)))
#?(:default slurp :cljs com/vslurp)
#?(:default read-string :cljs reader/read-string)))
(def data4 (-> "resources/drawing4.edn"
#?(:clj slurp :cljs com/vslurp)
#?(:clj read-string :cljs reader/read-string)))
#?(:default slurp :cljs com/vslurp)
#?(:default read-string :cljs reader/read-string)))

(deftest drawing-sample-test
(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"
Expand Down
1 change: 1 addition & 0 deletions test/editscript/edit_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
(:require [editscript.edit :as e]
[editscript.core :as c]
#?(:clj [clojure.test :refer [is are deftest ]]
:cljr [clojure.test :refer [is are deftest ]]
:cljs [cljs.test :refer [is are deftest] :include-macros true])))

(deftest edits-equality-test
Expand Down