]> asedeno.scripts.mit.edu Git - cl-protobufs.git/blobdiff - serialize.lisp
generalize CCL fasl ignores
[cl-protobufs.git] / serialize.lisp
index 607c371055b66c43865f2fd20f174aebe1df7134..b5be439fd449bafa1387a489f1ac2e081d2be1d5 100644 (file)
@@ -1,8 +1,8 @@
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;                                                                  ;;;
-;;; Confidential and proprietary information of ITA Software, Inc.   ;;;
+;;; Free Software published under an MIT-like license. See LICENSE   ;;;
 ;;;                                                                  ;;;
-;;; Copyright (c) 2012 ITA Software, Inc.  All rights reserved.      ;;;
+;;; Copyright (c) 2012 Google, Inc.  All rights reserved.            ;;;
 ;;;                                                                  ;;;
 ;;; Original author: Scott McKay                                     ;;;
 ;;;                                                                  ;;;
 
 ;;; Serialization
 
-;; Serialize the object using the given protobuf type
+(defun serialize-object-to-file (filename object type &key visited)
+  "Serializes the object 'object' of type 'type' into the file 'filename'
+   using the wire format.
+   'object' and 'type' are the same as for 'serialize-object-to-bytes'."
+  (with-open-file (stream filename
+                   :direction :output
+                   :element-type '(unsigned-byte 8))
+    (serialize-object-to-stream object type :stream stream :visited visited)))
+
 (defun serialize-object-to-stream (object type &key (stream *standard-output*) visited)
   "Serializes the object 'object' of type 'type' onto the stream 'stream'
+   using the wire format.
+   'object' and 'type' are the same as for 'serialize-object-to-bytes'."
+  (let ((buffer (serialize-object-to-bytes object type :visited visited)))
+    (write-sequence buffer stream)
+    buffer))
+
+(defun serialize-object-to-bytes (object type &key visited)
+  "Serializes the object 'object' of type 'type' into a new byte vector
    using the wire format.
    'type' is the Lisp name of a Protobufs message (usually the name of a 
    Lisp class) or a 'protobuf-message'.
          (size    (object-size object type visited))
          (buffer  (make-byte-vector size)))
     (serialize-object object type buffer 0 visited)
-    (when stream
-      (write-sequence buffer stream))
     buffer))
 
-(defun serialize-object-to-file (filename object type &key visited)
-  (with-open-file (stream filename
-                   :direction :output
-                   :element-type '(unsigned-byte 8))
-    (serialize-object-to-stream object type :stream stream :visited visited)))
+;; Serialize the object using the given protobuf type
+
 
 ;; Allow clients to add their own methods
 ;; This is how we address the problem of cycles, e.g. -- if you have an object
@@ -71,7 +82,7 @@
     (declare (type fixnum index))
     (macrolet ((read-slot (object slot reader)
                  ;; Don't do a boundp check, we assume the object is fully populated
-                 ;; Unpopulated slots should be "nullable" and should contain nil
+                 ;; Unpopulated slots should be "nullable" and will contain nil when empty
                  `(if ,reader
                     (funcall ,reader ,object)
                     (slot-value ,object ,slot))))
                                                                    type tag buffer index))))
                                   ((keywordp type)
                                    (let ((tag (make-tag type (proto-index field))))
-                                     (map () #'(lambda (v)
-                                                 (setq index (serialize-prim v type tag buffer index)))
-                                             (read-slot object slot reader))))
+                                     (doseq (v (read-slot object slot reader))
+                                       (setq index (serialize-prim v type tag buffer index)))))
                                   ((typep (setq msg (and type (or (find-message trace type)
-                                                                  (find-enum trace type))))
+                                                                  (find-enum trace type)
+                                                                  (find-type-alias trace type))))
                                           'protobuf-message)
                                    (if (eq (proto-message-type msg) :group)
-                                     (dolist (v (if slot (read-slot object slot reader) (list object)))
+                                     (doseq (v (if slot (read-slot object slot reader) (list object)))
                                        ;; To serialize a group, we encode a start tag,
                                        ;; serialize the fields, then encode an end tag
                                        (let ((tag1 (make-tag $wire-type-start-group (proto-index field)))
                                              (tag2 (make-tag $wire-type-end-group   (proto-index field))))
                                          (setq index (encode-uint32 tag1 buffer index))
-                                         (map () (curry #'do-field v msg)
-                                                 (proto-fields msg))
+                                         (dolist (f (proto-fields msg))
+                                           (do-field v msg f))
                                          (setq index (encode-uint32 tag2 buffer index))))
-                                     (dolist (v (if slot (read-slot object slot reader) (list object)))
+                                     (doseq (v (if slot (read-slot object slot reader) (list object)))
                                        ;; To serialize an embedded message, first say that it's
                                        ;; a string, then encode its size, then serialize its fields
                                        (let ((tag (make-tag $wire-type-string (proto-index field)))
                                              (len (object-size v msg visited)))
                                          (setq index (encode-uint32 tag buffer index))
                                          (setq index (encode-uint32 len buffer index)))
-                                       (map () (curry #'do-field v msg)
-                                               (proto-fields msg)))))
+                                       (dolist (f (proto-fields msg))
+                                         (do-field v msg f)))))
                                   ((typep msg 'protobuf-enum)
                                    (let ((tag (make-tag $wire-type-varint (proto-index field))))
                                      ;; 'proto-packed-p' of enum types returns nil,
                                      (if (proto-packed field)
                                        (setq index (serialize-packed-enum (read-slot object slot reader)
                                                                           (proto-values msg) tag buffer index))
-                                       (map () #'(lambda (v)
-                                                   (setq index (serialize-enum v (proto-values msg) tag buffer index)))
-                                               (read-slot object slot reader)))))))
+                                       (doseq (v (read-slot object slot reader))
+                                         (setq index (serialize-enum v (proto-values msg) tag buffer index))))))
+                                  ((typep msg 'protobuf-type-alias)
+                                   (let* ((type (proto-proto-type msg))
+                                          (tag  (make-tag type (proto-index field))))
+                                     (doseq (v (read-slot object slot reader))
+                                       (let ((v (funcall (proto-serializer msg) v)))
+                                         (setq index (serialize-prim v type tag buffer index))))))
+                                  (t
+                                   (undefined-field-type "While serializing ~S,"
+                                                         object type field))))
                            (t
                             (cond ((eq type :bool)
                                    ;; We have to handle optional boolean fields specially
                                          (setq index (serialize-prim v type tag buffer index))))))
                                   ((keywordp type)
                                    (let ((v (read-slot object slot reader)))
-                                     (when v
+                                     (when (and v (not (equal v (proto-default field))))
                                        (let ((tag (make-tag type (proto-index field))))
                                          (setq index (serialize-prim v type tag buffer index))))))
                                   ((typep (setq msg (and type (or (find-message trace type)
-                                                                  (find-enum trace type))))
+                                                                  (find-enum trace type)
+                                                                  (find-type-alias trace type))))
                                           'protobuf-message)
                                    (let ((v (if slot (read-slot object slot reader) object)))
                                      (when v
                                          (let ((tag1 (make-tag $wire-type-start-group (proto-index field)))
                                                (tag2 (make-tag $wire-type-end-group   (proto-index field))))
                                            (setq index (encode-uint32 tag1 buffer index))
-                                           (map () (curry #'do-field v msg)
-                                                   (proto-fields msg))
+                                           (dolist (f (proto-fields msg))
+                                             (do-field v msg f))
                                            (setq index (encode-uint32 tag2 buffer index)))
                                          (let ((tag (make-tag $wire-type-string (proto-index field)))
                                                (len (object-size v msg visited)))
                                            (setq index (encode-uint32 tag buffer index))
                                            (setq index (encode-uint32 len buffer index))
-                                           (map () (curry #'do-field v msg)
-                                                   (proto-fields msg)))))))
+                                           (dolist (f (proto-fields msg))
+                                             (do-field v msg f)))))))
                                   ((typep msg 'protobuf-enum)
                                    (let ((v (read-slot object slot reader)))
-                                     (when v
+                                     (when (and v (not (eql v (proto-default field))))
                                        (let ((tag (make-tag $wire-type-varint (proto-index field))))
-                                         (setq index (serialize-enum v (proto-values msg) tag buffer index)))))))))))))
+                                         (setq index (serialize-enum v (proto-values msg) tag buffer index))))))
+                                  ((typep msg 'protobuf-type-alias)
+                                   (let ((v (read-slot object slot reader)))
+                                     (when v
+                                       (let* ((v    (funcall (proto-serializer msg) v))
+                                              (type (proto-proto-type msg))
+                                              (tag  (make-tag type (proto-index field))))
+                                         (setq index (serialize-prim v type tag buffer index))))))
+                                  (t
+                                   (undefined-field-type "While serializing ~S,"
+                                                         object type field)))))))))
         (declare (dynamic-extent #'do-field))
-        (map () (curry #'do-field object message) (proto-fields message))))
+        (dolist (field (proto-fields message))
+          (do-field object message field))))
     (values buffer index)))
 
 
 ;;; Deserialization
 
+(defun deserialize-object-from-file (type filename)
+  "Deserializes an object of the given type 'type' from the given file
+   as a Protobuf object."
+  (with-open-file (stream filename
+                   :direction :input
+                   :element-type '(unsigned-byte 8))
+    (deserialize-object-from-stream type :stream stream)))
+
 (defun deserialize-object-from-stream (type &key (stream *standard-input*))
-  "Deserializes an object of the given type 'type' as a Protobuf object.
-   'type' is the Lisp name of a Protobufs message (usually the name of a 
-   Lisp class) or a 'protobuf-message'.
-   The return value is the object."
+  "Deserializes an object of the given type 'type' from the given stream
+   as a Protobuf object."
   (let* ((size    (file-length stream))
          (buffer  (make-byte-vector size)))
     (read-sequence buffer stream)
     (deserialize-object type buffer 0 size)))
 
-(defun deserialize-object-from-file (type filename)
-  (with-open-file (stream filename
-                   :direction :input
-                   :element-type '(unsigned-byte 8))
-    (deserialize-object-from-stream type :stream stream)))
+(defun deserialize-object-from-bytes (type buffer)
+  "Deserializes an object of the given type 'type' from the given stream
+   as a Protobuf object.
+   'type' is the Lisp name of a Protobufs message (usually the name of a 
+   Lisp class) or a 'protobuf-message'.
+   The return value is the object."
+  (deserialize-object type buffer))
 
 ;; Allow clients to add their own methods
 ;; This is you might preserve object identity, e.g.
                    `(let ((,vval ,value))
                       (if ,writer
                         (funcall ,writer ,object ,vval)
-                        (setf (slot-value ,object ,slot) ,vval))))))
+                        (setf (slot-value ,object ,slot) ,vval)))))
+               (push-slot (object slot reader writer value)
+                 (with-gensyms (vvals)
+                   `(let ((,vvals (read-slot ,object ,slot ,reader)))
+                      (if (i= (length ,vvals) 0)
+                        ;; We need the initial value to be a stretchy vector,
+                        ;; so scribble over it just to make sure
+                        (let ((,vvals (make-array 1
+                                        :fill-pointer t :adjustable t
+                                        :initial-contents (list ,value))))
+                          (write-slot ,object ,slot ,writer ,vvals))
+                        (vector-push-extend ,value ,vvals))))))
       (labels ((deserialize (type trace end end-tag)
                  (declare (type fixnum end end-tag))
                  (let* ((message (find-message trace type))
                            (setq index (skip-element buffer index tag))
                            ;;--- Check for mismatched wire type, running past end of buffer, etc
                            (cond ((and field (eq (proto-required field) :repeated))
-                                  (cond ((and (proto-packed field) (packed-type-p type))
-                                         (multiple-value-bind (values idx)
-                                             (deserialize-packed type buffer index)
-                                           (setq index idx)
-                                           (write-slot object slot writer values)))
-                                        ((keywordp type)
-                                         (multiple-value-bind (val idx)
-                                             (deserialize-prim type buffer index)
-                                           (setq index idx)
-                                           (pushnew field rslots)
-                                           ;; This "push" could type-check the entire list if
-                                           ;; there's a parameterized list type in effect,
-                                           ;; so you'll want to avoid using such types
-                                           ;; We'll reverse the slots at the last minute
-                                           (write-slot object slot writer
-                                                       (cons val (read-slot object slot reader)))))
-                                        ((typep (setq msg (and type (or (find-message trace type)
-                                                                        (find-enum trace type))))
-                                                'protobuf-message)
-                                         (if (eq (proto-message-type msg) :group)
-                                           (let* ((etag (make-tag $wire-type-end-group fidx))
-                                                  (obj  (deserialize type msg length etag)))
-                                             (pushnew field rslots)
-                                             (write-slot object slot writer
-                                                         (cons obj (read-slot object slot reader))))
-                                           (multiple-value-bind (len idx)
-                                               (decode-uint32 buffer index)
-                                             (setq index idx)
-                                             (let ((obj (deserialize type msg (+ index len) 0)))
-                                               (pushnew field rslots)
-                                               (write-slot object slot writer
-                                                           (cons obj (read-slot object slot reader)))))))
-                                        ((typep msg 'protobuf-enum)
-                                         (if (proto-packed field)
+                                  (let ((vectorp (vector-field-p field)))
+                                    (cond ((and (proto-packed field) (packed-type-p type))
                                            (multiple-value-bind (values idx)
-                                               (deserialize-packed-enum (proto-values msg) buffer index)
+                                               (deserialize-packed type buffer index)
                                              (setq index idx)
-                                             (write-slot object slot writer values))
+                                             (if vectorp
+                                               (let ((values (make-array (length values)
+                                                               :fill-pointer t :adjustable t
+                                                               :initial-contents values)))
+                                                 (write-slot object slot writer values))
+                                               (write-slot object slot writer values))))
+                                          ((keywordp type)
                                            (multiple-value-bind (val idx)
-                                               (deserialize-enum (proto-values msg) buffer index)
+                                               (deserialize-prim type buffer index)
                                              (setq index idx)
-                                             (pushnew field rslots)
-                                             (write-slot object slot writer
-                                                         (cons val (read-slot object slot reader))))))))
+                                             (cond (vectorp
+                                                    (push-slot object slot reader writer val))
+                                                   (t
+                                                    (pushnew field rslots)
+                                                    ;; This "push" could type-check the entire list if
+                                                    ;; there's a parameterized list type in effect,
+                                                    ;; so you'll want to avoid using such types
+                                                    ;; We'll reverse the slots at the last minute
+                                                    (write-slot object slot writer
+                                                                (cons val (read-slot object slot reader)))))))
+                                          ((typep (setq msg (and type (or (find-message trace type)
+                                                                          (find-enum trace type)
+                                                                          (find-type-alias trace type))))
+                                                  'protobuf-message)
+                                           (if (eq (proto-message-type msg) :group)
+                                             (let* ((etag (make-tag $wire-type-end-group fidx))
+                                                    (obj  (deserialize type msg length etag)))
+                                               (cond (vectorp
+                                                      (push-slot object slot reader writer obj))
+                                                     (t
+                                                      (pushnew field rslots)
+                                                      (write-slot object slot writer
+                                                                  (cons obj (read-slot object slot reader))))))
+                                             (multiple-value-bind (len idx)
+                                                 (decode-uint32 buffer index)
+                                               (setq index idx)
+                                               (let ((obj (deserialize type msg (+ index len) 0)))
+                                                 (cond (vectorp
+                                                        (push-slot object slot reader writer obj))
+                                                       (t
+                                                        (pushnew field rslots)
+                                                        (write-slot object slot writer
+                                                                    (cons obj (read-slot object slot reader)))))))))
+                                          ((typep msg 'protobuf-enum)
+                                           (if (proto-packed field)
+                                             (multiple-value-bind (values idx)
+                                                 (deserialize-packed-enum (proto-values msg) buffer index)
+                                               (setq index idx)
+                                               (if vectorp
+                                                 (let ((values (make-array (length values)
+                                                                 :fill-pointer t :adjustable t
+                                                                 :initial-contents values)))
+                                                   (write-slot object slot writer values))
+                                                 (write-slot object slot writer values)))
+                                             (multiple-value-bind (val idx)
+                                                 (deserialize-enum (proto-values msg) buffer index)
+                                               (setq index idx)
+                                               (cond (vectorp
+                                                      (push-slot object slot reader writer val))
+                                                     (t
+                                                      (pushnew field rslots)
+                                                      (write-slot object slot writer
+                                                                  (cons val (read-slot object slot reader))))))))
+                                          ((typep msg 'protobuf-type-alias)
+                                           (let ((type (proto-proto-type msg)))
+                                             (multiple-value-bind (val idx)
+                                                 (deserialize-prim type buffer index)
+                                               (setq index idx)
+                                               (cond (vectorp
+                                                      (push-slot object slot reader writer
+                                                                 (funcall (proto-deserializer msg) val)))
+                                                     (t
+                                                      (pushnew field rslots)
+                                                      (write-slot object slot writer
+                                                                  (cons (funcall (proto-deserializer msg) val)
+                                                                        (read-slot object slot reader)))))))))))
                                  (t
                                   (cond ((keywordp type)
                                          (multiple-value-bind (val idx)
                                            (setq index idx)
                                            (write-slot object slot writer val)))
                                         ((typep (setq msg (and type (or (find-message trace type)
-                                                                        (find-enum trace type))))
+                                                                        (find-enum trace type)
+                                                                        (find-type-alias trace type))))
                                                 'protobuf-message)
                                          ;;--- If there's already a value in the slot, merge messages
                                          (if (eq (proto-message-type msg) :group)
                                          (multiple-value-bind (val idx)
                                              (deserialize-enum (proto-values msg) buffer index)
                                            (setq index idx)
-                                           (write-slot object slot writer val)))))))))))))
+                                           (write-slot object slot writer val)))
+                                        ((typep msg 'protobuf-type-alias)
+                                         (let ((type (proto-proto-type msg)))
+                                           (multiple-value-bind (val idx)
+                                               (deserialize-prim type buffer index)
+                                             (setq index idx)
+                                             (write-slot object slot writer
+                                                         (funcall (proto-deserializer msg) val)))))))))))))))
         (declare (dynamic-extent #'deserialize))
         (deserialize (proto-class message) message length end-tag)))))
 
+
 ;;; Object sizes
 
 ;; Allow clients to add their own methods
     (declare (type fixnum size))
     (macrolet ((read-slot (object slot reader)
                  ;; Don't do a boundp check, we assume the object is fully populated
-                 ;; Unpopulated slots should be "nullable" and should contain nil
+                 ;; Unpopulated slots should be "nullable" and will contain nil when empty
                  `(if ,reader
                     (funcall ,reader ,object)
                     (slot-value ,object ,slot))))
                                      (iincf size (packed-size (read-slot object slot reader) type tag))))
                                   ((keywordp type)
                                    (let ((tag (make-tag type (proto-index field))))
-                                     (map () #'(lambda (v)
-                                                 (iincf size (prim-size v type tag)))
-                                             (read-slot object slot reader))))
+                                     (doseq (v (read-slot object slot reader))
+                                       (iincf size (prim-size v type tag)))))
                                   ((typep (setq msg (and type (or (find-message trace type)
-                                                                  (find-enum trace type))))
+                                                                  (find-enum trace type)
+                                                                  (find-type-alias trace type))))
                                           'protobuf-message)
                                    (if (eq (proto-message-type msg) :group)
-                                     (dolist (v (if slot (read-slot object slot reader) (list object)))
+                                     (doseq (v (if slot (read-slot object slot reader) (list object)))
                                        (let ((tag1 (make-tag $wire-type-start-group (proto-index field)))
                                              (tag2 (make-tag $wire-type-end-group   (proto-index field))))
                                          (iincf size (length32 tag1))
-                                         (map () (curry #'do-field v msg)
-                                                 (proto-fields msg))
+                                         (dolist (f (proto-fields msg))
+                                           (do-field v msg f))
                                          (iincf size (length32 tag2))))
-                                     (dolist (v (if slot (read-slot object slot reader) (list object)))
+                                     (doseq (v (if slot (read-slot object slot reader) (list object)))
                                        (let ((tag (make-tag $wire-type-string (proto-index field)))
                                              (len (object-size v msg visited)))
                                          (iincf size (length32 tag))
                                          (iincf size (length32 len))
-                                         (map () (curry #'do-field v msg)
-                                                 (proto-fields msg))))))
+                                         (dolist (f (proto-fields msg))
+                                           (do-field v msg f))))))
                                   ((typep msg 'protobuf-enum)
                                    (let ((tag (make-tag $wire-type-varint (proto-index field))))
                                      (if (proto-packed field)
                                        (iincf size (packed-enum-size (read-slot object slot reader) type tag))
-                                       (map () #'(lambda (v)
-                                                   (iincf size (enum-size v (proto-values msg) tag)))
-                                               (read-slot object slot reader)))))))
+                                       (doseq (v (read-slot object slot reader))
+                                         (iincf size (enum-size v (proto-values msg) tag))))))
+                                  ((typep msg 'protobuf-type-alias)
+                                   (let* ((type (proto-proto-type msg))
+                                          (tag  (make-tag type (proto-index field))))
+                                     (doseq (v (read-slot object slot reader))
+                                       (let ((v (funcall (proto-serializer msg) v)))
+                                         (iincf size (prim-size v type tag))))))
+                                  (t
+                                   (undefined-field-type "While computing the size of ~S,"
+                                                         object type field))))
                            (t
                             (cond ((eq type :bool)
                                    (let ((v (cond ((or (eq (proto-required field) :required)
                                          (iincf size (prim-size v type tag))))))
                                   ((keywordp type)
                                    (let ((v (read-slot object slot reader)))
-                                     (when v
+                                     (when (and v (not (equal v (proto-default field))))
                                        (let ((tag (make-tag type (proto-index field))))
                                          (iincf size (prim-size v type tag))))))
                                   ((typep (setq msg (and type (or (find-message trace type)
-                                                                  (find-enum trace type))))
+                                                                  (find-enum trace type)
+                                                                  (find-type-alias trace type))))
                                           'protobuf-message)
                                    (let ((v (if slot (read-slot object slot reader) object)))
                                      (when v
                                          (let ((tag1 (make-tag $wire-type-start-group (proto-index field)))
                                              (tag2 (make-tag $wire-type-end-group   (proto-index field))))
                                            (iincf size (length32 tag1))
-                                           (map () (curry #'do-field v msg)
-                                                   (proto-fields msg))
+                                           (dolist (f (proto-fields msg))
+                                             (do-field v msg f))
                                            (iincf size (length32 tag2)))
                                          (let ((tag (make-tag $wire-type-string (proto-index field)))
                                                (len (object-size v msg visited)))
                                            (iincf size (length32 tag))
                                            (iincf size (length32 len))
-                                           (map () (curry #'do-field v msg)
-                                                (proto-fields msg)))))))
+                                           (dolist (f (proto-fields msg))
+                                             (do-field v msg f)))))))
                                   ((typep msg 'protobuf-enum)
                                    (let ((v (read-slot object slot reader)))
-                                     (when v
+                                     (when (and v (not (eql v (proto-default field))))
                                        (let ((tag (make-tag $wire-type-varint (proto-index field))))
-                                         (iincf size (enum-size (read-slot object slot reader) (proto-values msg) tag)))))))))))))
+                                         (iincf size (enum-size (read-slot object slot reader) (proto-values msg) tag))))))
+                                  ((typep msg 'protobuf-type-alias)
+                                   (let ((v (read-slot object slot reader)))
+                                     (when v
+                                       (let* ((v    (funcall (proto-serializer msg) v))
+                                              (type (proto-proto-type msg))
+                                              (tag  (make-tag type (proto-index field))))
+                                         (iincf size (prim-size v type tag))))))
+                                  (t
+                                   (undefined-field-type "While computing the size of ~S,"
+                                                         object type field)))))))))
         (declare (dynamic-extent #'do-field))
-        (map () (curry #'do-field object message) (proto-fields message))
+        (dolist (field (proto-fields message))
+          (do-field object message field))
         (when visited
           (setf (gethash object visited) size))   ;cache the size
         size))))
       (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))
         (let* ((class  (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
                (msg    (and class (not (keywordp class))
                             (or (find-message message class)
-                                (find-enum message class))))
+                                (find-enum message class)
+                                (find-type-alias message class))))
                (reader (cond ((proto-reader field)
                               `(,(proto-reader field) ,vobj))
                              ((proto-value field)
                (index  (proto-index field)))
           (when reader
             (cond ((eq (proto-required field) :repeated)
-                   (cond ((and (proto-packed field) (packed-type-p class))
-                          (collect-serializer
-                           (let ((tag (make-tag class index)))
-                             `(setq ,vidx (serialize-packed ,reader ,class ,tag ,vbuf ,vidx)))))
-                         ((keywordp class)
-                          (collect-serializer
-                           (let ((tag (make-tag class index)))
-                             `(dolist (,vval ,reader)
-                                (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))
-                         ((typep msg 'protobuf-message)
-                          (collect-serializer
-                           (if (eq (proto-message-type msg) :group)
-                             (let ((tag1 (make-tag $wire-type-start-group index))
-                                   (tag2 (make-tag $wire-type-end-group   index)))
-                               `(dolist (,vval ,reader)
-                                  (let ((len (or (and visited (gethash ,vval visited))
-                                                 (object-size ,vval ,msg visited))))
-                                    (setq ,vidx (encode-uint32 ,tag1 ,vbuf ,vidx))
-                                    (serialize-object ,vval ,msg ,vbuf ,vidx visited)
-                                    (iincf ,vidx len)
-                                    (setq ,vidx (encode-uint32 ,tag2 ,vbuf ,vidx)))))
-                             (let ((tag (make-tag $wire-type-string index)))
-                               `(dolist (,vval ,reader)
-                                  (let ((len (or (and visited (gethash ,vval visited))
-                                                 (object-size ,vval ,msg visited))))
-                                    (setq ,vidx (encode-uint32 ,tag ,vbuf ,vidx))
-                                    (setq ,vidx (encode-uint32 len ,vbuf ,vidx))
-                                    (serialize-object ,vval ,msg ,vbuf ,vidx visited)
-                                    (iincf ,vidx len)))))))
-                         ((typep msg 'protobuf-enum)
-                          (collect-serializer
-                           (let ((tag (make-tag $wire-type-varint index)))
-                             (if (proto-packed field)
-                               `(setq ,vidx (serialize-packed-enum ,reader '(,@(proto-values msg)) ,tag ,vbuf ,vidx))
+                   (let* ((vectorp  (vector-field-p field))
+                          (iterator (if vectorp 'dovector 'dolist)))
+                     (cond ((and (proto-packed field) (packed-type-p class))
+                            (collect-serializer
+                             (let ((tag (make-tag class index)))
+                               `(setq ,vidx (serialize-packed ,reader ,class ,tag ,vbuf ,vidx
+                                                              ,vectorp)))))
+                           ((keywordp class)
+                            (collect-serializer
+                             (let ((tag (make-tag class index)))
+                               `(,iterator (,vval ,reader)
+                                  (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))
+                           ((typep msg 'protobuf-message)
+                            (collect-serializer
+                             (if (eq (proto-message-type msg) :group)
+                               (let ((tag1 (make-tag $wire-type-start-group index))
+                                     (tag2 (make-tag $wire-type-end-group   index)))
+                                 `(,iterator (,vval ,reader)
+                                    (let ((len (or (and visited (gethash ,vval visited))
+                                                   (object-size ,vval ,msg visited))))
+                                      (setq ,vidx (encode-uint32 ,tag1 ,vbuf ,vidx))
+                                      (serialize-object ,vval ,msg ,vbuf ,vidx visited)
+                                      (iincf ,vidx len)
+                                      (setq ,vidx (encode-uint32 ,tag2 ,vbuf ,vidx)))))
+                               (let ((tag (make-tag $wire-type-string index)))
+                                 `(,iterator (,vval ,reader)
+                                    (let ((len (or (and visited (gethash ,vval visited))
+                                                   (object-size ,vval ,msg visited))))
+                                      (setq ,vidx (encode-uint32 ,tag ,vbuf ,vidx))
+                                      (setq ,vidx (encode-uint32 len ,vbuf ,vidx))
+                                      (serialize-object ,vval ,msg ,vbuf ,vidx visited)
+                                      (iincf ,vidx len)))))))
+                           ((typep msg 'protobuf-enum)
+                            (collect-serializer
+                             (let ((tag (make-tag $wire-type-varint index)))
+                               (if (proto-packed field)
+                                 `(setq ,vidx (serialize-packed-enum ,reader '(,@(proto-values msg)) ,tag ,vbuf ,vidx
+                                                                     ,vectorp))
 
-                               `(dolist (,vval ,reader)
-                                  (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx)))))))))
+                                 `(,iterator (,vval ,reader)
+                                    (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx)))))))
+                           ((typep msg 'protobuf-type-alias)
+                            (collect-serializer
+                             (let* ((class (proto-proto-type msg))
+                                    (tag   (make-tag class (proto-index field))))
+                               `(,iterator (,vval ,reader)
+                                  (let ((,vval (funcall #',(proto-serializer msg) ,vval)))
+                                    (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx)))))))
+                           (t
+                            (undefined-field-type "While generating 'serialize-object' for ~S,"
+                                                  message class field)))))
                   (t
                    (cond ((keywordp class)
                           (collect-serializer
                            (let ((tag (make-tag class index)))
                              (if (eq class :bool)
                                (if (or (eq (proto-required field) :required)
-                                       reader)
+                                       (null (proto-value field)))
                                  `(let ((,vval ,reader))
                                     (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx)))
                                  `(let ((,vval (cond ((slot-boundp ,vobj ',(proto-value field))
                                                      (t :unbound))))
                                     (unless (eq ,vval :unbound)
                                       (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx)))))
-                               `(let ((,vval ,reader))
-                                  (when ,vval
-                                    (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))))
+                               (if (empty-default-p field)
+                                 `(let ((,vval ,reader))
+                                    (when ,vval
+                                      (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))
+                                 `(let ((,vval ,reader))
+                                    (when (and ,vval (not (equal ,vval ',(proto-default field))))
+                                      (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx)))))))))
                          ((typep msg 'protobuf-message)
                           (collect-serializer
                            (if (eq (proto-message-type msg) :group)
                          ((typep msg 'protobuf-enum)
                           (collect-serializer
                            (let ((tag (make-tag $wire-type-varint index)))
+                             (if (empty-default-p field)
+                               `(let ((,vval ,reader))
+                                  (when ,vval
+                                    (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx))))
+                               `(let ((,vval ,reader))
+                                  (when (and ,vval (not (eql ,vval ',(proto-default field))))
+                                    (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx))))))))
+                         ((typep msg 'protobuf-type-alias)
+                          (collect-serializer
+                           (let* ((class (proto-proto-type msg))
+                                  (tag   (make-tag class (proto-index field))))
                              `(let ((,vval ,reader))
                                 (when ,vval
-                                  (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx)))))))))))))
+                                  (let ((,vval (funcall #',(proto-serializer msg) ,vval)))
+                                    (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))))
+                         (t
+                          (undefined-field-type "While generating 'serialize-object' for ~S,"
+                                                message class field))))))))
       `(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)))
           (let* ((class  (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
                  (msg    (and class (not (keywordp class))
                               (or (find-message message class)
-                                  (find-enum message class))))
+                                  (find-enum message class)
+                                  (find-type-alias message class))))
                  (index  (proto-index field)))
             (cond ((eq (proto-required field) :repeated)
                    (cond ((and (proto-packed field) (packed-type-p class))
                              (multiple-value-bind (,vval idx)
                                  (deserialize-packed ,class ,vbuf ,vidx)
                                (setq ,vidx idx)
+                               ,@(when (vector-field-p field)
+                                   `((setq ,vval (make-array (length ,vval)
+                                                   :fill-pointer t :adjustable t
+                                                   :initial-contents ,vval))))
                                ,(write-slot vobj field vval)))))
                          ((keywordp class)
                           (let ((temp (gensym (string (proto-value field)))))
                                  (multiple-value-bind (,vval idx)
                                      (deserialize-enum '(,@(proto-values msg)) ,vbuf ,vidx)
                                    (setq ,vidx idx)
-                                   (push ,vval ,temp)))))))))
+                                   (push ,vval ,temp)))))))
+                         ((typep msg 'protobuf-type-alias)
+                          (let ((class (proto-proto-type msg))
+                                (temp  (gensym (string (proto-value field)))))
+                            (collect-rslot (list field temp))
+                            (collect-deserializer
+                             `((,(make-tag class index))
+                               (multiple-value-bind (,vval idx)
+                                   (deserialize-prim ,class ,vbuf ,vidx)
+                                 (setq ,vidx idx)
+                                 (push (funcall #',(proto-deserializer msg) ,vval) ,temp))))))
+                         (t
+                          (undefined-field-type "While generating 'deserialize-object' for ~S,"
+                                                message class field))))
                   (t
                    (cond ((keywordp class)
                           (collect-deserializer
                              (multiple-value-bind (,vval idx)
                                  (deserialize-enum '(,@(proto-values msg)) ,vbuf ,vidx)
                                (setq ,vidx idx)
-                               ,(write-slot vobj field vval)))))))))))
+                               ,(write-slot vobj field vval)))))
+                         ((typep msg 'protobuf-type-alias)
+                          (let ((class (proto-proto-type msg)))
+                            (collect-deserializer
+                             `((,(make-tag class index))
+                               (multiple-value-bind (,vval idx)
+                                     (deserialize-prim ,class ,vbuf ,vidx)
+                                   (let ((,vval (funcall #',(proto-deserializer msg) ,vval)))
+                                     (setq ,vidx idx)
+                                     ,(write-slot vobj field vval)))))))
+                         (t
+                          (undefined-field-type "While generating 'deserialize-object' for ~S,"
+                                                message class field))))))))
       (let* ((rslots  (delete-duplicates rslots :key #'first))
              (rfields (mapcar #'first  rslots))
              (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))))
                              for temp in rtemps
                              as slot = (proto-value field)
                              as writer = (proto-writer field)
-                             collect (if writer
-                                       `(funcall ,writer ,vobj (nreverse ,temp))
-                                       `(setf (slot-value ,vobj ',slot) (nreverse ,temp))))
+                             collect (cond ((vector-field-p field)
+                                            (if writer
+                                              `(funcall ,writer ,vobj (make-array (length ,temp)
+                                                                        :fill-pointer t :adjustable t
+                                                                        :initial-contents (nreverse ,temp)))
+                                              `(setf (slot-value ,vobj ',slot) (make-array (length ,temp)
+                                                                                 :fill-pointer t :adjustable t
+                                                                                 :initial-contents (nreverse ,temp)))))
+                                           (t
+                                            (if writer
+                                              `(funcall ,writer ,vobj (nreverse ,temp))
+                                              `(setf (slot-value ,vobj ',slot) (nreverse ,temp))))))
                      (return-from deserialize-object
                        (values ,vobj ,vidx)))
                    (case tag
       (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))
         (let* ((class  (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
                (msg    (and class (not (keywordp class))
                             (or (find-message message class)
-                                (find-enum message class))))
+                                (find-enum message class)
+                                (find-type-alias message class))))
                (reader (cond ((proto-reader field)
                               `(,(proto-reader field) ,vobj))
                              ((proto-value field)
                (index  (proto-index field)))
           (when reader
             (cond ((eq (proto-required field) :repeated)
-                   (cond ((and (proto-packed field) (packed-type-p class))
-                          (collect-sizer
-                           (let ((tag (make-tag class index)))
-                             `(iincf ,vsize (packed-size ,reader ,class ,tag)))))
-                         ((keywordp class)
-                          (collect-sizer
-                           (let ((tag (make-tag class index)))
-                             `(dolist (,vval ,reader)
-                                (iincf ,vsize (prim-size ,vval ,class ,tag))))))
-                         ((typep msg 'protobuf-message)
-                          (collect-sizer
-                           (if (eq (proto-message-type msg) :group)
-                             (let ((tag1 (make-tag $wire-type-start-group index))
-                                   (tag2 (make-tag $wire-type-end-group   index)))
-                               `(dolist (,vval ,reader)
-                                  (let ((len (or (and visited (gethash ,vval visited))
-                                                 (object-size ,vval ,msg visited))))
-                                    (iincf ,vsize (length32 ,tag1))
-                                    (iincf ,vsize len)
-                                    (iincf ,vsize ,tag2))))
-                             (let ((tag (make-tag $wire-type-string index)))
-                               `(dolist (,vval ,reader)
-                                  (let ((len (or (and visited (gethash ,vval visited))
-                                                 (object-size ,vval ,msg visited))))
-                                    (iincf ,vsize (length32 ,tag))
-                                    (iincf ,vsize (length32 len))
-                                    (iincf ,vsize len)))))))
-                         ((typep msg 'protobuf-enum)
-                          (let ((tag (make-tag $wire-type-varint index)))
+                   (let* ((vectorp  (vector-field-p field))
+                          (iterator (if vectorp 'dovector 'dolist)))
+                     (cond ((and (proto-packed field) (packed-type-p class))
+                            (collect-sizer
+                             (let ((tag (make-tag class index)))
+                               `(iincf ,vsize (packed-size ,reader ,class ,tag ,vectorp)))))
+                           ((keywordp class)
+                            (collect-sizer
+                             (let ((tag (make-tag class index)))
+                               `(,iterator (,vval ,reader)
+                                  (iincf ,vsize (prim-size ,vval ,class ,tag))))))
+                           ((typep msg 'protobuf-message)
                             (collect-sizer
-                             (if (proto-packed field)
-                               `(iincf ,vsize (packed-enum-size ,reader '(,@(proto-values msg)) ,tag))
-                               `(dolist (,vval ,reader)
-                                  (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag)))))))))
+                             (if (eq (proto-message-type msg) :group)
+                               (let ((tag1 (make-tag $wire-type-start-group index))
+                                     (tag2 (make-tag $wire-type-end-group   index)))
+                                 `(,iterator (,vval ,reader)
+                                    (let ((len (or (and visited (gethash ,vval visited))
+                                                   (object-size ,vval ,msg visited))))
+                                      (iincf ,vsize (length32 ,tag1))
+                                      (iincf ,vsize len)
+                                      (iincf ,vsize ,tag2))))
+                               (let ((tag (make-tag $wire-type-string index)))
+                                 `(,iterator (,vval ,reader)
+                                    (let ((len (or (and visited (gethash ,vval visited))
+                                                   (object-size ,vval ,msg visited))))
+                                      (iincf ,vsize (length32 ,tag))
+                                      (iincf ,vsize (length32 len))
+                                      (iincf ,vsize len)))))))
+                           ((typep msg 'protobuf-enum)
+                            (let ((tag (make-tag $wire-type-varint index)))
+                              (collect-sizer
+                               (if (proto-packed field)
+                                 `(iincf ,vsize (packed-enum-size ,reader '(,@(proto-values msg)) ,tag))
+                                 `(,iterator (,vval ,reader)
+                                    (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag)))))))
+                           ((typep msg 'protobuf-type-alias)
+                            (collect-sizer
+                             (let* ((class (proto-proto-type msg))
+                                    (tag   (make-tag class index)))
+                               `(,iterator (,vval ,reader)
+                                  (let ((,vval (funcall #',(proto-serializer msg) ,vval)))
+                                    (iincf ,vsize (prim-size ,vval ,class ,tag)))))))
+                           (t
+                            (undefined-field-type "While generating 'object-size' for ~S,"
+                                                  message class field)))))
                   (t
                    (cond ((keywordp class)
                           (let ((tag (make-tag class index)))
                             (collect-sizer
                              (if (eq class :bool)
                                (if (or (eq (proto-required field) :required)
-                                       reader)
+                                       (null (proto-value field)))
                                  `(let ((,vval ,reader))
                                     (declare (ignorable ,vval))
                                     (iincf ,vsize (prim-size ,vval ,class ,tag)))
                                                      (t :unbound))))
                                     (unless (eq ,vval :unbound)
                                       (iincf ,vsize (prim-size ,vval ,class ,tag)))))
-                               `(let ((,vval ,reader))
-                                  (when ,vval
-                                    (iincf ,vsize (prim-size ,vval ,class ,tag))))))))
+                               (if (empty-default-p field)
+                                 `(let ((,vval ,reader))
+                                    (when ,vval
+                                      (iincf ,vsize (prim-size ,vval ,class ,tag))))
+                                 `(let ((,vval ,reader))
+                                    (when (and ,vval (not (equal ,vval ',(proto-default field))))
+                                      (iincf ,vsize (prim-size ,vval ,class ,tag)))))))))
                          ((typep msg 'protobuf-message)
                           (collect-sizer
                            (if (eq (proto-message-type msg) :group)
                          ((typep msg 'protobuf-enum)
                           (let ((tag (make-tag $wire-type-varint index)))
                             (collect-sizer
+                             (if (empty-default-p field)
+                               `(let ((,vval ,reader))
+                                  (when ,vval
+                                    (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag))))
+                               `(let ((,vval ,reader))
+                                  (when (and ,vval (not (eql ,vval ',(proto-default field))))
+                                    (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag))))))))
+                         ((typep msg 'protobuf-type-alias)
+                          (collect-sizer
+                           (let* ((class (proto-proto-type msg))
+                                  (tag   (make-tag class index)))
                              `(let ((,vval ,reader))
                                 (when ,vval
-                                  (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag)))))))))))))
+                                  (iincf ,vsize (prim-size (funcall #',(proto-serializer msg) ,vval)
+                                                           ,class ,tag)))))))
+                         (t
+                          (undefined-field-type "While generating 'object-size' for ~S,"
+                                                message class field))))))))
       `(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