Skip to content

Commit

Permalink
Only eagerly evaluate serialization / deserialization functions for n…
Browse files Browse the repository at this point in the history
…ative or bytecode backends
  • Loading branch information
andersfugmann committed Apr 16, 2024
1 parent 43a0d0d commit d651723
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion melange_test/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(melange.emit
(target output)
(libraries ocaml-protoc-plugin)
;(libraries ocaml-protoc-plugin)
)


Expand Down
9 changes: 9 additions & 0 deletions src/ocaml_protoc_plugin/ocaml_protoc_plugin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ module Service = Service
module Result = Result
module Extensions = Extensions
module Json_options = Json_options


let apply_lazy f =
match Sys.backend_type with
| Native | Bytecode ->
f ()
| Other _ ->
let f = Lazy.from_fun f in
fun x -> (Lazy.force f) x
6 changes: 3 additions & 3 deletions src/plugin/emit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,22 @@ let rec emit_message ~params ~syntax ~scope
Code.emit implementation `None "let spec () = %s" spec_str;

Code.emit implementation `Begin "let to_proto' =";
Code.emit implementation `None "let serialize = Runtime'.Serialize.serialize (spec ()) in";
Code.emit implementation `None "let serialize = Runtime'.apply_lazy (fun () -> Runtime'.Serialize.serialize (spec ())) in";
Code.emit implementation `None "fun writer %s -> serialize writer %s" destructor (String.concat ~sep:" " args);
Code.emit implementation `End "";

Code.emit implementation `None "let to_proto t = let writer = Runtime'.Writer.init () in to_proto' writer t; writer";

Code.emit implementation `Begin "let from_proto_exn =";
Code.emit implementation `None "let constructor %s = %s in" (String.concat ~sep:" " args) destructor;
Code.emit implementation `None "Runtime'.Deserialize.deserialize (spec ()) constructor";
Code.emit implementation `None "Runtime'.apply_lazy (fun () -> Runtime'.Deserialize.deserialize (spec ()) constructor)";
Code.emit implementation `End "let from_proto writer = Runtime'.Result.catch (fun () -> from_proto_exn writer)";
Code.emit implementation `Begin "let to_json options = ";
Code.emit implementation `None "let serialize = Runtime'.Serialize_json.serialize ~message_name:(name ()) (spec ()) options in";
Code.emit implementation `None "fun %s -> serialize %s" destructor (String.concat ~sep:" " args);
Code.emit implementation `EndBegin "let from_json_exn =";
Code.emit implementation `None "let constructor %s = %s in" (String.concat ~sep:" " args) destructor;
Code.emit implementation `None "Runtime'.Deserialize_json.deserialize ~message_name:(name ()) (spec ()) constructor";
Code.emit implementation `None "Runtime'.apply_lazy (fun () -> Runtime'.Deserialize_json.deserialize ~message_name:(name ()) (spec ()) constructor)";
Code.emit implementation `End "let from_json json = Runtime'.Result.catch (fun () -> from_json_exn json)";
| None -> ()
in
Expand Down

0 comments on commit d651723

Please sign in to comment.