Skip to content

Commit

Permalink
improved JSONL performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jlangch committed Oct 18, 2023
1 parent 78bf66c commit 35e2780
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
2 changes: 1 addition & 1 deletion doc/examples/scripts/jsonl-bulk-demo.venice
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
(t/run #(write file data) (str "\nWriting " lines " records..."))))

(defn read-with-timing [file]
(t/run #(read file) "\nReading records..."))
(t/run #(read file) "\nReading..." #(printf "Read %d records.%n" (count %))))

(let [file (io/file "jsonl-bulk-demo.jsonl")
lines 10_000]
Expand Down
29 changes: 18 additions & 11 deletions src/main/resources/com/github/jlangch/venice/jsonl.venice
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@
(let [wr (apply io/buffered-writer out options)]
(if (sequential? val)
(do
(docoll #(println wr (apply json/write-str % options)) (butlast val))
(print wr (apply json/write-str (last val) options)))
(print wr (apply json/write-str val options)))
(docoll #(io/print-line wr (apply json/write-str % options)) (butlast val))
(io/print wr (apply json/write-str (last val) options)))
(io/print wr (apply json/write-str val options)))
(flush wr)))


Expand Down Expand Up @@ -214,11 +214,18 @@

slurp [in & options]

(let [rd (apply io/buffered-reader in options)]
(loop [line (read-line rd) data '()]
(if (nil? line)
data
(let [l (str/trim-to-nil line)
v (if (nil? l) nil (apply json/read-str l options))
d (if (nil? v) data (conj data v))]
(recur (read-line rd) d))))))
(if (empty? options)
(let [rd (io/buffered-reader in)]
(loop [line (read-line rd) data '()]
(if (nil? line)
data
(let [v (json/read-str (str/trim-to-nil line))
d (if (nil? v) data (conj data v))]
(recur (read-line rd) d)))))
(let [rd (apply io/buffered-reader in options)]
(loop [line (read-line rd) data '()]
(if (nil? line)
data
(let [v (apply json/read-str (str/trim-to-nil line) options)
d (if (nil? v) data (conj data v))]
(recur (read-line rd) d)))))))
32 changes: 22 additions & 10 deletions src/main/resources/com/github/jlangch/venice/timing.venice
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,25 @@
:doc "Runs a function f with printing elapsed time."
:examples '("(timing/run #(sleep 1000) \"Sleeping...\")" ) }

run [f start-msg]

{ :pre [(fn? f) (string? start-msg)] }

(println start-msg)
(let [start (current-time-millis)
retval (f)
elapsed (- (current-time-millis) start)]
(println "Done. Elapsed:" (format-milli-time elapsed :precision 2))
retval))
run

([f start-msg]
{ :pre [(fn? f) (string? start-msg)] }

(println start-msg)
(let [start (current-time-millis)
retval (f)
elapsed (- (current-time-millis) start)]
(println "Done. Elapsed:" (format-milli-time elapsed :precision 2))
retval))

([f start-msg end-msg]
{ :pre [(fn? f) (string? start-msg)] }

(println start-msg)
(let [start (current-time-millis)
retval (f)
elapsed (- (current-time-millis) start)]
(if (fn? end-msg) (end-msg retval) (println end-msg))
(println "Elapsed:" (format-milli-time elapsed :precision 2))
retval)))

0 comments on commit 35e2780

Please sign in to comment.