From 27db3021904bd73157e413a71a74ff4d642f22e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=B6tt=C3=B6nen?= Date: Fri, 14 Jun 2024 07:29:17 +0300 Subject: [PATCH 1/2] Add type hints to speed up The code loops through large byte arrays and checks the type every time. Tested with Ara ETP that the type hints are indeed a good optimisation in this case. --- src/clj/puumerkki/pdf.clj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/clj/puumerkki/pdf.clj b/src/clj/puumerkki/pdf.clj index e96f54e..fa71733 100644 --- a/src/clj/puumerkki/pdf.clj +++ b/src/clj/puumerkki/pdf.clj @@ -72,7 +72,7 @@ (if (= pos len) target (do - (aset-byte target pos (aget arr (+ start pos))) + (aset-byte target pos (aget ^bytes arr (+ start pos))) (recur (+ pos 1)))))))) (defn copy-bytes! [array content offset] @@ -80,7 +80,7 @@ (loop [pos (- (count content) 1)] (if (> pos -1) (do - (aset-byte array (+ offset pos) (aget content pos)) + (aset-byte array (+ offset pos) (aget ^bytes content pos)) (recur (- pos 1))) array)) nil)) @@ -216,7 +216,7 @@ ;; count number of ascii zeroes at position (which are used for signature area filling) (defn zeroes-at [data pos] (loop [pos pos n 0] - (let [val (aget data pos)] + (let [val (aget ^bytes data pos)] (if (= val 48) (recur (+ pos 1) (+ n 1)) n)))) @@ -226,7 +226,7 @@ (cond (not (aget data pos)) false ;; out of data - (and (= (aget data pos) 60) (> (zeroes-at data (+ pos 1)) 512)) + (and (= (aget ^bytes data pos) 60) (> (zeroes-at data (+ pos 1)) 512)) (+ pos 1) :else (recur (+ pos 1))))) From 647415f9336440a5af84226692bd9776bbfa29dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20M=C3=B6tt=C3=B6nen?= Date: Fri, 14 Jun 2024 11:42:44 +0300 Subject: [PATCH 2/2] Move type hints to function declaration --- src/clj/puumerkki/pdf.clj | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/clj/puumerkki/pdf.clj b/src/clj/puumerkki/pdf.clj index fa71733..514080c 100644 --- a/src/clj/puumerkki/pdf.clj +++ b/src/clj/puumerkki/pdf.clj @@ -64,7 +64,7 @@ (seq->byte-array (map unsigned->signed-byte seq))) -(defn subarray [arr start len] +(defn subarray [^bytes arr start len] (if (or (< len 0) (< start 0) (> (+ start len) (count arr))) nil (let [target (byte-array len)] @@ -72,15 +72,15 @@ (if (= pos len) target (do - (aset-byte target pos (aget ^bytes arr (+ start pos))) + (aset-byte target pos (aget arr (+ start pos))) (recur (+ pos 1)))))))) -(defn copy-bytes! [array content offset] +(defn copy-bytes! [array ^bytes content offset] (if (>= (count array) (+ offset (count content))) (loop [pos (- (count content) 1)] (if (> pos -1) (do - (aset-byte array (+ offset pos) (aget ^bytes content pos)) + (aset-byte array (+ offset pos) (aget content pos)) (recur (- pos 1))) array)) nil)) @@ -214,19 +214,19 @@ nil))) ;; count number of ascii zeroes at position (which are used for signature area filling) -(defn zeroes-at [data pos] +(defn zeroes-at [^bytes data pos] (loop [pos pos n 0] - (let [val (aget ^bytes data pos)] + (let [val (aget data pos)] (if (= val 48) (recur (+ pos 1) (+ n 1)) n)))) -(defn find-signature-space [data] +(defn find-signature-space [^bytes data] (loop [pos 0] (cond (not (aget data pos)) false ;; out of data - (and (= (aget ^bytes data pos) 60) (> (zeroes-at data (+ pos 1)) 512)) + (and (= (aget data pos) 60) (> (zeroes-at data (+ pos 1)) 512)) (+ pos 1) :else (recur (+ pos 1)))))