Skip to content

Commit

Permalink
Reducing the cost of including datatype.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuernber committed Feb 3, 2024
1 parent 101f1aa commit 6415407
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 38 deletions.
13 changes: 5 additions & 8 deletions src/tech/v3/datatype.clj
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@
(tech.v3.datatype-api/ensure-serializeable item)))


(defmacro export-symbols
([lib & args]
`(tech.v3.datatype-api/export-symbols ~lib ~@args)))


(defn get-datatype
"Legacy method, returns elemwise-datatype"
([item]
Expand Down Expand Up @@ -512,14 +517,6 @@ user> (dtype/make-reader :float32 5 (* idx 2))
(tech.v3.datatype.native-buffer/malloc n-bytes)))


(defn map-factory
"Create an IFn taking exactly n-keys arguments to rapidly create a map.
This moves the key-checking to the factory creation and simply creates a
new map as fast as possible when requrested"
([key-seq]
(tech.v3.datatype-api/map-factory key-seq)))


(defn prealloc-list
"Make an list with preallocated storage. This function exists to cause
a compilation error if older versions of dtype-next are included and is
Expand Down
15 changes: 12 additions & 3 deletions src/tech/v3/datatype/export_symbols.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
(ns tech.v3.datatype.export-symbols
(:require [clojure.java.io :as io]
[clojure.tools.logging :as log])
(:import [java.io Writer]))
(:import [java.io Writer]
[java.util.concurrent.locks ReentrantLock]))

(def ^ReentrantLock load-lib-lock (ReentrantLock.))


(defmacro export-symbols
[src-ns & symbol-list]
`(do
(#'clojure.core/load-lib nil '~src-ns)
~@(->> symbol-list
(mapv
(fn [sym-name]
Expand Down Expand Up @@ -39,7 +41,14 @@
(format "Cannot export macros as this breaks aot: %s"
sym-name)
{:symbol sym-name})))
`(def ~(with-meta sym-name full-meta) ~(symbol (name src-ns) (name sym-name)))))))))
`(def ~(with-meta sym-name full-meta)
(do
(.lock load-lib-lock)
(try
(#'clojure.core/load-lib nil '~src-ns)
(finally
(.unlock load-lib-lock)))
~(symbol (name src-ns) (name sym-name))))))))))

(defn- write!
^Writer [^Writer writer data & datas]
Expand Down
1 change: 0 additions & 1 deletion src/tech/v3/datatype/unary_op.clj
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@
(def builtin-ops
(->> [(make-double-unary-op :tech.numerics/floor (Math/floor x))
(make-double-unary-op :tech.numerics/ceil (Math/ceil x))
(make-double-unary-op :tech.numerics/round (unchecked-double (Math/round (double x))))
(make-double-unary-op :tech.numerics/rint (Math/rint x))
(make-double-unary-op :tech.numerics/get-significand (unchecked-double (get-significand x)))
(make-numeric-object-unary-op :tech.numerics/- (- x))
Expand Down
34 changes: 8 additions & 26 deletions src/tech/v3/datatype_api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,17 @@
"Base namespace for container creation and elementwise access of data"
(:require [tech.v3.datatype.dispatch :as dispatch]
[tech.v3.datatype.errors :as errors]
[tech.v3.datatype.array-buffer :as abuf]
[tech.v3.datatype.native-buffer :as nbuf]
[tech.v3.datatype.list]
[tech.v3.datatype.casting :as casting]
[tech.v3.datatype.protocols :as dtype-proto]
[tech.v3.datatype.export-symbols]
[tech.v3.datatype.clj-range]
[tech.v3.datatype.functional]
[tech.v3.datatype.copy-raw-to-item]
[tech.v3.datatype.primitive]
[tech.v3.datatype.copy-raw-to-item]
[tech.v3.datatype.monotonic-range :as dt-range]
;;import in clone for jvm maps
[tech.v3.datatype.export-symbols :refer [export-symbols] :as export-symbols]
;;Includes to let clj-kondo know to parse this file after parsing these
;;namespaces
[tech.v3.datatype.base]
[tech.v3.datatype.argtypes]
[tech.v3.datatype.emap]
[tech.v3.datatype.argops]
[tech.v3.datatype.io-indexed-buffer]
[tech.v3.datatype.const-reader]
[tech.v3.datatype.io-concat-buffer]
[tech.v3.datatype.list :as dt-list]
[tech.v3.datatype.copy-make-container]
[tech.v3.datatype.fastobjs :as fastobjs])
[tech.v3.datatype.list :as dt-list])
(:import [tech.v3.datatype BooleanReader LongReader DoubleReader ObjectReader
Buffer ArrayBufferData NativeBufferData]
[tech.v3.datatype.native_buffer NativeBuffer]
Expand All @@ -35,6 +22,11 @@
[org.roaringbitmap RoaringBitmap])
(:refer-clojure :exclude [cast reverse]))

(defmacro export-symbols
[lib & symbols]
(require '[tech.v3.datatype.export-symbols])
`(tech.v3.datatype.export-symbols/export-symbols ~lib ~@symbols))


(export-symbols tech.v3.datatype.casting
cast
Expand Down Expand Up @@ -423,16 +415,6 @@ user> (dt/make-list :float32 (range 10))
(errors/throwf "Unable create create tensor for nd object type: %s"
(type item))))))


(defn map-factory
"Create an IFn taking exactly n-keys arguments to rapidly create a map.
This moves the key-checking to the factory creation and simply creates a
new map as fast as possible when requrested"
[key-seq]
(if (empty? key-seq)
(constantly {})
(fastobjs/map-factory key-seq)))

(comment

(export-symbols/write-api! 'tech.v3.datatype-api
Expand Down

0 comments on commit 6415407

Please sign in to comment.