首页 > 其他分享 >Protobuf - FIELD NUMBERS

Protobuf - FIELD NUMBERS

时间:2023-11-14 09:12:05浏览次数:32  
标签:Protobuf field fields value FIELD numbers metadata data NUMBERS

Required fields in a message can be thought of as frequently used fields since you cannot skip them as you can for optional fields. It is a best practice to reserve some numbers between 1 and 15 for the fields that can be frequently used since the numbers take 1 byte to encode in that range. For example, if you introduce a field with the name correlation_id, and it is used in almost all types of requests, you can assign one of the pre-reserved numbers for this new field. In the same way, it takes 2 bytes to encode numbers from 16 to 2,047. Giving frequently used fields numbers between 1 and 15 will increase performance quality.

 

// order.proto
message CreateOrderRequest {
    int64 user_id = 1;
}

// main.go
request := CreateOrderRequest{
    UserId: 65
}
// send a request via gRPC

 

The request object is marshalled by the protocol buffer (http://mng.bz/D49n) into []byte to be able to be sent over gRPC. Marshalling results in some bytes containing encoding information of the metadata and the data itself (see figure 3.1):
1 The metadata section is expressed with 1 byte and has the first three bits for denoting the wire type: 000, which is type 0 (Varint) since our data type is int. (You can see the whole list here: http://mng.bz/QPV6.)
2 The first bit of the data section is called the most significant bit (MSB), and its value is 0 when there is no additional byte. Its value becomes 1 if more bytes come to encode the remaining data.
3 The remaining bits of the metadata section contain the field value.
4 The data section contains the MSB (i.e., a continuation bit) to state whether there are more bytes.
5 The remaining seven bits are used for the data itself.

 

A field’s value can be anything based on your needs, and thus cannot affect performance. However, we can affect performance by following some rules for field numbers. For example, you can use numbers less than or equal to 15 for field numbers since that is the maximum number a metadata block can store. More metadata blocks are needed to express a specified field number. In the same way, if you want to store a data value greater than 127 (the maximum capacity of a data block), you need more bytes to fit that value in those data blocks.

 

标签:Protobuf,field,fields,value,FIELD,numbers,metadata,data,NUMBERS
From: https://www.cnblogs.com/zhangzhihui/p/17830855.html

相关文章

  • 无涯教程-Dart - Numbers(数值)
    Dartnumber可以归类为-int    -  任意大小的整数。double -  64位(双精度)浮点数,由IEEE754标准指定,double数据类型用于表示小数语法-intvar_name;//声明一个整型变量doublevar_name;//声明一个双精度变量voidmain(){intnum......
  • Sitecore FieldRenderer
    前提Sitecore的@Html.Sitecore().Field("fileicon",item,new{@class="icon",})确实也挺好用的,但是局限于item,当有多个subitem,或者其他地方的item,远不如使用@Model方便。所以自己写了个Helper:publicclassCustomerRenderer:FieldRenderer{privateStac......
  • C++ ubuntu install libpq-fe.h PGconn PQconnectdb PGresult PQexec PQnfields P
    1.Installlibpq-devsudoaptinstalllibpq-devlocatelibpq-fe.h/usr/include/postgresql/libpq-fe.h 2.main.cpp#include<chrono>#include<fstream>#include<iomanip>#include<iostream>#include<sstream>#include<......
  • Required request parameter 'numbers' for method parameter type String[] is not p
    报错就是这个,然后报错的信息再给点详细的 org.springframework.web.bind.MissingServletRequestParameterException:Requiredrequestparameter'numbers'formethodparametertypeString[]isnotpresent atorg.springframework.web.method.annotation.RequestParam......
  • Flutter TextField组件的使用
    FlutterTextField组件简单的说,这个组件其实就是个输入框。1.属性constTextField({Keykey,this.controller,//控制器this.focusNode,//焦点this.decoration=constInputDecoration(),//装饰TextInputTypekeyboardType,//键盘类型,即输入类型this.textInputAction,//......
  • asp.net中怎样用Javascript控制RequiredFieldValidator控件什么时候启用,什么时候不启
    Enable/DisableRequiredFieldValidatorwithJavascriptdocument.getElementById("requiredfieldvalidatorid").enabled=false;<asp:DropDownListID="ddlServiceName"runat="server"onchange='varDateValidator=docume......
  • 关于fieldMask在go项目使用
    介绍: NetflixAPI设计实践:使用FieldMask(qq.com)该文章,详述了关于proto中,使用fieldMask的背景及收益,还有具体example提供参考。接下来,需要考虑在go项目中的使用。主要为以下几方面:1.服务端需要提供新的proto文件2.客户端需要感知这些proto文件,且需要关注声明为fiel......
  • Go 生成protobuf示例
    先安装好工具goinstallgoogle.golang.org/protobuf/cmd/[email protected]/grpc/cmd/protoc-gen-go-grpc@latest下载安装protocwgethttps://github.com/protocolbuffers/protobuf/releases/download/v25.0/protoc-25.0-linux-x86_64.zip......
  • 无涯教程-Clojure - Accessing Individual Fields函数
    可以通过与结构对象一起访问键来访问结构的各个字段。AccessingIndividual-语法:keystructure-name参数   - "key"是结构中的键值,"structure-name"是作为相应关键字的结构。返回值 - 将返回与键关联的值。以下程序显示了有关如何使用它的示例。AccessingI......
  • linux IFS(internal field separator,内部字段分隔符)
    1、简介IFS是一个shell内置变量,它是一个字符列表,列表里的每个字符是默认的字段分隔符2、查看IFS变量(1)centosset|less然后搜索字符串IFS,如下图 (2)kali的zsh环境下set|grepIFS结果如下图3、打印IFS变量因为IFS变量是空格、tab、换行,所以打印结果一片空白......