From 8ef6873e0e14309a1715a252a650bab0ae1a33ef Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sun, 20 Mar 2011 13:13:51 -0700 Subject: upb_stream: all callbacks registered ahead-of-time. This is a significant change to the upb_stream protocol, and should hopefully be the last significant change. All callbacks are now registered ahead-of-time instead of having delegated callbacks registered at runtime, which makes it much easier to aggressively optimize ahead-of-time (like with a JIT). Other impacts of this change: - You no longer need to have loaded descriptor.proto as a upb_def to load other descriptors! This means the special-case code we used for bootstrapping is no longer necessary, and we no longer need to link the descriptor for descriptor.proto into upb. - A client can now register any upb_value as what will be delivered to their value callback, not just a upb_fielddef*. This should allow for other clients to get more bang out of the streaming decoder. This change unfortunately causes a bit of a performance regression -- I think largely due to highly suboptimal code that GCC generates when structs are returned by value. See: http://blog.reverberate.org/2011/03/19/when-a-compilers-slow-code-actually-bites-you/ On the other hand, once we have a JIT this should no longer matter. Performance numbers: plain.parsestream_googlemessage1.upb_table: 374 -> 396 (5.88) plain.parsestream_googlemessage2.upb_table: 616 -> 449 (-27.11) plain.parsetostruct_googlemessage1.upb_table_byref: 268 -> 269 (0.37) plain.parsetostruct_googlemessage1.upb_table_byval: 215 -> 204 (-5.12) plain.parsetostruct_googlemessage2.upb_table_byref: 307 -> 281 (-8.47) plain.parsetostruct_googlemessage2.upb_table_byval: 297 -> 272 (-8.42) omitfp.parsestream_googlemessage1.upb_table: 423 -> 410 (-3.07) omitfp.parsestream_googlemessage2.upb_table: 679 -> 483 (-28.87) omitfp.parsetostruct_googlemessage1.upb_table_byref: 287 -> 282 (-1.74) omitfp.parsetostruct_googlemessage1.upb_table_byval: 226 -> 219 (-3.10) omitfp.parsetostruct_googlemessage2.upb_table_byref: 315 -> 298 (-5.40) omitfp.parsetostruct_googlemessage2.upb_table_byval: 297 -> 287 (-3.37) --- src/upb_decoder.h | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) (limited to 'src/upb_decoder.h') diff --git a/src/upb_decoder.h b/src/upb_decoder.h index 98b1b94..af2124c 100644 --- a/src/upb_decoder.h +++ b/src/upb_decoder.h @@ -18,7 +18,6 @@ #include #include -#include "upb_def.h" #include "upb_stream.h" #ifdef __cplusplus @@ -27,31 +26,17 @@ extern "C" { /* upb_decoder *****************************************************************/ -// The decoder keeps a stack with one entry per level of recursion. -// upb_decoder_frame is one frame of that stack. -typedef struct { - upb_fielddef *f; - size_t end_offset; // For groups, 0. -} upb_decoder_frame; - struct _upb_decoder { - // Immutable state of the decoder. - upb_src src; - upb_dispatcher dispatcher; + // Bytesrc from which we pull serialized data. upb_bytesrc *bytesrc; - // Mutable state of the decoder. - - // Msgdef for the current level. - upb_msgdef *msgdef; - - // Stack entries store the offset where the submsg ends (for groups, 0). - upb_decoder_frame *top, *limit; + // Dispatcher to which we push parsed data. + upb_dispatcher dispatcher; // Current input buffer. upb_string *buf; - // Temporary string for passing to callbacks. + // Temporary string for passing string data to callbacks. upb_string *tmp; // The offset within the overall stream represented by the *beginning* of buf. @@ -66,12 +51,11 @@ struct _upb_decoder { // End of this submessage, relative to *ptr. const char *submsg_end; + // The closure that was passed by the caller for the top-level message. + void *closure; + // Where we will store any errors that occur. upb_status *status; - - // A fake fielddef for storing the msgdef for the top-level message. - upb_fielddef f; - upb_decoder_frame stack[UPB_MAX_NESTING]; }; // A upb_decoder decodes the binary protocol buffer format, writing the data it @@ -80,18 +64,16 @@ struct _upb_decoder; typedef struct _upb_decoder upb_decoder; // Allocates and frees a upb_decoder, respectively. -void upb_decoder_init(upb_decoder *d, upb_msgdef *md); +void upb_decoder_init(upb_decoder *d, upb_handlers *handlers); void upb_decoder_uninit(upb_decoder *d); // Resets the internal state of an already-allocated decoder. This puts it in a // state where it has not seen any data, and expects the next data to be from // the beginning of a new protobuf. Parsers must be reset before they can be // used. A decoder can be reset multiple times. -void upb_decoder_reset(upb_decoder *d, upb_bytesrc *bytesrc); +void upb_decoder_reset(upb_decoder *d, upb_bytesrc *bytesrc, void *closure); -// Returns a upb_src pointer by which the decoder can be used. The returned -// upb_src is invalidated by upb_decoder_reset() or upb_decoder_free(). -upb_src *upb_decoder_src(upb_decoder *d); +void upb_decoder_decode(upb_decoder *d, upb_status *status); #ifdef __cplusplus } /* extern "C" */ -- cgit v1.2.3