Thanks for the feedback.
On 02/16/2010 10:44 PM, Kenton Varda wrote:
GeneratedMessageReflection is an internal class used by the protobuf
implementation. Currently, users are not allowed to use it directly,
because we may change it at any time. You're suggesting that we promote
it to a public interface, which has implications with regard to
maintenance costs and implementation agility.
I understand that.
I'm not necessarily suggesting to make this particular class public, but
am looking for some way to iterate over the elements of a repeated field.
I see that this is available for classes that are generated by protoc,
which is what is mostly used, but unfortunately we don't/can't really do
that with R.
On the same score, if I want to increase the number of elements in a
repeated field, I have to do it one by one right ? I can't do something
like first reserve space, and then fill the generated space. Does that
mean that memory is reallocated each time ?
I'm open to considering making this change for performance purposes.
However, even them I'm hesitant to expose Repeated[Ptr]Field
references directly via this interface. I'd like to see what happens if
we simply make all of the existing public accessor methods inline, so
you could then do something like:
int size = reflection-FieldSize(message, field);
for (int i = 0; i size; i++) {
const Message sub_message =
reflection-GeneratedMessageReflection::GetRepeatedMessage(message,
field, i);
// Do something with sub_message.
}
If GetRepeatedMessage is inline then I believe the above loop would be
nearly as efficient as iterating directly over a RepeatedPtrField. Note
that the funny syntax for the method call avoids making a virtual call.
BTW, the has_templates template you suggest would not work as you
think -- how would you actually use it?
implement some sort of TMP dispatch :
template typename T
foo( T t ){
foo_dispatch( t, typename has_templatesT::value_type() ) ;
}
and then have
template typename T
foo( T t , false_type){
/* the less efficient version */
}
template typename T
foo( T t, true_type){
/* the more efficient version using the RepeatedField */
}
This is similar to how e.g std::distance dispatches depending on whether
it deals with random access iterator or some other iterator category.
What you would really need to
use is dynamic_cast. My golden rule of dynamic_cast is that it should
only be used for optimization, and you must provide an implementation
for the case where dynamic_cast always returns NULL.
But dynamic_cast is a runtime thing, where TMP dispatch happens at
compile time.
In your case, you
are doing this, so that should be fine.
On Sat, Feb 13, 2010 at 1:16 AM, Romain Francois
romain.francois.r.enthusi...@gmail.com
mailto:romain.francois.r.enthusi...@gmail.com wrote:
Hello,
Thanks for the answers.
Maybe I should give some more background on why this is of interest
to me. In RProtoBuf, we essentially only use the reflection api so
that we can dynamically load new proto message types at runtime, etc
... we don't use protoc and therefore have no access to the
generated classes.
In the class GeneratedMessageReflection, there are templates such as :
template typename Type
inline Type GetRepeatedField(const Message message,
const FieldDescriptor* field,
int index) const;
but they are private ? Why ?
From what I can read of the code, methods like GetRepeatedInt32
get expanded out of :
PASSTYPE GeneratedMessageReflection::GetRepeated##TYPENAME(
\
const Message message, \
const FieldDescriptor* field, int index) const { \
USAGE_CHECK_ALL(GetRepeated##TYPENAME, REPEATED, CPPTYPE); \
if (field-is_extension()) { \
return GetExtensionSet(message).GetRepeated##TYPENAME( \
field-number(), index); \
} else { \
return GetRepeatedFieldTYPE(message, field, index); \
} \
} \
so doing things like this code
for( int i=0; isize; i++){
INTEGER(res)[i] = (int) ref-GetRepeatedInt32(
*message,
fieldDesc,
i ) ;
}
is going to be not as efficient as if I could directly iterate over
the repeated field using RepeatedField::iterator
Instead of extending the Reflection interface, what about making the
templates public in GeneratedMessageReflection and then maybe use
some sort of trait to indicate whether that the instance of
Reflection I have access to has these templates.
Something like :
template typename _T, _T _V struct integral_constant {
static const _Tvalue = _V;
typedef _T value_type;