Skip to content

Commit

Permalink
upb: move ElemSizeLg2() from message/ into mini_table/
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 584380544
  • Loading branch information
ericsalo authored and copybara-github committed Nov 21, 2023
1 parent e81cda1 commit a49f6b8
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion upb/message/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ cc_library(
"internal/map_entry.h",
"internal/map_sorter.h",
"internal/message.h",
"internal/size_log2.h",
"map_gencode_util.h",
],
copts = UPB_DEFAULT_COPTS,
Expand All @@ -121,6 +120,7 @@ cc_library(
"//upb:hash",
"//upb:mem",
"//upb:mini_table",
"//upb:mini_table_internal",
"//upb:port",
],
)
Expand Down
3 changes: 2 additions & 1 deletion upb/message/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "upb/mini_table/enum.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"

// Must be last.
#include "upb/mini_table/internal/field.h"
Expand Down Expand Up @@ -378,7 +379,7 @@ UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray(
_upb_MiniTableField_CheckIsArray(field);
upb_Array* array = upb_Message_GetMutableArray(msg, field);
if (!array) {
array = _upb_Array_New(arena, 4, _upb_MiniTable_ElementSizeLg2(field));
array = _upb_Array_New(arena, 4, _upb_MiniTableField_ElemSizeLg2(field));
// Check again due to: https://godbolt.org/z/7WfaoKG1r
_upb_MiniTableField_CheckIsArray(field);
upb_MessageValue val;
Expand Down
4 changes: 2 additions & 2 deletions upb/message/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
#include "upb/base/descriptor_constants.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/array.h"
#include "upb/message/internal/size_log2.h"
#include "upb/mini_table/internal/size_log2.h"

// Must be last.
#include "upb/port/def.inc"

upb_Array* upb_Array_New(upb_Arena* a, upb_CType type) {
return _upb_Array_New(a, 4, upb_SizeLog2_CType(type));
return _upb_Array_New(a, 4, upb_CType_SizeLg2(type));
}

const void* upb_Array_DataPtr(const upb_Array* arr) {
Expand Down
4 changes: 2 additions & 2 deletions upb/message/copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/size_log2.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/message/tagged_ptr.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/message.h"
#include "upb/mini_table/internal/size_log2.h"
#include "upb/mini_table/message.h"

// Must be last.
Expand Down Expand Up @@ -143,7 +143,7 @@ upb_Array* upb_Array_DeepClone(const upb_Array* array, upb_CType value_type,
const upb_MiniTable* sub, upb_Arena* arena) {
size_t size = array->size;
upb_Array* cloned_array =
_upb_Array_New(arena, size, upb_SizeLog2_CType(value_type));
_upb_Array_New(arena, size, upb_CType_SizeLg2(value_type));
if (!cloned_array) {
return NULL;
}
Expand Down
8 changes: 0 additions & 8 deletions upb/message/internal/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
#include <stdint.h>
#include <string.h>

#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/array.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/size_log2.h"
#include "upb/message/internal/types.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
Expand Down Expand Up @@ -185,12 +183,6 @@ UPB_INLINE void _upb_MiniTable_CopyFieldData(void* to, const void* from,
UPB_UNREACHABLE();
}

UPB_INLINE size_t
_upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) {
return upb_SizeLog2_FieldType(
(upb_FieldType)field->UPB_PRIVATE(descriptortype));
}

// Here we define universal getter/setter functions for message fields.
// These look very branchy and inefficient, but as long as the MiniTableField
// values are known at compile time, all the branches are optimized away and
Expand Down
1 change: 1 addition & 0 deletions upb/mini_table/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ cc_library(
"internal/field.h",
"internal/file.h",
"internal/message.h",
"internal/size_log2.h",
"internal/sub.h",
],
visibility = ["//visibility:public"],
Expand Down
8 changes: 8 additions & 0 deletions upb/mini_table/internal/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#ifndef UPB_MINI_TABLE_INTERNAL_FIELD_H_
#define UPB_MINI_TABLE_INTERNAL_FIELD_H_

#include <stddef.h>
#include <stdint.h>

#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/size_log2.h"

// Must be last.
#include "upb/port/def.inc"
Expand Down Expand Up @@ -95,6 +97,12 @@ UPB_INLINE void _upb_MiniTableField_CheckIsMap(
UPB_ASSUME(field->presence == 0);
}

UPB_INLINE size_t
_upb_MiniTableField_ElemSizeLg2(const struct upb_MiniTableField* field) {
return upb_FieldType_SizeLg2(
(upb_FieldType)field->UPB_PRIVATE(descriptortype));
}

#ifdef __cplusplus
} /* extern "C" */
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

#ifndef UPB_MESSAGE_INTERNAL_SIZE_LOG2_H_
#define UPB_MESSAGE_INTERNAL_SIZE_LOG2_H_
#ifndef UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_
#define UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_

#include <stddef.h>
#include <stdint.h>
Expand All @@ -22,7 +22,7 @@ extern "C" {
#endif

// Return the log2 of the storage size in bytes for a upb_CType
UPB_INLINE int upb_SizeLog2_CType(upb_CType c_type) {
UPB_INLINE int upb_CType_SizeLg2(upb_CType c_type) {
static const int8_t size[] = {
0, // kUpb_CType_Bool
2, // kUpb_CType_Float
Expand All @@ -42,7 +42,7 @@ UPB_INLINE int upb_SizeLog2_CType(upb_CType c_type) {
}

// Return the log2 of the storage size in bytes for a upb_FieldType
UPB_INLINE int upb_SizeLog2_FieldType(upb_FieldType field_type) {
UPB_INLINE int upb_FieldType_SizeLg2(upb_FieldType field_type) {
static const int8_t size[] = {
3, // kUpb_FieldType_Double
2, // kUpb_FieldType_Float
Expand Down Expand Up @@ -74,4 +74,4 @@ UPB_INLINE int upb_SizeLog2_FieldType(upb_FieldType field_type) {

#include "upb/port/undef.inc"

#endif /* UPB_MESSAGE_INTERNAL_SIZE_LOG2_H_ */
#endif /* UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_ */
4 changes: 2 additions & 2 deletions upb/wire/decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "upb/message/internal/map.h"
#include "upb/message/internal/map_entry.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/size_log2.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/message/tagged_ptr.h"
Expand All @@ -35,6 +34,7 @@
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/message.h"
#include "upb/mini_table/internal/size_log2.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
#include "upb/port/atomic.h"
Expand Down Expand Up @@ -476,7 +476,7 @@ static const char* _upb_Decoder_DecodeEnumPacked(
upb_Array* _upb_Decoder_CreateArray(upb_Decoder* d,
const upb_MiniTableField* field) {
const upb_FieldType field_type = field->UPB_PRIVATE(descriptortype);
const size_t lg2 = upb_SizeLog2_FieldType(field_type);
const size_t lg2 = upb_FieldType_SizeLg2(field_type);
upb_Array* ret = _upb_Array_New(&d->arena, 4, lg2);
if (!ret) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
return ret;
Expand Down

0 comments on commit a49f6b8

Please sign in to comment.