From 86bad61b76a260ffc442acffbe58feee67df45e5 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 24 Mar 2012 11:24:16 -0700 Subject: Sync from internal Google development. Many improvements, too many to mention. One significant perf regression warrants investigation: omitfp.parsetoproto2_googlemessage1.upb_jit: 343 -> 252 (-26.53) plain.parsetoproto2_googlemessage1.upb_jit: 334 -> 251 (-24.85) 25% regression for this benchmark is bad, but since I don't think there's any fundamental design issue that caused it I'm going to go ahead with the commit anyway. Can investigate and fix later. Other benchmarks were neutral or showed slight improvement. --- bindings/cpp/upb/bytestream.hpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'bindings/cpp/upb/bytestream.hpp') diff --git a/bindings/cpp/upb/bytestream.hpp b/bindings/cpp/upb/bytestream.hpp index 968d542..81134b9 100644 --- a/bindings/cpp/upb/bytestream.hpp +++ b/bindings/cpp/upb/bytestream.hpp @@ -68,6 +68,7 @@ #include "upb/bytestream.h" #include "upb/upb.hpp" +#include namespace upb { @@ -204,6 +205,18 @@ class ByteRegion : public upb_byteregion { return upb_byteregion_strdup(this); } + template void AssignToString(T* str) { + uint64_t ofs = start_ofs(); + str->clear(); + str->reserve(Length()); + while (ofs < end_ofs()) { + size_t len; + const char *ptr = GetPtr(ofs, &len); + str->append(ptr, len); + ofs += len; + } + } + // TODO: add if/when there is a demonstrated need. // // // Pins this byteregion's bytes in memory, allowing it to outlive its @@ -220,12 +233,24 @@ class ByteRegion : public upb_byteregion { class StringSource : public upb_stringsrc { public: StringSource() : upb_stringsrc() { upb_stringsrc_init(this); } + template explicit StringSource(const T& str) { + upb_stringsrc_init(this); + Reset(str); + } + StringSource(const char *data, size_t len) { + upb_stringsrc_init(this); + Reset(data, len); + } ~StringSource() { upb_stringsrc_uninit(this); } void Reset(const char* data, size_t len) { upb_stringsrc_reset(this, data, len); } + template void Reset(const T& str) { + Reset(str.c_str(), str.size()); + } + ByteRegion* AllBytes() { return static_cast(upb_stringsrc_allbytes(this)); } @@ -233,6 +258,14 @@ class StringSource : public upb_stringsrc { upb_bytesrc* ByteSource() { return upb_stringsrc_bytesrc(this); } }; +template <> inline ByteRegion* GetValue(Value v) { + return static_cast(upb_value_getbyteregion(v)); +} + +template <> inline Value MakeValue(ByteRegion* v) { + return upb_value_byteregion(v); +} + } // namespace upb #endif -- cgit v1.2.3