zig-protobuf icon indicating copy to clipboard operation
zig-protobuf copied to clipboard

MessageMixins included in every struct, is this redundancy?

Open jiacai2050 opened this issue 1 year ago • 1 comments

Take this file as an example:

syntax = "proto3";

package test_demo;
message Foo {
  string name = 1;
  repeated string loved = 2;
  int32 birth = 3;
}
protoc --zig_out=. foo.proto

This will output

// Code generated by protoc-gen-zig
 ///! package test_demo
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;

const protobuf = @import("protobuf");
const ManagedString = protobuf.ManagedString;
const fd = protobuf.fd;

pub const Foo = struct {
    name: ManagedString = .Empty,
    loved: ArrayList(ManagedString),
    birth: i32 = 0,

    pub const _desc_table = .{
        .name = fd(1, .String),
        .loved = fd(2, .{ .List = .String}),
        .birth = fd(3, .{ .Varint = .Simple }),
    };

    pub usingnamespace protobuf.MessageMixins(@This());
};

It seems every struct contains a MessageMixins to provide decode/encode methods.

Since those methods are generics over T, We can do just let users do this with protobuf.pb_encode(struct, allocator), this may be beneficial for compile time?

Happy to hear what your thoughts, thanks.

jiacai2050 avatar Apr 03 '24 03:04 jiacai2050

Hello @jiacai2050 ,

I'm pretty sure the zig compiler only compiles code that you actively use. This is what makes zig's stdblib so lightweight to use, after all: only the part you actively use are imported and compiled.

Importing all the interfaces using MessageMixins should then not have any important impact on compilation times, especially if you consider that from the compiler's point of view, protobuf.pb_encode(struct, allocator) is strictly the same thing as doing struct.encode(allocator). (i at least expect the compiler to inline such a trivial indirection).

See https://ziglang.org/documentation/master/#struct

Thanks a lot for your interest in the project, I hope I answered your question properly.

Arwalk avatar Apr 03 '24 14:04 Arwalk

Closing, I consider the question answered.

Arwalk avatar Jun 05 '24 14:06 Arwalk