Java中常用字段的Protobuf部分序列化

问题描述 投票:0回答:1

我有一条 protobuf 消息,需要发送给多个客户端。该消息对于每个客户端具有相同的数据,除了一个字段(序列号)对于每个客户端来说是不同的。现在,我可以更改生成的 Java 消息对象中的字段,并分别为每个客户端序列化消息。但是有没有一种方法可以序列化除一个字段之外的所有内容,然后仅为每个客户端序列化该字段(例如,交换序列化消息中的相应字节或其他内容)?

编辑:我见过 mergeFrom 方法来合并两条消息,但根据我的理解,它首先解析消息,然后交换数据,然后您可以再次序列化它,所以根本不是性能优化(?)。

java protocol-buffers proto3
1个回答
0
投票

我认为这将具有挑战性,特别是如果您的序列不是消息中的第一个字段并且不是固定长度。

参见编码

即使对于使用固定长度

fixed32
和可变长度
uint32
标量类型 的琐碎消息,您也可以看到其复杂性:
foo.proto

syntax = "proto3";

option go_package = "stackoverflow/77923370/protos";

message VLengthID {
  uint32 id = 1;
}
message FLengthID {
  fixed32 id = 1;
}

并且:

MODULE="stackoverflow/77923370"

protoc \
--proto_path=${PWD}/protos \
--go_out=${PWD} \
--go_opt=module=${MODULE} \
${PWD}/protos/foo.proto

并且:

main.go

package main

import (
    "fmt"
    "time"

    pb "stackoverflow/77923370/protos"

    "google.golang.org/protobuf/proto"
)

func main() {
    value := uint32(1)

    mFLengthID := &pb.FLengthID{}
    mVLengthID := &pb.VLengthID{}

    for i := 0; i < 32; i++ {
        mFLengthID.Id = value
        bFlengthID, _ := proto.Marshal(mFLengthID)

        mVLengthID.Id = value
        bVLengthID, _ := proto.Marshal(mVLengthID)

        fmt.Printf(
            "Result i=%2d value=%10d fixed=%+x variable=%+x\n",
            i,
            value,
            bFlengthID,
            bVLengthID,
        )

        value *= 2

        time.Sleep(1 * time.Second)
    }
}

产量:

Result i= 0 value=         1 fixed=0d01000000 variable=0801
Result i= 1 value=         2 fixed=0d02000000 variable=0802
Result i= 2 value=         4 fixed=0d04000000 variable=0804
Result i= 3 value=         8 fixed=0d08000000 variable=0808
Result i= 4 value=        16 fixed=0d10000000 variable=0810
Result i= 5 value=        32 fixed=0d20000000 variable=0820
Result i= 6 value=        64 fixed=0d40000000 variable=0840
Result i= 7 value=       128 fixed=0d80000000 variable=088001
Result i= 8 value=       256 fixed=0d00010000 variable=088002
Result i= 9 value=       512 fixed=0d00020000 variable=088004
Result i=10 value=      1024 fixed=0d00040000 variable=088008
Result i=11 value=      2048 fixed=0d00080000 variable=088010
Result i=12 value=      4096 fixed=0d00100000 variable=088020
Result i=13 value=      8192 fixed=0d00200000 variable=088040
Result i=14 value=     16384 fixed=0d00400000 variable=08808001
Result i=15 value=     32768 fixed=0d00800000 variable=08808002
Result i=16 value=     65536 fixed=0d00000100 variable=08808004
Result i=17 value=    131072 fixed=0d00000200 variable=08808008
Result i=18 value=    262144 fixed=0d00000400 variable=08808010
Result i=19 value=    524288 fixed=0d00000800 variable=08808020
Result i=20 value=   1048576 fixed=0d00001000 variable=08808040
Result i=21 value=   2097152 fixed=0d00002000 variable=0880808001
Result i=22 value=   4194304 fixed=0d00004000 variable=0880808002
Result i=23 value=   8388608 fixed=0d00008000 variable=0880808004
Result i=24 value=  16777216 fixed=0d00000001 variable=0880808008
Result i=25 value=  33554432 fixed=0d00000002 variable=0880808010
Result i=26 value=  67108864 fixed=0d00000004 variable=0880808020
Result i=27 value= 134217728 fixed=0d00000008 variable=0880808040
Result i=28 value= 268435456 fixed=0d00000010 variable=088080808001
Result i=29 value= 536870912 fixed=0d00000020 variable=088080808002
Result i=30 value=1073741824 fixed=0d00000040 variable=088080808004
Result i=31 value=2147483648 fixed=0d00000080 variable=088080808008
© www.soinside.com 2019 - 2024. All rights reserved.