Files of check-in [26de579de1] in the top-level directory

Elixir Protocol Buffer

Warning: only protocol buffers 3 is supported. Use protobuf-elixir if you need support for version 2 (protobuf-elixir was a major inspiration for this project).

This is a protocol buffer encoder and decoder. Its goal is to be fast at the cost of larger generated files. This is achieved by generate a significant part of the encoding and decoding logic at generation time with the protoc plugin.

Encoding and decoding performance is ~3-4x times faster yhan protobuf-elixir. For example, if we take our test %Everything structure, which has all field types, including all array types (with 2 values per array) and a few maps, pbuf takes ~14µs to encode and ~24µs to decode, versus 66µs and 67µs. However, the .beam file is 19K vs 7K.


Assuming you already have protoc installed, you'll want to run:

$ mix escript.install hex pbuf

to install the pbuf elixir generator. This will place protoc-gen-fast-elixir in your ~/.mix/escript/ folder. This must be on your $PATH.

You can then generate elixir files using the protoc command with the -fast-elixir_out=PATH flag:

protoc --fast-elixir_out=generated/ myschema.proto 

Note the name fast-elixir_out. This allows you to also have protobuf installed in order to support proto2 syntax.


The generated code is normal Elixir modules with a defstruct. Use new/1 to create new instances:

user = "leto", age: 2000)

And Pbuf.encode!/1 and Pbuf.encode_to_iodata!/1 to encode them:

data = Pbuf.encode!(user)

Only structures generated by protoc can be passed to encode!/1 and encode_to_iodata!/1; you cannot pass maps or other structures.

These functions will raise a Pbuf.Encoder.Error on invalid data (such as assigning a float to a bool field). There are currently no non-raising functions.


Decoding is done via Pbuf.decode!/2:

user = Pbuf.decode!(Models.User, data)

As an alternative, you can also use: Models.User.decode!(data).

Unlike encoding, there are non-raising versions of decode!:

# or use Models.user.decode(data)
case Pbuf.decode(Models.User, data) do
  {:ok, user} -> ...
  {:error, err} -> # err is a %Pbuf.Decode.Error{}

Decoding truly invalid data (as opposed to simply unexpected types) can raise.


A field declared as an enum should be set to the atom representation of the protocol buffer name, or the integer value. For example, a message defined as:

message User {
  UserType type = 1;

enum UserType {

Should be used as:

# OR
user = 1)

(casing is preserved from the proto file)


The value of a oneof field must be set to a tuple where the first element is the name of the field and the second is the value. Given:

message Event {
  oneof event_oneof {
    Commit commit = 1;
    Wiki wiki = 2;

Then valid values for event_oneof are: nil, {:commit, Commit.t} or {:wiki, Wiki.t}.

What's Ugly?

There are two distinctly ugly parts of the code. The first is pretty much anything to do with oneof fields. The second is the decoding of maps.