mransan/ocaml-protoc

Immutable record fields by default

Closed this issue · 2 comments

Why do you generate mutable record fields? It may be useful, as an option, but it seems more appropriate to go with immutable records by default.

The number one reason was the performance of the generated code which on my initial benchmark was faster with mutable field.

In order to support non mutable field i was thinking of 2 options:

  • Have a different backend with mostly functional decoding and keep the same function signature.
  • Keep the mutable type only in the .ml file, do all the decoding using the mutable type but expose a purely functional type in the .mli. Since the code generator can guarantee that the types have the exact same structure, Obj.magic could be used when returning the value. This would combine both speed and functional interface.

@artemkin @bendiken I modified the compiler to generate immutable fields by default. I also introduce the support for a custom field option in the .proto file called (ocam_mutable) which allows the author of the .proto file to generate mutable fields in OCaml.

The decoding performance is not affected by this change since the .ml file uses internally a record with all field mutable.

Thanks for your suggestion!