]> asedeno.scripts.mit.edu Git - cl-protobufs.git/commitdiff
Make it a bit easier to debug (de)serialization
authorScott McKay <swm@google.com>
Fri, 1 Jun 2012 19:13:48 +0000 (19:13 +0000)
committerScott McKay <swm@google.com>
Fri, 1 Jun 2012 19:13:48 +0000 (19:13 +0000)
git-svn-id: http://svn.internal.itasoftware.com/svn/ita/trunk/qres/lisp/quux/protobufs@547034 f8382938-511b-0410-9cdd-bb47b084005c

parser.lisp
serialize.lisp
utilities.lisp
wire-format.lisp

index c252d3bbe6c0c7762abfb1c91dbefec0ed2958cf..8998db27fb545514df7460bedd3cbd65d222aa9b 100644 (file)
 
 (declaim (inline proto-whitespace-char-p))
 (defun proto-whitespace-char-p (ch)
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
-    (and ch (member ch '(#\space #\tab #\return #\newline)))))
+  (declare #.$optimize-fast-unsafe)
+  (and ch (member ch '(#\space #\tab #\return #\newline))))
 
 (declaim (inline proto-eol-char-p))
 (defun proto-eol-char-p (ch)
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
-    (and ch (member ch '(#\return #\newline)))))
+  (declare #.$optimize-fast-unsafe)   
+  (and ch (member ch '(#\return #\newline))))
 
 (declaim (inline proto-token-char-p))
 (defun proto-token-char-p (ch)
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
-    (and ch (or (alpha-char-p ch)
-                (digit-char-p ch)
-                (member ch '(#\_ #\.))))))
+  (declare #.$optimize-fast-unsafe)
+  (and ch (or (alpha-char-p ch)
+              (digit-char-p ch)
+              (member ch '(#\_ #\.)))))
 
 
 (defun skip-whitespace (stream)
index 93db43fd2241de80b8019599d29d407b3fdc960c..966f42a67e0be10f5cfa6fd91bcdf4e263df69d2 100644 (file)
       (return-from generate-serializer
         `(defmethod serialize-object
            (,vobj (,vclass (eql ,message)) ,vbuf &optional (,vidx 0) visited)
-         (declare (optimize (speed 3) (safety 0) (debug 0)))
+         (declare #.$optimize-serialization)
          (declare (ignorable ,vobj ,vclass visited)
                   (type (simple-array (unsigned-byte 8)) ,vbuf)
                   (type fixnum ,vidx))
                                   (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx)))))))))))))
       `(defmethod serialize-object
            (,vobj (,vclass (eql ,message)) ,vbuf &optional (,vidx 0) visited)
-         (declare (optimize (speed 3) (safety 0) (debug 0)))
+         (declare #.$optimize-serialization)
          (declare (ignorable visited)
                   (type (simple-array (unsigned-byte 8)) ,vbuf)
                   (type fixnum ,vidx))
       (return-from generate-deserializer
         `(defmethod deserialize-object
              ((,vclass (eql ,message)) ,vbuf &optional ,vidx ,vlen (,vendtag 0))
-           (declare (optimize (speed 3) (safety 0) (debug 0)))
+           (declare #.$optimize-serialization)
            (declare (ignorable ,vclass ,vbuf ,vlen ,vendtag)
                     (type (simple-array (unsigned-byte 8)) ,vbuf))
            (let ((,vidx (or ,vidx 0)))
              (rtemps  (mapcar #'second rslots)))
         `(defmethod deserialize-object
              ((,vclass (eql ,message)) ,vbuf &optional ,vidx ,vlen (,vendtag 0))
-           (declare (optimize (speed 3) (safety 0) (debug 0)))
+           (declare #.$optimize-serialization)
            (declare (type (simple-array (unsigned-byte 8)) ,vbuf))
            (let ((,vidx (or ,vidx 0))
                  (,vlen (or ,vlen (length ,vbuf))))
       (return-from generate-object-size
         `(defmethod object-size
              (,vobj (,vclass (eql ,message)) &optional visited)
-         (declare (optimize (speed 3) (safety 0) (debug 0)))
+         (declare #.$optimize-serialization)
          (declare (ignorable ,vobj visited))
          0)))
     (with-collectors ((sizers collect-sizer))
                                   (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag)))))))))))))
       `(defmethod object-size
            (,vobj (,vclass (eql ,message)) &optional visited)
-         (declare (optimize (speed 3) (safety 0) (debug 0)))
+         (declare #.$optimize-serialization)
          (declare (ignorable visited))
          (let ((,vsize (and visited (gethash ,vobj visited))))
            (when ,vsize
index e86846e816a54bd17a48741b3a0a845a2cafda06..3857286a0fc0a56163430258c54fe3088aae5612 100644 (file)
 
 ;;; Optimized fixnum arithmetic
 
+(defconstant $optimize-default     '(optimize (speed 1) (safety 3) (debug 3))
+  "Compiler optimization settings for safe, debuggable code.")
+
+(defconstant $optimize-fast-unsafe '(optimize (speed 3) (safety 0) (debug 0))
+  "Compiler optimization settings for fast, unsafe, hard-to-debug code.")
+
+
 (defmacro i+ (&rest fixnums)
   `(the fixnum (+ ,@(loop for n in fixnums collect `(the fixnum ,n)))))
 
@@ -60,7 +67,7 @@
 (define-modify-macro idecf (&optional (delta 1)) i-)
 
 (defmacro ildb (bytespec value)
-  `(ldb ,bytespec (the fixnum ,value)))
+  `(the fixnum (ldb ,bytespec (the fixnum ,value))))
 
 
 ;;; String utilities
index 6d3bfc980ca2264ac802f7a83b5aff3d038b43c2..5c8050697714ad2c00f3fe5eca15e241a0de29d6 100644 (file)
 
 (eval-when (:compile-toplevel :load-toplevel :execute)
 
+;; If you need to debug the (de)serializer, (pushnew :debug-serialization *features*)
+;; Otherwise, we try to make (de)serialization as fast as possible,
+;; risking life and limb to do so
+(defconstant $optimize-serialization
+  #+debug-serialization $optimize-default
+  #-debug-serialization $optimize-fast-unsafe)
+
 (defconstant $wire-type-varint 0)
 (defconstant $wire-type-64bit  1)
 (defconstant $wire-type-string 2)
-(defconstant $wire-type-start-group 3)          ;supposedly obsolete
-(defconstant $wire-type-end-group   4)          ;supposedly obsolete
+(defconstant $wire-type-start-group 3)          ;supposedly deprecated, but no such luck
+(defconstant $wire-type-end-group   4)          ;supposedly deprecated
 (defconstant $wire-type-32bit  5)
 
 )       ;eval-when
@@ -30,7 +37,7 @@
 (defun make-tag (type index)
   "Given a wire type or the name of a Protobufs type and a field index,
    return the tag that encodes both of them."
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (if (typep type 'fixnum)
       (ilogior type (iash index 3))
       (let ((type (ecase type
 
 
 (defun zig-zag-encode32 (val)
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (signed-byte 32) val))
   (logxor (ash val 1) (ash val -31)))
 
 (defun zig-zag-encode64 (val)
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (signed-byte 64) val))
   (logxor (ash val 1) (ash val -63)))
 
 (define-compiler-macro zig-zag-encode32 (&whole form val)
   (if (atom val)
-    `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
+    `(locally (declare #.$optimize-serialization
                        (type (signed-byte 32) ,val))
        (logxor (ash ,val 1) (ash ,val -31)))
     form))
 
 (define-compiler-macro zig-zag-encode64 (&whole form val)
   (if (atom val)
-    `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
+    `(locally (declare #.$optimize-serialization
                        (type (signed-byte 64) ,val))
        (logxor (ash ,val 1) (ash ,val -63)))
     form))
 
 (defun zig-zag-decode32 (val)
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (logxor (ash val -1) (- (logand val 1))))
 
 (defun zig-zag-decode64 (val)
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (logxor (ash val -1) (- (logand val 1))))
 
 (define-compiler-macro zig-zag-decode32 (&whole form val)
   (if (atom val)
-    `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+    `(locally (declare #.$optimize-serialization)
        (logxor (ash ,val -1) (- (logand ,val 1))))
     form))
 
 (define-compiler-macro zig-zag-decode64 (&whole form val)
   (if (atom val)
-    `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+    `(locally (declare #.$optimize-serialization)
        (logxor (ash ,val -1) (- (logand ,val 1))))
     form))
 
   (declare 
            (type (unsigned-byte 32) tag)
            (type fixnum index))
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (let ((idx (encode-uint32 tag buffer index)))
       (declare (type fixnum idx))
       (ecase type
   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
                      :fixed32 :sfixed32 :fixed64 :sfixed64
                      :string :bytes :bool :float :double))
-    `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
+    `(locally (declare #.$optimize-serialization
                        (type (simple-array (unsigned-byte 8)) ,buffer)
                        ;; 'tag' is a constant, no need to declare its type
                        (type fixnum ,index))
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type (unsigned-byte 32) tag)
            (type fixnum index))
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (let ((idx (encode-uint32 tag buffer index)))
       (declare (type fixnum idx))
       (multiple-value-bind (full-len len)
   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
                      :fixed32 :sfixed32 :fixed64 :sfixed64
                      :bool :float :double))
-    `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
+    `(locally (declare #.$optimize-serialization
                        (type (simple-array (unsigned-byte 8)) ,buffer)
                        ;; 'tag' is a constant, no need to declare its type
                        (type fixnum ,index))
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type (unsigned-byte 32) tag)
            (type fixnum index))
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (let* ((val (let ((e (find val enum-values :key #'proto-value)))
                   (and e (proto-index e))))
            (idx (encode-uint32 tag buffer index)))
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type (unsigned-byte 32) tag)
            (type fixnum index))
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (let ((idx (encode-uint32 tag buffer index)))
       (declare (type fixnum idx))
       (multiple-value-bind (full-len len)
    Watch out, this function turns off most type checking and all array bounds checking."
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (ecase type
       ((:int32)
        (decode-int32 buffer index))
   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
                      :fixed32 :sfixed32 :fixed64 :sfixed64
                      :string :bytes :bool :float :double))
-    `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
+    `(locally (declare #.$optimize-serialization
                        (type (simple-array (unsigned-byte 8)) ,buffer)
                        (type fixnum ,index))
        ,(ecase type
    Watch out, this function turns off most type checking and all array bounds checking."
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (multiple-value-bind (len idx)
         (decode-uint32 buffer index)
       (declare (type (unsigned-byte 32) len)
   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
                      :fixed32 :sfixed32 :fixed64 :sfixed64
                      :bool :float :double))
-    `(locally (declare (optimize (speed 3) (safety 0) (debug 0))
+    `(locally (declare #.$optimize-serialization
                        (type (simple-array (unsigned-byte 8)) ,buffer)
                        (type fixnum ,index))
        (block deserialize-packed
    Watch out, this function turns off most type checking and all array bounds checking."
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (multiple-value-bind (val idx)
         (decode-int32 buffer index)
       (let ((val (let ((e (find val enum-values :key #'proto-index)))
    Watch out, this function turns off most type checking and all array bounds checking."
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (multiple-value-bind (len idx)
         (decode-uint32 buffer index)
       (declare (type (unsigned-byte 32) len)
   "Returns the size in bytes that the primitive object will take when serialized.
    Watch out, this function turns off most type checking."
   (declare (type (unsigned-byte 32) tag))
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (ecase type
       ((:int32 :uint32)
        (i+ (length32 tag) (length32 (ldb (byte 32 0) val))))
   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
                      :fixed32 :sfixed32 :fixed64 :sfixed64
                      :string :bytes :bool :float :double))
-    `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+    `(locally (declare #.$optimize-serialization)
        ,(ecase type
           ((:int32)
            `(i+ (length32 ,tag) (length32 (ldb (byte 32 0) ,val))))
   "Returns the size in bytes that the packed object will take when serialized.
    Watch out, this function turns off most type checking."
   (declare (type (unsigned-byte 32) tag))
-  (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (locally (declare #.$optimize-serialization)
     (let ((len (let ((len 0))
                  (declare (type fixnum len))
                  (map () #'(lambda (val)
   (if (member type '(:int32 :uint32 :int64 :uint64 :sint32 :sint64
                      :fixed32 :sfixed32 :fixed64 :sfixed64
                      :bool :float :double))
-    `(locally (declare (optimize (speed 3) (safety 0) (debug 0)))
+    `(locally (declare #.$optimize-serialization)
        (let ((len (let ((len 0))
                     (declare (type fixnum len))
                     (map () #'(lambda (val)
    at the given index.
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (unsigned-byte 32) val)
            (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
    at the given index.
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (unsigned-byte 64) val)
            (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
    at the given index.
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (unsigned-byte 32) val)
            (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
    at the given index.
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (unsigned-byte 64) val)
            (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
    at the given index.
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (signed-byte 32) val)
            (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
    at the given index.
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (signed-byte 64) val)
            (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   "Encodes the single float 'val' into the buffer at the given index.
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type single-float val)
            (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   "Encodes the double float 'val' into the buffer at the given index.
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type double-float val)
            (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   "Encodes the octets into the buffer at the given index.
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   (let* ((octets (babel:string-to-octets string :encoding :utf-8))
   "Encodes the octets into the buffer at the given index.
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   (let* ((len (length octets))
   "Decodes the next 32-bit varint integer in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   ;; Seven bits at a time, least significant bits first
     (declare (type (unsigned-byte 32) val))
     (loop for places fixnum upfrom 0 by 7
           for byte fixnum = (prog1 (aref buffer index) (iincf index))
-          do (setq val (ilogior val (iash (ildb (byte 7 0) byte) places)))
+          do (let ((bits (ildb (byte 7 0) byte)))
+               (declare (type (unsigned-byte 8) bits))
+               (setq val (ilogior val (iash bits places))))
           until (i< byte 128)
           finally (progn
                     (assert (< val #.(ash 1 32)) ()
   "Decodes the next 64-bit varint integer in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   ;; Seven bits at a time, least significant bits first
     (declare (type (unsigned-byte 64) val))
     (loop for places fixnum upfrom 0 by 7
           for byte fixnum = (prog1 (aref buffer index) (iincf index))
-          do (setq val (logior val (ash (ildb (byte 7 0) byte) places)))
+          do (let ((bits (ildb (byte 7 0) byte)))
+               (declare (type (unsigned-byte 8) bits))
+               (setq val (logior val (ash bits places))))
           until (i< byte 128)
           finally (return (values val index)))))
 
   "Decodes the next 32-bit varint integer in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   (multiple-value-bind (val index)
   "Decodes the next 64-bit varint integer in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   (multiple-value-bind (val index)
   "Decodes the next 32-bit unsigned fixed integer in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   ;; Eight bits at a time, least significant bits first
   "Decodes the next unsigned 64-bit fixed integer in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   ;; Eight bits at a time, least significant bits first
   "Decodes the next 32-bit signed fixed integer in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   ;; Eight bits at a time, least significant bits first
   "Decodes the next signed 64-bit fixed integer in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   ;; Eight bits at a time, least significant bits first
   "Decodes the next single float in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   ;; Eight bits at a time, least significant bits first
   "Decodes the next double float in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   ;; Eight bits at a time, least significant bits first
   "Decodes the next UTF-8 encoded string in the buffer at the given index.
    Returns both the decoded string and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   (multiple-value-bind (len idx)
   "Decodes the next octets in the buffer at the given index.
    Returns both the decoded value and the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index))
   (multiple-value-bind (len idx)
 
 (defun length32 (val)
   "Returns the length that 'val' will take when encoded as a 32-bit integer."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (unsigned-byte 32) val))
   (let ((size 0))
     (declare (type fixnum size))
 
 (defun length64 (val)
   "Returns the length that 'val' will take when encoded as a 64-bit integer."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (unsigned-byte 64) val))
   (let ((size 0))
     (declare (type fixnum size))
   "Skip an element in the buffer at the index of the given wire type.
    Returns the new index in the buffer.
    Watch out, this function turns off all type checking and all array bounds checking."
-  (declare (optimize (speed 3) (safety 0) (debug 0)))
+  (declare #.$optimize-serialization)
   (declare (type (simple-array (unsigned-byte 8)) buffer)
            (type fixnum index)
            (type (unsigned-byte 32) tag))