protobuf string/bytes

protobuf提供了多种基础数据格式,包括string/bytes。从字面意义上,我们了解bytes适用于任意的二进制字节序列。然而对C++程序员来讲,std::string既能存储ASCII文本字符串,也能存储任意多个\0的二进制序列。那么区别在哪里呢?

同时在实际使用中,我们偶尔会看到类似这样的运行错误:

 
  1. [libprotobuf ERROR google/protobuf/wire_format.cc:1091] String field 'str' contains invalid UTF-8 data when serializing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.

  2. [libprotobuf ERROR google/protobuf/wire_format.cc:1091] String field 'str' contains invalid UTF-8 data when parsing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.

这篇文章从源码角度分析下string/bytes类型的区别。

之前的文章里介绍过protobuf序列化的过程,我们看下string/bytes序列化的过程。 在之前的文章里介绍过protobuf序列化的过程,我们看下string/bytes序列化的过程。

所有的序列化操作都会在SerializeFieldWithCachedSizes这个函数里进行。根据不同的类型调用对应的序列化函数,例如对于string类型

 
  1. case FieldDescriptor::TYPE_STRING: {

  2. string scratch;

  3. const string& value = field->is_repeated() ?

  4. message_reflection->GetRepeatedStringReference(

  5. message, field, j, &scratch) :

  6. message_reflection->GetStringReference(message, field, &scratch);

  7. VerifyUTF8StringNamedField(value.data(), value.length(), SERIALIZE,

  8. field->name().c_str());

  9. WireFormatLite::WriteString(field->number(), value, output);

  10. break;

  11. }

而对于bytes类型:

 
  1. case FieldDescriptor::TYPE_BYTES: {

  2. string scratch;

  3. const string& value = field->is_repeated() ?

  4. message_reflection->GetRepeatedStringReference(

  5. message, field, j, &scratch) :

  6. message_reflection->GetStringReference(message, field, &scratch);

  7. WireFormatLite::WriteBytes(field->number(), value, output);

  8. break;

  9. }

可以看到在序列化时主要有两点区别:

  1. string类型调用了VerifyUTF8StringNamedField函数
  2. 序列化函数不同:WriteString vs WriteBytes

关于第二点,两个函数都定义在wire_format_lite.cc,实现是相同的。

那么我们继续看下第一点,VerifyUTF8StringNamedField调用了VerifyUTF8StringFallback(话说一直不理解fallback在这里什么意思,protobuf源码里经常看到这个后缀)。看下这个函数的实现:

 
  1.  
  2. void WireFormat::VerifyUTF8StringFallback(const char* data,

  3. int size,

  4. Operation op,

  5. const char* field_name) {

  6. if (!IsStructurallyValidUTF8(data, size)) {

  7. const char* operation_str = NULL;

  8. switch (op) {

  9. case PARSE:

  10. operation_str = "parsing";

  11. break;

  12. case SERIALIZE:

  13. operation_str = "serializing";

  14. break;

  15. // no default case: have the compiler warn if a case is not covered.

  16. }

  17. string quoted_field_name = "";

  18. if (field_name != NULL) {

  19. quoted_field_name = StringPrintf(" '%s'", field_name);

  20. }

  21. // no space below to avoid double space when the field name is missing.

  22. GOOGLE_LOG(ERROR) << "String field" << quoted_field_name << " contains invalid "

  23. << "UTF-8 data when " << operation_str << " a protocol "

  24. << "buffer. Use the 'bytes' type if you intend to send raw "

  25. << "bytes. ";

  26. }

  27. }

运行错误是从这里输出的,关键还是在于IsStructurallyValidUTF8这个函数,实现在structurally_valid.cc里:

 
  1. bool IsStructurallyValidUTF8(const char* buf, int len) {

  2. if (!module_initialized_) return true;

  3.  
  4. int bytes_consumed = 0;

  5. UTF8GenericScanFastAscii(&utf8acceptnonsurrogates_obj,

  6. buf, len, &bytes_consumed);

  7. return (bytes_consumed == len);

  8. }

这里逐个字符扫描是否符合utf-8规范,比如110xxxxx 10xxxxxx这样,具体可以参考utf-8的编码标准。

反序列化过程类似。

看到这里我们可以得到这样的结论:

  1. protobuf里的string/bytes在C++接口里实现上都是std::string
  2. 两者序列化、反序列化格式上一致,不过对于string格式,会有一个utf-8格式的检查。

出于效率,我们应当在确定字段编码格式后直接使用bytes,减少utf8编码的判断,效率上会有提高。

注意以上代码在pb2.6下,2.4不会输出field_name

据了解java接口上有一定的区别,分别对应String以及ByteString

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值