diff --git a/upb/mini_table.c b/upb/mini_table.c index 8bcaf7449c..af6cb2a860 100644 --- a/upb/mini_table.c +++ b/upb/mini_table.c @@ -638,29 +638,26 @@ static void upb_MtDecoder_ParseMessage(upb_MtDecoder* d, const char* data, upb_MtDecoder_AllocateSubs(d, sub_count); } -#define UPB_COMPARE_INTEGERS(a, b) ((a) < (b) ? -1 : ((a) == (b) ? 0 : 1)) -#define UPB_COMBINE(rep, ty, idx) (((rep << type_bits) | ty) << idx_bits) | idx - int upb_MtDecoder_CompareFields(const void* _a, const void* _b) { const upb_LayoutItem* a = _a; const upb_LayoutItem* b = _b; // Currently we just sort by: // 1. rep (smallest fields first) // 2. type (oneof cases first) - // 2. field_number (smallest numbers first) + // 2. field_index (smallest numbers first) // The main goal of this is to reduce space lost to padding. // Later we may have more subtle reasons to prefer a different ordering. const int rep_bits = _upb_Log2Ceiling(kUpb_FieldRep_Max); const int type_bits = _upb_Log2Ceiling(kUpb_LayoutItemType_Max); const int idx_bits = (sizeof(a->field_index) * 8); UPB_ASSERT(idx_bits + rep_bits + type_bits < 32); +#define UPB_COMBINE(rep, ty, idx) (((rep << type_bits) | ty) << idx_bits) | idx uint32_t a_packed = UPB_COMBINE(a->rep, a->type, a->field_index); uint32_t b_packed = UPB_COMBINE(b->rep, b->type, b->field_index); - return UPB_COMPARE_INTEGERS(a_packed, b_packed); -} - + assert(a_packed != b_packed); #undef UPB_COMBINE -#undef UPB_COMPARE_INTEGERS + return a < b ? -1 : 1; +} static bool upb_MtDecoder_SortLayoutItems(upb_MtDecoder* d) { // Add items for all non-oneof fields (oneofs were already added). diff --git a/upb/upb.h b/upb/upb.h index ba15451229..50bb0cb1cf 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -223,7 +223,7 @@ UPB_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) { return ret; } -// Call to shrink the last alloc from this arena. +// Shrinks the last alloc from arena. // REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena. // We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if // this was not the last alloc.