For me, the biggest benefit of immutability here is that it prevents
cycles in the object graph. (Well, I think this is true for Java and
C++. In Haskell, though, this is not the case). Without cycles, the
serialization code doesn't have to worry about getting stuck in an
infinite loop (though it's more likely that it'll eventually stack
overflow...).
The downside is the potential inefficiency of making unnecessary
copies of the data structures. I'm on board with the benefits of
immutability, so I'm usually willing to take the performance hit, but
I wasn't sure if others would be as well. Have you guys gotten any
requests to add a "generate mutable data structures" mode to protoc?
If I do end up having to support mutable structures, maybe there's
some clever way to efficiently prevent cycles in an object graph as it
is being manipulated. I really don't want to have to detect cycles in
the serializer (I think cycle detection is what makes Java's built-in
serialization so slow...).
- Kannan
The reason I'm asking all this is that I'm implementing a data
serialization format that has the same usage model as protocol buffers
(i.e. generate language bindings, serialize/deserialize).
For me, the biggest benefit of immutability here is that it prevents
cycles in the object graph. (Well, I think this is true for Java and
C++. In Haskell, though, this is not the case). Without cycles, the
serialization code doesn't have to worry about getting stuck in an
infinite loop (though it's more likely that it'll eventually stack
overflow...).
The downside is the potential inefficiency of making unnecessary
copies of the data structures.
I'm on board with the benefits of
immutability, so I'm usually willing to take the performance hit, but
I wasn't sure if others would be as well. Have you guys gotten any
requests to add a "generate mutable data structures" mode to protoc?
If I do end up having to support mutable structures, maybe there's
some clever way to efficiently prevent cycles in an object graph as it
is being manipulated. I really don't want to have to detect cycles in
the serializer (I think cycle detection is what makes Java's built-in
serialization so slow...).