1.8 Advanced Topics
1.8.1 Precompiled Messages
Performance can be significantly improved using a strategy of precompiling the constant portions of your message. Enumerations for example, are excellent candidates for precompilation. Using protobuf_message/3, the precompiled portion of the message is inserted directly in the wire-stream on encode, and is unified with, and removed from the wire-stream on decode. The following shows how the "send_command" example above, can be converted to precompiled form:
:- dynamic precompiled_message/3. send_precompiled_command(Command, Vector, Msg) :- basic_vector(Vector, Proto1), precompiled_message(commands(Command), Msg, Tail), protobuf_message(protobuf([embedded(3, Proto1)]), Tail). precompile_commands :- abolish(precompiled_message/3), forall(protobufs:commands(Key, _), ( Proto = protobuf([atom(1, command), enum(2, commands(Key))]), protobuf_message(Proto, Msg, Tail), assert(precompiled_message(commands(Key), Msg, Tail)) )), compile_predicates([precompiled_message/3]). * * * :- initialization precompile_commands.
1.8.2 Supplying Your Own Host Type Message Sequences
You can extend the parser to support your own compound host types.
These are treated as first class entities by the parser. That is they
can be used either by themselves, or in repeated
and embedded
clauses just as any other host type would be. You do this by hooking
into the parser and adding your own message_sequence
productions. Your hook eventually calls back into the parser with your
substitution/expansion protobuf, which is then embedded in the wire
stream. Recursive structures can be defined this way. A simple example
of a recursive XML like structure is shown in the appendix.