Thoughts of Hiram Chirino

Friday, September 18, 2009

ActiveMQ Protobuf Implemtation Features

I promised I would follow up on my previous post on how the "The ActiveMQ Protobuf Implementation Rocks!".

So you might be asking yourself, what's the secret sauce? Well before I get into that, let me first explain the class model that our proto compiler generates.

For every message definition in the '.proto' file, the compiler will generate 3 classes:
  • the message interface: is implemented by the bean and buffer classes. It has all the 'getters'.
  • the bean class: has all the 'setters' and 'merge' methods
  • the buffer class: has all the encoding and decoding methods. It does not allow mutation.
The message interface also defines the freeze(), frozen(), and copy() methods which allow you to make an instance immutable, check to see if an instance is immutable, and create a mutable copy. Buffer classes are alway immutable. Bean class can transition to being immutable via freeze(). freeze() naturally returns a buffer object. copy() naturally returns a bean object.

This bean model gives substantial flexibility. Besides making it easy to transition from immutable to mutable and back, the message interface lets you implement business methods that operate against either type of instance. You could use the bean class purely in a builder style to always generate a buffer instance, or you could just use them like traditional java bean objects.

Once a bean instance is frozen, any attempts to modify the instance will throw assertion errors if assertions are enabled in your JVM. So the CPU cost of validating program correctness can can be disabled at run time.

Finally, the most important feature of the buffer class is that it holds on to either the byte array that it was created from or the frozen bean that created it, and sometimes both, after it builds one from the other. This has several implications. Firstly, once a buffer is encoded to a byte[], subsequent encoding passes are free. This is also true when a buffer is decoded, as the next encoding is free since it still retains the original encoding of the message. And the other benefit that this provides which the benchmark highlighted, is that deferred decoding is possible. A newly created buffer class will not decode the data until a field is accessed. This also true of the nested messages that are encoded in a buffer. While the outer message may get decoded, the nested message will not be decoded until it's fields are accessed.

posted by Hiram Chirino at 1:57 PM spacer spacer

2 Comments:

  • Looks cool. Sounds like it lazily deserializes on demand.

    By spacer  woolfel, At 9:13 PM  

  • exactly.

    By spacer  Hiram Chirino, At 9:34 PM  

Post a Comment



<$I18N$LinksToThisPost>:

Create a Link

<< Home


 
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.