Skip to content

Commit

Permalink
rename upb::SymbolTable as upb::DefPool
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 456147709
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jun 21, 2022
1 parent 88047d9 commit 97993b2
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 97 deletions.
8 changes: 4 additions & 4 deletions benchmarks/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ template <LoadDescriptorMode Mode>
static void BM_LoadAdsDescriptor_Upb(benchmark::State& state) {
size_t bytes_per_iter = 0;
for (auto _ : state) {
upb::SymbolTable symtab;
upb::DefPool defpool;
if (Mode == NoLayout) {
google_ads_googleads_v11_services_SearchGoogleAdsRequest_getmsgdef(
symtab.ptr());
bytes_per_iter = _upb_DefPool_BytesLoaded(symtab.ptr());
defpool.ptr());
bytes_per_iter = _upb_DefPool_BytesLoaded(defpool.ptr());
} else {
bytes_per_iter = 0;
LoadDefInit_BuildLayout(
symtab.ptr(),
defpool.ptr(),
&google_ads_googleads_v11_services_google_ads_service_proto_upbdefinit,
&bytes_per_iter);
}
Expand Down
59 changes: 30 additions & 29 deletions upb/bindings/lua/def.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#define LUPB_FILEDEF "lupb.filedef"
#define LUPB_MSGDEF "lupb.msgdef"
#define LUPB_ONEOFDEF "lupb.oneof"
#define LUPB_SYMTAB "lupb.symtab"
#define LUPB_SYMTAB "lupb.defpool"
#define LUPB_OBJCACHE "lupb.objcache"

static void lupb_DefPool_pushwrapper(lua_State* L, int narg, const void* def,
Expand All @@ -51,7 +51,7 @@ static void lupb_DefPool_pushwrapper(lua_State* L, int narg, const void* def,
/* lupb_wrapper ***************************************************************/

/* Wrappers around upb def objects. The userval contains a reference to the
* symtab. */
* defpool. */

#define LUPB_SYMTAB_INDEX 1

Expand All @@ -65,19 +65,19 @@ static const void* lupb_wrapper_check(lua_State* L, int narg,
return w->def;
}

static void lupb_wrapper_pushsymtab(lua_State* L, int narg) {
static void lupb_wrapper_pushdefpool(lua_State* L, int narg) {
lua_getiuservalue(L, narg, LUPB_SYMTAB_INDEX);
}

/* lupb_wrapper_pushwrapper()
*
* For a given def wrapper at index |narg|, pushes a wrapper for the given |def|
* and the given |type|. The new wrapper will be part of the same symtab. */
* and the given |type|. The new wrapper will be part of the same defpool. */
static void lupb_wrapper_pushwrapper(lua_State* L, int narg, const void* def,
const char* type) {
lupb_wrapper_pushsymtab(L, narg);
lupb_wrapper_pushdefpool(L, narg);
lupb_DefPool_pushwrapper(L, -1, def, type);
lua_replace(L, -2); /* Remove symtab from stack. */
lua_replace(L, -2); /* Remove defpool from stack. */
}

/* lupb_MessageDef_pushsubmsgdef()
Expand Down Expand Up @@ -337,8 +337,8 @@ static int lupb_MessageDef_OneofCount(lua_State* L) {

static bool lupb_MessageDef_pushnested(lua_State* L, int msgdef, int name) {
const upb_MessageDef* m = lupb_MessageDef_check(L, msgdef);
lupb_wrapper_pushsymtab(L, msgdef);
upb_DefPool* symtab = lupb_DefPool_check(L, -1);
lupb_wrapper_pushdefpool(L, msgdef);
upb_DefPool* defpool = lupb_DefPool_check(L, -1);
lua_pop(L, 1);

/* Construct full package.Message.SubMessage name. */
Expand All @@ -350,7 +350,7 @@ static bool lupb_MessageDef_pushnested(lua_State* L, int msgdef, int name) {

/* Try lookup. */
const upb_MessageDef* nested =
upb_DefPool_FindMessageByName(symtab, nested_name);
upb_DefPool_FindMessageByName(defpool, nested_name);
if (!nested) return false;
lupb_wrapper_pushwrapper(L, msgdef, nested, LUPB_MSGDEF);
return true;
Expand Down Expand Up @@ -671,8 +671,8 @@ static int lupb_FileDef_Package(lua_State* L) {

static int lupb_FileDef_Pool(lua_State* L) {
const upb_FileDef* f = lupb_FileDef_check(L, 1);
const upb_DefPool* symtab = upb_FileDef_Pool(f);
lupb_wrapper_pushwrapper(L, 1, symtab, LUPB_SYMTAB);
const upb_DefPool* defpool = upb_FileDef_Pool(f);
lupb_wrapper_pushwrapper(L, 1, defpool, LUPB_SYMTAB);
return 1;
}

Expand All @@ -691,30 +691,30 @@ static const struct luaL_Reg lupb_FileDef_m[] = {
{"msgcount", lupb_FileDef_msgcount},
{"name", lupb_FileDef_Name},
{"package", lupb_FileDef_Package},
{"symtab", lupb_FileDef_Pool},
{"defpool", lupb_FileDef_Pool},
{"syntax", lupb_FileDef_Syntax},
{NULL, NULL}};

/* lupb_DefPool
* ****************************************************************/

/* The symtab owns all defs. Thus GC-rooting the symtab ensures that all
/* The defpool owns all defs. Thus GC-rooting the defpool ensures that all
* underlying defs stay alive.
*
* The symtab's userval is a cache of def* -> object. */
* The defpool's userval is a cache of def* -> object. */

#define LUPB_CACHE_INDEX 1

typedef struct {
upb_DefPool* symtab;
upb_DefPool* defpool;
} lupb_DefPool;

upb_DefPool* lupb_DefPool_check(lua_State* L, int narg) {
lupb_DefPool* lsymtab = luaL_checkudata(L, narg, LUPB_SYMTAB);
if (!lsymtab->symtab) {
lupb_DefPool* ldefpool = luaL_checkudata(L, narg, LUPB_SYMTAB);
if (!ldefpool->defpool) {
luaL_error(L, "called into dead object");
}
return lsymtab->symtab;
return ldefpool->defpool;
}

void lupb_DefPool_pushwrapper(lua_State* L, int narg, const void* def,
Expand All @@ -739,7 +739,7 @@ void lupb_DefPool_pushwrapper(lua_State* L, int narg, const void* def,
w->def = def;
lua_replace(L, -2); /* Replace nil */

/* Set symtab as userval. */
/* Set defpool as userval. */
lua_pushvalue(L, narg);
lua_setiuservalue(L, -2, LUPB_SYMTAB_INDEX);

Expand All @@ -754,11 +754,12 @@ void lupb_DefPool_pushwrapper(lua_State* L, int narg, const void* def,
/* upb_DefPool_New()
*
* Handles:
* upb.SymbolTable() -> <new instance>
* upb.DefPool() -> <new instance>
*/
static int lupb_DefPool_New(lua_State* L) {
lupb_DefPool* lsymtab = lupb_newuserdata(L, sizeof(*lsymtab), 1, LUPB_SYMTAB);
lsymtab->symtab = upb_DefPool_New();
lupb_DefPool* ldefpool =
lupb_newuserdata(L, sizeof(*ldefpool), 1, LUPB_SYMTAB);
ldefpool->defpool = upb_DefPool_New();

/* Create our object cache. */
lua_newtable(L);
Expand All @@ -769,9 +770,9 @@ static int lupb_DefPool_New(lua_State* L) {
lua_setfield(L, -2, "__mode");
lua_setmetatable(L, -2);

/* Put the symtab itself in the cache metatable. */
/* Put the defpool itself in the cache metatable. */
lua_pushvalue(L, -2);
lua_rawsetp(L, -2, lsymtab->symtab);
lua_rawsetp(L, -2, ldefpool->defpool);

/* Set the cache as our userval. */
lua_setiuservalue(L, -2, LUPB_CACHE_INDEX);
Expand All @@ -780,9 +781,9 @@ static int lupb_DefPool_New(lua_State* L) {
}

static int lupb_DefPool_gc(lua_State* L) {
lupb_DefPool* lsymtab = luaL_checkudata(L, 1, LUPB_SYMTAB);
upb_DefPool_Free(lsymtab->symtab);
lsymtab->symtab = NULL;
lupb_DefPool* ldefpool = luaL_checkudata(L, 1, LUPB_SYMTAB);
upb_DefPool_Free(ldefpool->defpool);
ldefpool->defpool = NULL;
return 0;
}

Expand Down Expand Up @@ -859,7 +860,7 @@ static int lupb_DefPool_FindEnumByNameval(lua_State* L) {
}

static int lupb_DefPool_tostring(lua_State* L) {
lua_pushfstring(L, "<upb.SymbolTable>");
lua_pushfstring(L, "<upb.DefPool>");
return 1;
}

Expand All @@ -884,7 +885,7 @@ static void lupb_setfieldi(lua_State* L, const char* field, int i) {
}

static const struct luaL_Reg lupbdef_toplevel_m[] = {
{"SymbolTable", lupb_DefPool_New}, {NULL, NULL}};
{"DefPool", lupb_DefPool_New}, {NULL, NULL}};

void lupb_def_registertypes(lua_State* L) {
lupb_setfuncs(L, lupbdef_toplevel_m);
Expand Down
30 changes: 15 additions & 15 deletions upb/bindings/lua/test_upb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ function test_finalizer()
end)
end)
t = {
upb.SymbolTable(),
upb.DefPool(),
}
end
collectgarbage()
Expand Down Expand Up @@ -666,14 +666,14 @@ function test_unknown()
end

function test_foo()
local symtab = upb.SymbolTable()
local defpool = upb.DefPool()
local filename = "external/com_google_protobuf/descriptor_proto-descriptor-set.proto.bin"
local file = io.open(filename, "rb") or io.open("bazel-bin/" .. filename, "rb")
assert_not_nil(file)
local descriptor = file:read("*a")
assert_true(#descriptor > 0)
symtab:add_set(descriptor)
local FileDescriptorSet = symtab:lookup_msg("google.protobuf.FileDescriptorSet")
defpool:add_set(descriptor)
local FileDescriptorSet = defpool:lookup_msg("google.protobuf.FileDescriptorSet")
assert_not_nil(FileDescriptorSet)
set = FileDescriptorSet()
assert_equal(#set.file, 0)
Expand All @@ -690,7 +690,7 @@ function test_foo()
end

function test_descriptor()
local symtab = upb.SymbolTable()
local defpool = upb.DefPool()
local file_proto = descriptor.FileDescriptorProto {
name = "test.proto",
message_type = upb.Array(descriptor.DescriptorProto, {
Expand All @@ -699,12 +699,12 @@ function test_descriptor()
},
})
}
local file = symtab:add_file(upb.encode(file_proto))
assert_equal(file:symtab(), symtab)
local file = defpool:add_file(upb.encode(file_proto))
assert_equal(file:defpool(), defpool)
end

function test_descriptor_error()
local symtab = upb.SymbolTable()
local defpool = upb.DefPool()
local file = descriptor.FileDescriptorProto()
file.name = "test.proto"
file.message_type[1] = descriptor.DescriptorProto{
Expand All @@ -713,12 +713,12 @@ function test_descriptor_error()
file.message_type[2] = descriptor.DescriptorProto{
name = "BC."
}
assert_error(function () symtab:add_file(upb.encode(file)) end)
assert_nil(symtab:lookup_msg("ABC"))
assert_error(function () defpool:add_file(upb.encode(file)) end)
assert_nil(defpool:lookup_msg("ABC"))
end

function test_duplicate_enumval()
local symtab = upb.SymbolTable()
local defpool = upb.DefPool()
local file_proto = descriptor.FileDescriptorProto {
name = "test.proto",
message_type = upb.Array(descriptor.DescriptorProto, {
Expand All @@ -738,16 +738,16 @@ function test_duplicate_enumval()
},
})
}
assert_error(function () symtab:add_file(upb.encode(file_proto)) end)
assert_error(function () defpool:add_file(upb.encode(file_proto)) end)
end

function test_duplicate_filename_error()
local symtab = upb.SymbolTable()
local defpool = upb.DefPool()
local file = descriptor.FileDescriptorProto()
file.name = "test.proto"
symtab:add_file(upb.encode(file))
defpool:add_file(upb.encode(file))
-- Second add with the same filename fails.
assert_error(function () symtab:add_file(upb.encode(file)) end)
assert_error(function () defpool:add_file(upb.encode(file)) end)
end

function test_encode_skipunknown()
Expand Down
2 changes: 1 addition & 1 deletion upb/bindings/lua/upb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

local upb = require("lupb")

upb.generated_pool = upb.SymbolTable()
upb.generated_pool = upb.DefPool()

local module_metatable = {
__index = function(t, k)
Expand Down
11 changes: 7 additions & 4 deletions upb/def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ class FileDefPtr {
const upb_FileDef* ptr_;
};

// Non-const methods in upb::SymbolTable are NOT thread-safe.
class SymbolTable {
// Non-const methods in upb::DefPool are NOT thread-safe.
class DefPool {
public:
SymbolTable() : ptr_(upb_DefPool_New(), upb_DefPool_Free) {}
explicit SymbolTable(upb_DefPool* s) : ptr_(s, upb_DefPool_Free) {}
DefPool() : ptr_(upb_DefPool_New(), upb_DefPool_Free) {}
explicit DefPool(upb_DefPool* s) : ptr_(s, upb_DefPool_Free) {}

const upb_DefPool* ptr() const { return ptr_.get(); }
upb_DefPool* ptr() { return ptr_.get(); }
Expand Down Expand Up @@ -409,6 +409,9 @@ class SymbolTable {
std::unique_ptr<upb_DefPool, decltype(&upb_DefPool_Free)> ptr_;
};

// TODO(b/236632406): This typedef is deprecated. Delete it.
using SymbolTable = DefPool;

inline FileDefPtr MessageDefPtr::file() const {
return FileDefPtr(upb_MessageDef_File(ptr_));
}
Expand Down
8 changes: 4 additions & 4 deletions upb/json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
static std::string JsonEncode(const upb_test_Box* msg, int options) {
upb::Arena a;
upb::Status status;
upb::SymbolTable symtab;
upb::MessageDefPtr m(upb_test_Box_getmsgdef(symtab.ptr()));
upb::DefPool defpool;
upb::MessageDefPtr m(upb_test_Box_getmsgdef(defpool.ptr()));
EXPECT_TRUE(m.ptr() != nullptr);

size_t json_size = upb_JsonEncode(msg, m.ptr(), symtab.ptr(), options, NULL,
size_t json_size = upb_JsonEncode(msg, m.ptr(), defpool.ptr(), options, NULL,
0, status.ptr());
char* json_buf = (char*)upb_Arena_Malloc(a.ptr(), json_size + 1);

size_t size = upb_JsonEncode(msg, m.ptr(), symtab.ptr(), options, json_buf,
size_t size = upb_JsonEncode(msg, m.ptr(), defpool.ptr(), options, json_buf,
json_size + 1, status.ptr());
EXPECT_EQ(size, json_size);
return std::string(json_buf, json_size);
Expand Down
Loading

0 comments on commit 97993b2

Please sign in to comment.