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 简单的增删改查语句
增加记录: 注:null关键字与auto_increment限制条件相结合,可以为字段自动赋值:字段必须全,且一一对应:字符型用单引号: mysql> insert into test valu ...
- 第8章BOM笔记
第八章 BOM 一. Window 在浏览器中window有双重角色,他既是JavaScript访问浏览器窗口的一个借口,又是ECMAscript 规定的Global对象. 1.全局作用域 由于win ...
- TransactionScope的正确用法(转自:http://blog.csdn.net/greystar/article/details/1359960)
前一阵贴了一个关于transactionscope的贴子,以为是MS的BUG.后来经过认真仔细的调试,才找到原因. 原来的代码本身是没有没问题的.只是关于事务资源的处理放错了地方.正确的写法应该如下 ...
- 15--Box2D使用(一、创建物理世界)
创建工程Box2DTest,去掉背景和精灵对象等.首先在HelloWorldScene.h头文件定义一个屏幕像素与物理世界长度转换宏,并引入box2D头文件 #define PIXEL_TO_METE ...
- 关于curl_setopt参数的记录
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE) 其中true输出执行结果,false为不输出 fsockopen与curl_setopt请求的区别之一就是 ...
- xml文件操作
public static XmlDocument getDoc(String path)//加载xml文档 { XmlDocument doc = new XmlDocument(); doc.Lo ...
- iOS学习之网易新闻简易Demo
简易NewsDemo代码分析 界面布局就不多说了.效果图:(自定义了三套Cell,最后一套Cell是页面最下方的"正在加载中..."Cell,图三.) 主要分析工程 ...
- Kafka笔记--监控系统KafkaOffsetMonitor
KafkaOffsetMonitor下载链接: http://download.csdn.net/detail/changong28/7930337github官方:https://github.co ...
- 安装Visual Studio 2010之后怎样安装MSDN Library
这篇博客参考自:http://justargon.blog.163.com/blog/static/21394413020134100737688/ MSDN2010安装及使用(MSDN Librar ...
- 64位调试器花费的时间比预期的要长(A 64-bit debugging operation is taking longer than expected)
在stackoverflow上找到解决方案的: http://stackoverflow.com/questions/21329899/vs2013-professional-local-64-bit ...