From 3a97180293149e5036da3067bd54258d866d16f0 Mon Sep 17 00:00:00 2001 From: lahiker42 Date: Wed, 10 Feb 2010 18:04:17 +0000 Subject: [PATCH] optimziation git-svn-id: https://protobuf-c.googlecode.com/svn/trunk@206 00440858-1255-0410-a3e6-75ea37f81c3a --- ChangeLog | 2 ++ src/google/protobuf-c/protobuf-c.c | 25 ++++++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1974d0a..74f0b02 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,8 @@ - Add --disable-protoc to only build libprotobuf-c (daveb) - Bug fixes for protobuf_c_enum_descriptor_get_value_by_name() and protobuf_c_service_descriptor_get_method_by_name() + - if descriptor->message_init != NULL, use it from unpack() + as an optimization (daveb) 0.12: - for field names which are reserved words, use the real name diff --git a/src/google/protobuf-c/protobuf-c.c b/src/google/protobuf-c/protobuf-c.c index 11e91d2..26c4b89 100644 --- a/src/google/protobuf-c/protobuf-c.c +++ b/src/google/protobuf-c/protobuf-c.c @@ -1448,11 +1448,17 @@ parse_member (ScannedMember *scanned_member, return 0; } -static inline void -setup_default_values (ProtobufCMessage *message) + +/* TODO: expose/use this function if desc->message_init==NULL + (which occurs for old code, and may be useful for certain + programatic techniques for generating descriptors). */ +void +protobuf_c_message_init_generic (const ProtobufCMessageDescriptor *desc, + ProtobufCMessage *message) { - const ProtobufCMessageDescriptor *desc = message->descriptor; unsigned i; + memset (message, 0, desc->sizeof_message); + message->descriptor = desc; for (i = 0; i < desc->n_fields; i++) if (desc->fields[i].default_value != NULL && desc->fields[i].label != PROTOBUF_C_LABEL_REPEATED) @@ -1548,10 +1554,15 @@ protobuf_c_message_unpack (const ProtobufCMessageDescriptor *desc, DO_ALLOC (rv, allocator, desc->sizeof_message, return NULL); scanned_member_slabs[0] = first_member_slab; - memset (rv, 0, desc->sizeof_message); - rv->descriptor = desc; - - setup_default_values (rv); + /* Generated code always defines "message_init". + However, we provide a fallback for (1) users of old protobuf-c + generated-code that do not provide the function, + and (2) descriptors constructed from some other source + (most likely, direct construction from the .proto file) */ + if (desc->message_init != NULL) + protobuf_c_message_init (desc, rv); + else + protobuf_c_message_init_generic (desc, rv); while (rem > 0) {