diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2017-02-24 15:41:09 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-24 15:41:09 -0800 |
commit | b4b0e304be5a68de3d0ee1af9b286f958750f5e4 (patch) | |
tree | 5e2d0be798c9dd8edef7056a6e7e126a01ecc588 /src | |
parent | 8387b88cdc9f60b6f5e9ab9e1a69de719f3751c1 (diff) | |
parent | bd158fc23849663f1cdb430ddea2dbab9deb336f (diff) | |
download | protobuf-b4b0e304be5a68de3d0ee1af9b286f958750f5e4.tar.gz protobuf-b4b0e304be5a68de3d0ee1af9b286f958750f5e4.tar.bz2 protobuf-b4b0e304be5a68de3d0ee1af9b286f958750f5e4.zip |
Merge pull request #2355 from xfxyjwf/fixjson
Speed up JSON parsing.
Diffstat (limited to 'src')
-rw-r--r-- | src/google/protobuf/util/json_util.cc | 29 | ||||
-rw-r--r-- | src/google/protobuf/util/json_util.h | 5 |
2 files changed, 20 insertions, 14 deletions
diff --git a/src/google/protobuf/util/json_util.cc b/src/google/protobuf/util/json_util.cc index c65e5323..129b6eaf 100644 --- a/src/google/protobuf/util/json_util.cc +++ b/src/google/protobuf/util/json_util.cc @@ -49,22 +49,25 @@ namespace protobuf { namespace util { namespace internal { +ZeroCopyStreamByteSink::~ZeroCopyStreamByteSink() { + stream_->BackUp(buffer_size_); +} + void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) { - while (len > 0) { - void* buffer; - int length; - if (!stream_->Next(&buffer, &length)) { - // There isn't a way for ByteSink to report errors. + while (true) { + if (len <= buffer_size_) { + memcpy(buffer_, bytes, len); + buffer_ = static_cast<char*>(buffer_) + len; + buffer_size_ -= len; return; } - if (len < length) { - memcpy(buffer, bytes, len); - stream_->BackUp(length - len); - break; - } else { - memcpy(buffer, bytes, length); - bytes += length; - len -= length; + memcpy(buffer_, bytes, buffer_size_); + bytes += buffer_size_; + len -= buffer_size_; + if (!stream_->Next(&buffer_, &buffer_size_)) { + // There isn't a way for ByteSink to report errors. + buffer_size_ = 0; + return; } } } diff --git a/src/google/protobuf/util/json_util.h b/src/google/protobuf/util/json_util.h index 8dda70c3..53b1d1ba 100644 --- a/src/google/protobuf/util/json_util.h +++ b/src/google/protobuf/util/json_util.h @@ -176,12 +176,15 @@ namespace internal { class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink { public: explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream) - : stream_(stream) {} + : stream_(stream), buffer_size_(0) {} + ~ZeroCopyStreamByteSink(); virtual void Append(const char* bytes, size_t len); private: io::ZeroCopyOutputStream* stream_; + void* buffer_; + int buffer_size_; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyStreamByteSink); }; |