On Wed, Mar 17, 2010 at 2:06 AM, Thorsten <schu...@gmail.com> wrote: > I already modified the value in my copy and now I am using a modified > copy of libprotobuf. That's fine. > I was wondering whether this feature is of use to others as well. I > haven't thought about reflection yet. > But if you define RepeatedField like this: > > template <typename Element, int kInitialSize> > class RepeatedField { > ... > private: > Element* elements_; > int current_size_; > int total_size_; > > Element initial_space_[kInitialSize]; > }; > > So that, initial_space_ is the last field, the offsets of the fields > should be independent of kInitialSize. >
According to C++ aliasing rules, it is not legal to dereference a pointer of type RepeatedField<T, n>* if it actually points at RepeatedField<T, m> where m != n. In practice, doing so could lead to very subtle bugs and the compiler optimizes around the assumption that pointers to RepeatedField<T, n> and RepeatedField<T, m> could not possibly point to the same value. So, unfortunately, this is not an option. What *would* be an option would be for all RepeatedField objects to implement some shared abstract interface, and then we could call that. Earlier versions of protobufs actually used this approach. Unfortunately, having the extra vtable pointer in every object increased memory usage enough to significantly affect performance. > > Thorsten > > On Mar 16, 10:02 pm, Kenton Varda <ken...@google.com> wrote: > > We can't make it a template parameter because it would break reflection, > > which depends on being able to find the RepeatedField objects via pointer > > offsets. If it doesn't know exactly which template instance of > > RepeatedField it is looking for it wouldn't work. > > > > On Tue, Mar 16, 2010 at 2:00 PM, Kenton Varda <ken...@google.com> wrote: > > > You can, of course, modify the value all you want in your copy of the > code. > > > However, the value is only useful if it is a compile-time constant, so > we > > > can't really parameterize it in general. Note that if you change the > value, > > > you must recompile libprotobuf and anything you have that depends on > it. > > > > > On Tue, Mar 16, 2010 at 5:39 AM, Thorsten <schu...@gmail.com> wrote: > > > > >> Hi all, > > > > >> let's say you have a large number of messages of the same type in > > >> memory. One field of this type is marked as repeated: > > >> message Test { > > >> repeated uint64 foo = 1; > > >> } > > >> RepeatedField defines the default size to be 4. If most of my objects > > >> have 5 foos, I waste a lot of memory. > > > > >> Is it possible to make kInitialSize into a template parameter of > > >> RepeatedField? Thatway I could have different default sizes for > > >> different message types. The message description could provide an > > >> option for setting a new default length: > > >> message Test { > > >> repeated uint64 foo = 1 [default_size=5]; > > >> } > > > > >> Cheers, > > >> Thorsten > > > > >> -- > > >> You received this message because you are subscribed to the Google > Groups > > >> "Protocol Buffers" group. > > >> To post to this group, send email to proto...@googlegroups.com. > > >> To unsubscribe from this group, send email to > > >> protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com> > <protobuf%2bunsubscr...@googlegroups.com<protobuf%252bunsubscr...@googlegroups.com> > > > > >> . > > >> For more options, visit this group at > > >>http://groups.google.com/group/protobuf?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Protocol Buffers" group. > To post to this group, send email to proto...@googlegroups.com. > To unsubscribe from this group, send email to > protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/protobuf?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.