Introduce upb_ByteSize

Naïve computation of a serialized upb message

PiperOrigin-RevId: 670990379
pull/18006/head
Hong Shin 5 months ago committed by Copybara-Service
parent b01eee755e
commit 290f28871f
  1. 1
      pkg/BUILD.bazel
  2. 29
      upb/wire/BUILD
  3. 37
      upb/wire/byte_size.c
  4. 31
      upb/wire/byte_size.h
  5. 40
      upb/wire/byte_size_test.cc

@ -218,6 +218,7 @@ cc_dist_library(
"//upb/text",
"//upb/util:def_to_proto",
"//upb/util:required_fields",
"//upb/wire:byte_size",
],
)

@ -61,6 +61,35 @@ cc_library(
],
)
cc_library(
name = "byte_size",
srcs = ["byte_size.c"],
hdrs = ["byte_size.h"],
visibility = ["//visibility:public"],
deps = [
"//upb:mem",
"//upb:message",
"//upb:mini_table",
"//upb:port",
"//upb:wire",
],
)
cc_test(
name = "byte_size_test",
srcs = ["byte_size_test.cc"],
deps = [
":byte_size",
"//upb:base",
"//upb:mem",
"//upb:mini_table",
"//upb/test:test_messages_proto2_upb_minitable",
"//upb/test:test_messages_proto2_upb_proto",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "eps_copy_input_stream",
srcs = ["eps_copy_input_stream.c"],

@ -0,0 +1,37 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/wire/byte_size.h"
#include <stddef.h>
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/mini_table/message.h"
#include "upb/wire/encode.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
size_t upb_ByteSize(const upb_Message* msg, const upb_MiniTable* mt) {
upb_Arena* arena = upb_Arena_New();
char* buf;
size_t res = 0;
upb_Encode(msg, mt, 0, arena, &buf, &res);
upb_Arena_Free(arena);
return res;
}
#ifdef __cplusplus
} // extern "C"
#endif

@ -0,0 +1,31 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_WIRE_BYTE_SIZE_H_
#define UPB_WIRE_BYTE_SIZE_H_
#include <stddef.h>
#include "upb/message/message.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
UPB_API size_t upb_ByteSize(const upb_Message* msg, const upb_MiniTable* mt);
#ifdef __cplusplus
} // extern "C"
#endif
#include "upb/port/undef.inc"
#endif // UPB_WIRE_BYTE_SIZE_H_

@ -0,0 +1,40 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include "upb/wire/byte_size.h"
#include <gtest/gtest.h>
#include "google/protobuf/test_messages_proto2.upb.h"
#include "google/protobuf/test_messages_proto2.upb_minitable.h"
#include "upb/base/upcast.h"
#include "upb/mem/arena.h"
#include "upb/mini_table/message.h"
namespace {
static const upb_MiniTable* kTestMiniTable =
&protobuf_0test_0messages__proto2__TestAllTypesProto2_msg_init;
TEST(ByteSizeTest, UnpopulatedMsg) {
upb_Arena* arena = upb_Arena_New();
protobuf_test_messages_proto2_TestAllTypesProto2* msg =
protobuf_test_messages_proto2_TestAllTypesProto2_new(arena);
auto res = upb_ByteSize(UPB_UPCAST(msg), kTestMiniTable);
EXPECT_EQ(res, 0);
upb_Arena_Free(arena);
}
TEST(ByteSizeTest, PopulatedMsg) {
upb_Arena* arena = upb_Arena_New();
protobuf_test_messages_proto2_TestAllTypesProto2* msg =
protobuf_test_messages_proto2_TestAllTypesProto2_new(arena);
protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_int32(msg, 322);
auto res = upb_ByteSize(UPB_UPCAST(msg), kTestMiniTable);
EXPECT_EQ(res, 3);
upb_Arena_Free(arena);
}
} // namespace
Loading…
Cancel
Save