4 ms·
[Not sure whether the question is directed to the author or to my comment but I'll answer anyway :) ] Basically, there are 3 notions 1) the buffer (byte[] or
by pacoverdi 14y ago
[Not sure whether the question is directed to the author or to my comment but I'll answer anyway :) ]
Basically, there are 3 notions
1) the buffer (byte[] or whatever) containing the data, immutable at least while the data is being accessed by a given thread
2) the Struct object: defines the structure of a message (field length, offset, arrays etc.)
3) the Message (aka the flyweight) that points to a given offset in buffer and is bound to a Struct instance
The buffer and the message are normally accessed by only one thread, but several threads can share Struct instances (i.e. the dictionary).
Each field (even deep in the field hierarchy) knows its offset relative to the beginning of the message, so values can be accessed like eg.
int foo = struct.foo.getInt(msg);
The only complicated thing is when dealing with arrays (especially when nested in other arrays). The message does the bookkeeping necessary to safely access elements, ie. the mutable state is stored in the flyweight.