How expensive is reflection?

752 views
Skip to first unread message

Leandro Lucarella

unread,
Aug 27, 2008, 10:58:41 AM8/27/08
to
Hello.

I'm facing a situation where using reflection could be useful, but I'm
wondering if this imposes a performance loss.

Would this (sightly modified extract from the C++ API[1]):

Foo foo = new Foo;
Descriptor* descriptor = foo->GetDescriptor();
FieldDescriptor* text_field = descriptor->FindFieldByName("text");
foo->ParseFromString(data);
Message::Reflection* reflection = foo->GetReflection();
std::cout << reflection->GetString(text_field);

be less efficient than this:

Foo foo;
foo.ParseFromString(data);
std::cout << foo.text();

?

Thank you.

[1] http://code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.message.html

--
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
TIGRE SE COMIO A EMPLEADO DE CIRCO: DETUVIERON A DUEÑO Y DOMADOR
-- Crónica TV

Kenton Varda

unread,
Aug 27, 2008, 1:40:27 PM8/27/08
to Leandro Lucarella, Protocol Buffers
(Please don't BCC this mailing list.  It's confusing to the recipients.)

Yes, it's less efficient, since the regular (generated) accessor is an inline method, whereas the Reflection-based accessor is virtual.  However, this only matters if you're using it in an inner loop in a performance-sensitive app.

Leandro Lucarella

unread,
Aug 27, 2008, 2:46:17 PM8/27/08
to prot...@googlegroups.com
Kenton Varda, el 27 de agosto a las 10:40 me escribiste:

> (Please don't BCC this mailing list. It's confusing to the recipients.)

I'm sorry, I've posted the message using gmane NNTP interface but for some
reason it didn't arrived so I bounced it to the list and I forgot the To:
header is not set for newsgroups =)

> Yes, it's less efficient, since the regular (generated) accessor is an
> inline method, whereas the Reflection-based accessor is virtual. However,
> this only matters if you're using it in an inner loop in a
> performance-sensitive app.

Ok, so it's just a matter of function call only, there's no extra
processing or lookups when using the reflection API?

--
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------

I am so psychosomatic it makes me sick just thinking about it!
-- George Constanza

Kenton Varda

unread,
Aug 27, 2008, 3:03:19 PM8/27/08
to Leandro Lucarella, prot...@googlegroups.com
On Wed, Aug 27, 2008 at 11:46 AM, Leandro Lucarella <llu...@gmail.com> wrote:
Ok, so it's just a matter of function call only, there's no extra
processing or lookups when using the reflection API?

Just a simple array lookup to find the byte offset.  No hashtables or anything like that.

Note that Descriptor::FindFieldByName() *does* do a hashtable lookup, but you only need to do this once.  After you have the FieldDescriptor for the field you want, you can reuse it for every message.
Reply all
Reply to author
Forward
0 new messages