Categorygithub.com/billyplus/fastproto
modulepackage
0.0.0-20231008085235-da76002eecc0
Repository: https://github.com/billyplus/fastproto.git
Documentation: pkg.go.dev

# README

Protocol Buffers for Go with faster Marshal and Unmarshal methods

FastProto is a go support for adding extra Marshal and Unmarshal methods to standard generated protobuf file inspired by gogo/protobuf. For now, FastProto support proto 3 only.

Getting Started

Installation

Install the standard protocol buffer implementation from https://github.com/protocolbuffers/protobuf first.

Then install the protoc-gen-go-fast binary

go install github.com/billyplus/fastproto/cmd/protoc-gen-go-fast@latest

How to use

Generate standard .pb.go with protoc-gen-go first, then generate an extra _fast.pb.go file with Marshal and Unmarshal methods.

protoc --go_out=./ ./test/msg.proto
protoc --go-fast_out=./ ./test/msg.proto

Marshal message

    msg := &pb.SomeProtoMsg{}
    if err := fastproto.Marshal(msg); err!=nil {}

    // you can allocate []byte first
    data := make([]byte, msg.Size())
    if n, err := fastproto.MarshalTo(data, msg); err!=nil {}
    // result is data[:n]

Unmarshal message

    msg := &pb.SomeProtoMsg{}
    if err := fastproto.Unmarshal(data, &msg); err!=nil {}

GRPC

It works with grpc.

Option 1. Replace the default codec for proto

import "google.golang.org/grpc/encoding"

func main() {
    // replace the default codec.
	encoding.RegisterCodec(fastproto.ProtoCodec())
}

Option 2. Not recommended. Use grpc.ForceServerCodec option or grpc.CustomCodec option to create grpc server. Notice: This API is may be changed or removed in a later release.

    import "google.golang.org/grpc"
    s := grpc.NewServer(grpc.CustomCodec(fastproto.ProtoCodec()))

    // or

    s := grpc.NewServer(grpc.ForceServerCodec(fastproto.ProtoCodec()))

Options to control code generation

if you want to ignore marshal method

    import "github.com/billyplus/fastproto/options/options.proto"

    // if true, Marshal interface will not be generated for this file
    option (options.fastproto_no_marshaler) = true;

    message XXXX {
        // if true, Marshal interface will be generated for this message whatever fastproto_no_marshaler is.
        option (options.fastproto_msg_marshaler) = true;
        // if true, Marshal interface will not be generated for this message
        option (options.fastproto_msg_no_marshaler) = true;
    }

if you want to ignore unmarshal method

    import "github.com/billyplus/fastproto/options/options.proto"

    // if true, Unmarshal interface will not be generated for this file
    option (options.fastproto_no_unmarshaler) = true;

    message XXXX {
        // if true, Unmarshal interface will be generated for this message whatever fastproto_no_unmarshaler is.
        option (options.fastproto_msg_unmarshaler) = true;
        // if true, Unmarshal interface will not be generated for this message
        option (options.fastproto_msg_no_unmarshaler) = true;
    }

please check options/options.proto for other options.

Benchmarks

code

goos: linux
goarch: amd64
pkg: github.com/billyplus/fastproto/test
cpu: AMD Ryzen 9 5950X 16-Core Processor
FastMarshalStringSlice-61464470781.75 ns/op80 B/op1 allocs/op
StandardMarshalStringSlice-68223910144.4 ns/op80 B/op1 allocs/op
FastMarshalBytesSlice-61306502293.40 ns/op80 B/op1 allocs/op
StandardMarshalBytesSlice-610043254124.9 ns/op80 B/op1 allocs/op
FastMarshalInt32Slice-65772819213.1 ns/op128 B/op1 allocs/op
StandardMarshalInt32Slice-65056791237.5 ns/op128 B/op1 allocs/op
FastMarshalSint64Slice-64123633288.3 ns/op224 B/op1 allocs/op
StandardMarshalSint64Slice-63811389311.4 ns/op224 B/op1 allocs/op
FastMarshalSfixed32Slice-61625707473.97 ns/op112 B/op1 allocs/op
StandardMarshalSfixed32Slice-61291785093.63 ns/op112 B/op1 allocs/op
FastMarshalSfixed64Slice-61400351089.69 ns/op208 B/op1 allocs/op
StandardMarshalSfixed64Slice-611058189115.9 ns/op208 B/op1 allocs/op
FastMarshalToMixedProto-67473415354 ns/op0 B/op0 allocs/op
FastMarshalMixedProto-64384427804 ns/op18432 B/op1 allocs/op
StandardMarshalMixedProto-61255294428 ns/op37664 B/op1521 allocs/op
FastSizeMixedProto-62054326061 ns/op0 B/op0 allocs/op
StandardSizeMixedProto-63241239230 ns/op9616 B/op760 allocs/op
FastUnmarshalStringSlice-64322337291.3 ns/op314 B/op7 allocs/op
StandardUnmarshalStringSlice-63088686384.5 ns/op314 B/op7 allocs/op
FastUnmarshalBytesSlice-63194150376.0 ns/op448 B/op8 allocs/op
StandardUnmarshalBytesSlice-62770154426.6 ns/op448 B/op8 allocs/op
FastUnmarshalInt32Slice-66377149183.2 ns/op112 B/op1 allocs/op
StandardUnmarshalInt32Slice-63752682318.7 ns/op248 B/op5 allocs/op
FastUnmarshalSint64Slice-64416526271.5 ns/op208 B/op1 allocs/op
StandardUnmarshalSint64Slice-62903524405.0 ns/op504 B/op6 allocs/op
FastUnmarshalSfixed32Slice-61431300185.07 ns/op112 B/op1 allocs/op
StandardUnmarshalSfixed32Slice-65353230224.2 ns/op248 B/op5 allocs/op
FastUnmarshalSfixed64Slice-612808696103.0 ns/op208 B/op1 allocs/op
StandardUnmarshalSfixed64Slice-63824290317.3 ns/op504 B/op6 allocs/op
FastUnmarshalMixedProto-62058058110 ns/op46909 B/op606 allocs/op
StandardUnmarshalMixedProto-68949132525 ns/op60842 B/op1966 allocs/op

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
data must have enough space for message which means cap(data) >= msg.Size(), or else it would return error the return int indicate how many bytes of data is used.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Unmarshal parses the wire-format message in b and places the result in m.

# Structs

No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author