From 2272d5ea578ac9c36b0a2d6e7c161d284bff8d88 Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Thu, 21 Dec 2017 10:08:24 +0100 Subject: [PATCH] [#104] Micro-optimization: remove unnecessary runtime 'if' in extend-freeze macro (@scramjet) --- src/taoensso/nippy.clj | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index 96bc300..9b645a9 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -1426,15 +1426,18 @@ (.writeUTF [data-output] (:data x)))" [type custom-type-id [x out] & body] (assert-custom-type-id custom-type-id) - `(extend-type ~type IFreezable1 - (~'-freeze-without-meta! [~x ~(with-meta out {:tag 'java.io.DataOutput})] - (if-not ~(keyword? custom-type-id) - ;; Unprefixed [cust byte id][payload]: - (write-id ~out ~(coerce-custom-type-id custom-type-id)) - ;; Prefixed [const byte id][cust hash id][payload]: - (do (write-id ~out ~id-prefixed-custom) - (.writeShort ~out ~(coerce-custom-type-id custom-type-id)))) - ~@body))) + (let [write-id-form + (if (keyword? custom-type-id) + ;; Prefixed [const byte id][cust hash id][payload]: + `(do (write-id ~out ~id-prefixed-custom) + (.writeShort ~out ~(coerce-custom-type-id custom-type-id))) + ;; Unprefixed [cust byte id][payload]: + `(write-id ~out ~(coerce-custom-type-id custom-type-id)))] + + `(extend-type ~type IFreezable1 + (~'-freeze-without-meta! [~x ~(with-meta out {:tag 'java.io.DataOutput})] + ~write-id-form + ~@body)))) (defmacro extend-thaw "Extends Nippy to support thawing of a custom type with given id: