Protobuf, understand the hood
proto文件定义
package lm;
message Foo{
required int32 id = 1;
}
message Bar{
required int32 id = 1 [default = 12];
required string str= 2 [default = "bar::str"];
required Foo foo= 3;
}
下面分析protoc编译生成的:ph.b和pb.cc文件。
初始化
protoc会定义一个全局对象:
struct StaticDescriptorInitializer_lm_2ehelloworld_2eproto {
StaticDescriptorInitializer_lm_2ehelloworld_2eproto() {
protobuf_AddDesc_lm_2ehelloworld_2eproto();
};
} static_descriptor_initializer_lm_2ehelloworld_2eproto_;
其中protobuf_AddDesc_lm_2ehelloworld_2eproto()函数完成了默认值的初始化:
void protobuf_AddDesc_lm_2ehelloworld_2eproto() {
static bool already_here = false;
...
Foo::default_instance_ = new Foo();
Bar::_default_str_ =
new ::std::string("bar::str", 8); // string默认值存在这里
Bar::default_instance_ = new Bar();
Foo::default_instance_->InitAsDefaultInstance();
Bar::default_instance_->InitAsDefaultInstance();
::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_lm_2ehelloworld_2eproto);
}
同时注册了清理函数protobuf_ShutdownFile_lm_2ehelloworld_2eproto:
void protobuf_ShutdownFile_lm_2ehelloworld_2eproto() {
delete Foo::default_instance_;
delete Foo_reflection_;
delete Bar::default_instance_;
delete Bar_reflection_;
delete Bar::_default_str_;
}
构造
默认值的赋值在构造函数里:
Bar::Bar()
: ::google::protobuf::Message() {
SharedCtor();
// @@protoc_insertion_point(constructor:lm.Bar)
}
void Bar::SharedCtor() {
::google::protobuf::internal::GetEmptyString();
_cached_size_ = 0;
id_ = 12; // id默认值
str_ = const_cast< ::std::string*>(_default_str_); // str默认值
foo_ = NULL;
::memset(_has_bits_, 0, sizeof(_has_bits_));
}
整数标示符的名称为kXxxFieldNumber:
class Bar : public ::google::protobuf::Message {
public:
static const int kIdFieldNumber = 1;
static const int kStrFieldNumber = 2;
static const int kFooFieldNumber = 3;
};
析构
新建的对象在析构函数里删除:
Bar::~Bar() {
// @@protoc_insertion_point(destructor:lm.Bar)
SharedDtor();
}
void Bar::SharedDtor() {
if (str_ != _default_str_) {
delete str_;
}
if (this != default_instance_) {
delete foo_;
}
};
Mutable Pointer
使用mutable_xxx()时,如果没有set过,则new一个新对象出来返回:
inline ::std::string* Bar::mutable_str() {
set_has_str();
if (str_ == _default_str_) {
str_ = new ::std::string(*_default_str_);
}
return str_;
}
inline ::lm::Foo* Bar::mutable_foo() {
set_has_foo();
if (foo_ == NULL) foo_ = new ::lm::Foo;
return foo_;
}
Allocate Value
使用set_allocated_xxx()时,清理掉原来的对象,用新对象赋值:
inline void Bar::set_allocated_str(::std::string* str) {
if (str_ != _default_str_) {
delete str_;
}
if (str) {
set_has_str();
str_ = str;
} else {
clear_has_str();
str_ = const_cast< ::std::string*>(_default_str_);
}
}
inline void Bar::set_allocated_foo(::lm::Foo* foo) {
delete foo_;
foo_ = foo;
if (foo) {
set_has_foo();
} else {
clear_has_foo();
}
}
Clear
Clear()时重设为默认值:
void Bar::Clear() {
if (_has_bits_[0 / 32] & 7) {
id_ = 12;
if (has_str()) {
if (str_ != _default_str_) {
str_->assign(*_default_str_);
}
}
if (has_foo()) {
if (foo_ != NULL) foo_->::lm::Foo::Clear();
}
}
::memset(_has_bits_, 0, sizeof(_has_bits_));
mutable_unknown_fields()->Clear();
}
从这里面看出,只是重新赋值,如果是allocated的值,不会清理,所以需要allocate的人自己管理内存。
Protobuf, understand the hood的更多相关文章
- UNDER THE HOOD OF THE NEW AZURE PORTAL
http://jbeckwith.com/2014/09/20/how-the-azure-portal-works/ So - I haven’t been doing much blogging ...
- [under the hood]Reduce EXE and DLL Size with LIBCTINY.LIB
Matt Pietrek Download the code for this article: Hood0101.exe (45KB) W ay back in my October 1996 co ...
- C++ Under the Hood
The original article is taken from http://msdn.microsoft.com/archive/en-us/dnarvc/html/jangrayhood.a ...
- under the hood
under the hood adjective a metaphorical area that contains the underlying implementation of somethin ...
- C++操作Kafka使用Protobuf进行跨语言数据交互
C++操作Kafka使用Protobuf进行跨语言数据交互 Kafka 是一种分布式的,基于发布 / 订阅的消息系统.主要设计目标如下: 以时间复杂度为 O(1) 的方式提供消息持久化能力,即使对 T ...
- python通过protobuf实现rpc
由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc.rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的童鞋可以自行g ...
- Protobuf使用规范分享
一.Protobuf 的优点 Protobuf 有如 XML,不过它更小.更快.也更简单.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍.你可以定义自己的数据结构 ...
- java netty socket库和自定义C#socket库利用protobuf进行通信完整实例
之前的文章讲述了socket通信的一些基本知识,已经本人自定义的C#版本的socket.和java netty 库的二次封装,但是没有真正的发表测试用例. 本文只是为了讲解利用protobuf 进行C ...
- 在Wcf中应用ProtoBuf替代默认的序列化器
Google的ProtoBuf序列化器性能的牛逼已经有目共睹了,可以把它应用到Socket通讯,队列,Wcf中,身为dotnet程序员一边期待着不久后Grpc对dotnet core的支持更期待着Wc ...
随机推荐
- MySQL 5.6 root密码丢失,使用mysqld --skip-grant-tables
MySQL 5.6 root密码丢失,(window平台)使用mysqld –skip-grant-tables启动MySQL服务,出现警告: 1 [Warning] TIMESTAMP with i ...
- jquery多级联动(ajax查数据库)
/id 代表下级下拉框ID,cityCode代表的是父级菜单代码,所有级菜单在同一张表,后台在加载是把菜单已经加入到Map缓存中.... //id 代表下级下拉框ID,cityCode代表的是父级菜单 ...
- Hadoop学习历程(一、编译)
近期对Hadoop很感兴趣,下面是在CentOS系统上进行编译的分享 系统情况如下: 1. CentOS 6.2 64位 2. hadoop-2.2.0 3. jdk 1.6.0_31 ...
- 文成小盆友python-num9 socket编程
socket编程 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意思 ...
- MVC 中引入Jquery文件的几种方法
方法1: <script src="@Url.Content("~/Scripts/jquery-1.9.1.js")" type="text/ ...
- 后台管理UI
后台管理UI 目录 一.EasyUI 二.DWZ JUI 三.HUI 四.BUI 五.Ace Admin 六.Metronic 七.H+ UI 八.Admin LTE 九.INSPINIA 十.Lig ...
- Java学习笔记--Collection和Collections的区别
转自 http://pengcqu.iteye.com/blog/492196 比较Collection 和Collections的区别. 1.java.util.Collection 是一个集合 ...
- 模拟Hibernate动态生成SQL语句
这里有一个xml配置文件,也就是Hibernate框架中会用到的POJO和数据库的映射文件 <?xml version="1.0" encoding="utf-8& ...
- NGINX当中的SSL配置(PEM启动密码免输入)
干过的都懂,这个东东好像很碍事. 很影响RELOAD的效率...并且,,,好像这个弄了之后,NGINX作RELOAD之后,可以即时更新配置了..(还是说那个DVROOT.CER影响的??未考证) ht ...
- VS IDE环境下,windows GUI(Qt MFC,win32)使用控制台实时打印调试信息
在工程属性的页面下,点击Build Events,在Build Events下点击Post-Build Event. 然后再Command Line里面输入以下命令: editbin /SUBSYST ...