Update pb_msgdesc_t definition to improve performance.

1. Include precalculated required field count and largest tag, to avoid calculating them at runtime.
2. Reorder fields for better natural alignment (pointers first in struct)
3. Remove PB_PACKED_STRUCT specification, no longer needed.
diff --git a/docs/migration.rst b/docs/migration.rst
index 2b00fba..29fb5d3 100644
--- a/docs/migration.rst
+++ b/docs/migration.rst
@@ -11,7 +11,22 @@
 
 .. contents ::
 
-Nanopb-0.4.2 (2020-xx-xx)
+Nanopb-0.4.3 (2020-xx-xx)
+=========================
+
+pb_msgdesc_t struct has new fields
+----------------------------------
+
+**Changes:** New fields `required_field_count` and `largest_tag` were added
+to `pb_msgdesc_t` and existing fields were reordered.
+
+**Required actions:** All `.pb.c` files must be recompiled. Regeneration is not
+needed.
+
+**Error indications:** Messages may fail to encode or decode, or the code can
+crash inside `load_descriptor_values()` in `pb_common.c`.
+
+Nanopb-0.4.2 (2020-06-23)
 =========================
 
 Generator now uses Python 3 by default
diff --git a/pb.h b/pb.h
index cc78303..3cc130f 100644
--- a/pb.h
+++ b/pb.h
@@ -276,17 +276,18 @@
 /* This structure is used in auto-generated constants
  * to specify struct fields.
  */
-PB_PACKED_STRUCT_START
 typedef struct pb_msgdesc_s pb_msgdesc_t;
 struct pb_msgdesc_s {
-    pb_size_t field_count;
     const uint32_t *field_info;
     const pb_msgdesc_t * const * submsg_info;
     const pb_byte_t *default_value;
 
     bool (*field_callback)(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_iter_t *field);
-} pb_packed;
-PB_PACKED_STRUCT_END
+
+    pb_size_t field_count;
+    pb_size_t required_field_count;
+    pb_size_t largest_tag;
+};
 
 /* Iterator for message descriptor */
 struct pb_field_iter_s {
@@ -469,15 +470,21 @@
     }; \
     const pb_msgdesc_t structname ## _msg = \
     { \
-       0 msgname ## _FIELDLIST(PB_GEN_FIELD_COUNT, structname), \
        structname ## _field_info, \
        structname ## _submsg_info, \
        msgname ## _DEFAULT, \
        msgname ## _CALLBACK, \
+       0 msgname ## _FIELDLIST(PB_GEN_FIELD_COUNT, structname), \
+       0 msgname ## _FIELDLIST(PB_GEN_REQ_FIELD_COUNT, structname), \
+       0 msgname ## _FIELDLIST(PB_GEN_LARGEST_TAG, structname), \
     }; \
     msgname ## _FIELDLIST(PB_GEN_FIELD_INFO_ASSERT_ ## width, structname)
 
 #define PB_GEN_FIELD_COUNT(structname, atype, htype, ltype, fieldname, tag) +1
+#define PB_GEN_REQ_FIELD_COUNT(structname, atype, htype, ltype, fieldname, tag) \
+    + (PB_HTYPE_ ## htype == PB_HTYPE_REQUIRED)
+#define PB_GEN_LARGEST_TAG(structname, atype, htype, ltype, fieldname, tag) \
+    * 0 + tag
 
 /* X-macro for generating the entries in struct_field_info[] array. */
 #define PB_GEN_FIELD_INFO_1(structname, atype, htype, ltype, fieldname, tag) \
diff --git a/pb_common.c b/pb_common.c
index c718eb1..6aee76b 100644
--- a/pb_common.c
+++ b/pb_common.c
@@ -198,6 +198,10 @@
     {
         return true; /* Nothing to do, correct field already. */
     }
+    else if (tag > iter->descriptor->largest_tag)
+    {
+        return false;
+    }
     else
     {
         pb_size_t start = iter->index;
diff --git a/pb_decode.c b/pb_decode.c
index 8055295..2dab0e3 100644
--- a/pb_decode.c
+++ b/pb_decode.c
@@ -1091,27 +1091,15 @@
 
     /* Check that all required fields were present. */
     {
-        /* First figure out the number of required fields by
-         * seeking to the end of the field array. Usually we
-         * are already close to end after decoding.
-         */
-        pb_size_t req_field_count;
-        pb_type_t last_type;
-        pb_size_t i;
-        do {
-            req_field_count = iter.required_field_index;
-            last_type = iter.type;
-        } while (pb_field_iter_next(&iter));
-
-        /* Fixup if last field was also required. */
-        if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.tag != 0)
-            req_field_count++;
-
-        if (req_field_count > PB_MAX_REQUIRED_FIELDS)
-            req_field_count = PB_MAX_REQUIRED_FIELDS;
+        pb_size_t req_field_count = iter.descriptor->required_field_count;
 
         if (req_field_count > 0)
         {
+            pb_size_t i;
+
+            if (req_field_count > PB_MAX_REQUIRED_FIELDS)
+                req_field_count = PB_MAX_REQUIRED_FIELDS;
+
             /* Check the whole words */
             for (i = 0; i < (req_field_count >> 5); i++)
             {