Skip to content

Commit

Permalink
upb: inline upb_Message_New()
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 588189563
  • Loading branch information
ericsalo authored and copybara-github committed Dec 5, 2023
1 parent 7d4ead2 commit 3c75c33
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 148 deletions.
2 changes: 1 addition & 1 deletion upb/message/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage(
const upb_MiniTable* sub_mini_table = upb_MiniTableSub_Message(
mini_table->UPB_PRIVATE(subs)[field->UPB_PRIVATE(submsg_index)]);
UPB_ASSERT(sub_mini_table);
sub_message = _upb_Message_New(sub_mini_table, arena);
sub_message = upb_Message_New(sub_mini_table, arena);
*UPB_PTR_AT(msg, field->offset, upb_Message*) = sub_message;
UPB_PRIVATE(_upb_Message_SetPresence)(msg, field);
}
Expand Down
4 changes: 1 addition & 3 deletions upb/message/copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
#include "upb/message/internal/array.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/message.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"
#include "upb/mini_table/sub.h"
Expand Down Expand Up @@ -288,7 +286,7 @@ upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src,
if (unknown_size != 0) {
UPB_ASSERT(ptr);
// Make a copy into destination arena.
if (!_upb_Message_AddUnknown(dst, ptr, unknown_size, arena)) {
if (!upb_Message_AddUnknown(dst, ptr, unknown_size, arena)) {
return NULL;
}
}
Expand Down
3 changes: 1 addition & 2 deletions upb/message/copy_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/accessors.h"
#include "upb/message/internal/message.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/mini_table/field.h"
Expand Down Expand Up @@ -301,7 +300,7 @@ TEST(GeneratedCode, DeepCloneMessageWithUnknowns) {
ASSERT_EQ(status, kUpb_EncodeStatus_Ok);
std::string unknown_data(data, len);
// Add unknown data.
_upb_Message_AddUnknown(msg, data, len, source_arena);
upb_Message_AddUnknown(msg, data, len, source_arena);
// Create clone.
upb_Arena* clone_arena = upb_Arena_New();
protobuf_test_messages_proto2_TestAllTypesProto2* clone =
Expand Down
4 changes: 3 additions & 1 deletion upb/message/internal/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#ifndef UPB_MESSAGE_INTERNAL_EXTENSION_H_
#define UPB_MESSAGE_INTERNAL_EXTENSION_H_

#include <stddef.h>

#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/message/types.h"
#include "upb/mini_table/extension.h"

// Must be last.
Expand Down
10 changes: 2 additions & 8 deletions upb/message/internal/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ UPB_INLINE size_t upb_msg_sizeof(const upb_MiniTable* m) {
return m->UPB_PRIVATE(size) + sizeof(upb_Message_Internal);
}

// Inline version upb_Message_New(), for internal use.
UPB_INLINE upb_Message* _upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena) {
UPB_INLINE upb_Message* UPB_PRIVATE(_upb_Message_New)(
const upb_MiniTable* mini_table, upb_Arena* arena) {
size_t size = upb_msg_sizeof(mini_table);
void* mem = upb_Arena_Malloc(arena, size + sizeof(upb_Message_Internal));
if (UPB_UNLIKELY(!mem)) return NULL;
Expand All @@ -87,11 +86,6 @@ UPB_INLINE upb_Message_Internal* upb_Message_Getinternal(
// Discards the unknown fields for this message only.
void _upb_Message_DiscardUnknown_shallow(upb_Message* msg);

// Adds unknown data (serialized protobuf data) to the given message.
// The data is copied into the message instance.
bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena);

bool UPB_PRIVATE(_upb_Message_Realloc)(upb_Message* msg, size_t need,
upb_Arena* arena);

Expand Down
9 changes: 2 additions & 7 deletions upb/message/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@

static const size_t message_overhead = sizeof(upb_Message_InternalData);

upb_Message* upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena) {
return _upb_Message_New(mini_table, arena);
}

bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena) {
bool upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena) {
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, len, arena)) return false;
upb_Message_Internal* in = upb_Message_Getinternal(msg);
memcpy(UPB_PTR_AT(in->internal, in->internal->unknown_end, char), data, len);
Expand Down
12 changes: 10 additions & 2 deletions upb/message/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stddef.h>

#include "upb/mem/arena.h"
#include "upb/message/internal/message.h"
#include "upb/message/types.h" // IWYU pragma: export
#include "upb/mini_table/message.h"

Expand All @@ -26,8 +27,15 @@ extern "C" {
#endif

// Creates a new message with the given mini_table on the given arena.
UPB_API upb_Message* upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena);
UPB_API_INLINE upb_Message* upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena) {
return UPB_PRIVATE(_upb_Message_New)(mini_table, arena);
}

// Adds unknown data (serialized protobuf data) to the given message.
// The data is copied into the message instance.
bool upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena);

// Returns a reference to the message's unknown data.
const char* upb_Message_GetUnknown(const upb_Message* msg, size_t* len);
Expand Down
3 changes: 1 addition & 2 deletions upb/message/promote.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "upb/message/internal/accessors.h"
#include "upb/message/internal/array.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/message.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/message/tagged_ptr.h"
Expand All @@ -42,7 +41,7 @@ static upb_UnknownToMessageRet upb_MiniTable_ParseUnknownMessage(
int decode_options, upb_Arena* arena) {
upb_UnknownToMessageRet ret;
ret.message =
base_message ? base_message : _upb_Message_New(mini_table, arena);
base_message ? base_message : upb_Message_New(mini_table, arena);
if (!ret.message) {
ret.status = kUpb_UnknownToMessage_OutOfMemory;
return ret;
Expand Down
23 changes: 11 additions & 12 deletions upb/message/promote_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "upb/message/array.h"
#include "upb/message/copy.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/message.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/message/tagged_ptr.h"
Expand Down Expand Up @@ -325,15 +324,15 @@ TEST(GeneratedCode, PromoteUnknownMessage) {
// If we parse without allowing unlinked objects, the parse will fail.
// TODO: re-enable this test once the old method of tree shaking is
// removed
// upb_Message* fail_msg = _upb_Message_New(mini_table, arena.ptr());
// upb_Message* fail_msg = upb_Message_New(mini_table, arena.ptr());
// decode_status =
// upb_Decode(serialized, serialized_size, fail_msg, mini_table, nullptr,
// 0,
// arena.ptr());
// EXPECT_EQ(decode_status, kUpb_DecodeStatus_UnlinkedSubMessage);

// if we parse while allowing unlinked objects, the parse will succeed.
upb_Message* msg = _upb_Message_New(mini_table, arena.ptr());
upb_Message* msg = upb_Message_New(mini_table, arena.ptr());
decode_status =
upb_Decode(serialized, serialized_size, msg, mini_table, nullptr,
kUpb_DecodeOption_ExperimentalAllowUnlinked, arena.ptr());
Expand Down Expand Up @@ -399,7 +398,7 @@ TEST(GeneratedCode, ReparseUnlinked) {
upb_MiniTable* mini_table = CreateMiniTableWithEmptySubTables(arena.ptr());

// Parse twice without linking the MiniTable.
upb_Message* msg = _upb_Message_New(mini_table, arena.ptr());
upb_Message* msg = upb_Message_New(mini_table, arena.ptr());
upb_DecodeStatus decode_status =
upb_Decode(serialized, serialized_size, msg, mini_table, nullptr,
kUpb_DecodeOption_ExperimentalAllowUnlinked, arena.ptr());
Expand Down Expand Up @@ -454,7 +453,7 @@ TEST(GeneratedCode, PromoteInParser) {
upb_MiniTable* mini_table = CreateMiniTableWithEmptySubTables(arena.ptr());

// Parse once without linking the MiniTable.
upb_Message* msg = _upb_Message_New(mini_table, arena.ptr());
upb_Message* msg = upb_Message_New(mini_table, arena.ptr());
upb_DecodeStatus decode_status =
upb_Decode(serialized, serialized_size, msg, mini_table, nullptr,
kUpb_DecodeOption_ExperimentalAllowUnlinked, arena.ptr());
Expand Down Expand Up @@ -512,15 +511,15 @@ TEST(GeneratedCode, PromoteUnknownRepeatedMessage) {
// If we parse without allowing unlinked objects, the parse will fail.
// TODO: re-enable this test once the old method of tree shaking is
// removed
// upb_Message* fail_msg = _upb_Message_New(mini_table, arena.ptr());
// upb_Message* fail_msg = upb_Message_New(mini_table, arena.ptr());
// decode_status =
// upb_Decode(serialized, serialized_size, fail_msg, mini_table, nullptr,
// 0,
// arena.ptr());
// EXPECT_EQ(decode_status, kUpb_DecodeStatus_UnlinkedSubMessage);

// if we parse while allowing unlinked objects, the parse will succeed.
upb_Message* msg = _upb_Message_New(mini_table, arena.ptr());
upb_Message* msg = upb_Message_New(mini_table, arena.ptr());
decode_status =
upb_Decode(serialized, serialized_size, msg, mini_table, nullptr,
kUpb_DecodeOption_ExperimentalAllowUnlinked, arena.ptr());
Expand Down Expand Up @@ -586,14 +585,14 @@ TEST(GeneratedCode, PromoteUnknownToMap) {
CreateMiniTableWithEmptySubTablesForMaps(arena.ptr());

// If we parse without allowing unlinked objects, the parse will fail.
upb_Message* fail_msg1 = _upb_Message_New(mini_table, arena.ptr());
upb_Message* fail_msg1 = upb_Message_New(mini_table, arena.ptr());
upb_DecodeStatus decode_status =
upb_Decode(serialized, serialized_size, fail_msg1, mini_table, nullptr, 0,
arena.ptr());
EXPECT_EQ(decode_status, kUpb_DecodeStatus_UnlinkedSubMessage);

// if we parse while allowing unlinked objects, the parse will succeed.
upb_Message* msg = _upb_Message_New(mini_table, arena.ptr());
upb_Message* msg = upb_Message_New(mini_table, arena.ptr());
decode_status =
upb_Decode(serialized, serialized_size, msg, mini_table, nullptr,
kUpb_DecodeOption_ExperimentalAllowUnlinked, arena.ptr());
Expand Down Expand Up @@ -700,7 +699,7 @@ TEST(GeneratedCode, PromoteUnknownMessageOld) {
&serialized_size);

upb_MiniTable* mini_table = CreateMiniTableWithEmptySubTablesOld(arena);
upb_Message* msg = _upb_Message_New(mini_table, arena);
upb_Message* msg = upb_Message_New(mini_table, arena);
upb_DecodeStatus decode_status = upb_Decode(serialized, serialized_size, msg,
mini_table, nullptr, 0, arena);
EXPECT_EQ(decode_status, kUpb_DecodeStatus_Ok);
Expand Down Expand Up @@ -747,7 +746,7 @@ TEST(GeneratedCode, PromoteUnknownRepeatedMessageOld) {
&serialized_size);

upb_MiniTable* mini_table = CreateMiniTableWithEmptySubTablesOld(arena);
upb_Message* msg = _upb_Message_New(mini_table, arena);
upb_Message* msg = upb_Message_New(mini_table, arena);
upb_DecodeStatus decode_status = upb_Decode(serialized, serialized_size, msg,
mini_table, nullptr, 0, arena);
EXPECT_EQ(decode_status, kUpb_DecodeStatus_Ok);
Expand Down Expand Up @@ -805,7 +804,7 @@ TEST(GeneratedCode, PromoteUnknownToMapOld) {
upb_MiniTable* mini_table =
CreateMiniTableWithEmptySubTablesForMapsOld(arena);
upb_MiniTable* map_entry_mini_table = CreateMapEntryMiniTableOld(arena);
upb_Message* msg = _upb_Message_New(mini_table, arena);
upb_Message* msg = upb_Message_New(mini_table, arena);
const int decode_options = upb_DecodeOptions_MaxDepth(0);
upb_DecodeStatus decode_status =
upb_Decode(serialized, serialized_size, msg, mini_table, nullptr,
Expand Down
Loading

0 comments on commit 3c75c33

Please sign in to comment.