Commit b4b0e304 authored by Feng Xiao's avatar Feng Xiao Committed by GitHub

Merge pull request #2355 from xfxyjwf/fixjson

Speed up JSON parsing.
parents 8387b88c bd158fc2
...@@ -49,22 +49,25 @@ namespace protobuf { ...@@ -49,22 +49,25 @@ namespace protobuf {
namespace util { namespace util {
namespace internal { namespace internal {
ZeroCopyStreamByteSink::~ZeroCopyStreamByteSink() {
stream_->BackUp(buffer_size_);
}
void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) { void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) {
while (len > 0) { while (true) {
void* buffer; if (len <= buffer_size_) {
int length; memcpy(buffer_, bytes, len);
if (!stream_->Next(&buffer, &length)) { buffer_ = static_cast<char*>(buffer_) + len;
// There isn't a way for ByteSink to report errors. buffer_size_ -= len;
return; return;
} }
if (len < length) { memcpy(buffer_, bytes, buffer_size_);
memcpy(buffer, bytes, len); bytes += buffer_size_;
stream_->BackUp(length - len); len -= buffer_size_;
break; if (!stream_->Next(&buffer_, &buffer_size_)) {
} else { // There isn't a way for ByteSink to report errors.
memcpy(buffer, bytes, length); buffer_size_ = 0;
bytes += length; return;
len -= length;
} }
} }
} }
......
...@@ -176,12 +176,15 @@ namespace internal { ...@@ -176,12 +176,15 @@ namespace internal {
class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink { class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink {
public: public:
explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream) explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream)
: stream_(stream) {} : stream_(stream), buffer_size_(0) {}
~ZeroCopyStreamByteSink();
virtual void Append(const char* bytes, size_t len); virtual void Append(const char* bytes, size_t len);
private: private:
io::ZeroCopyOutputStream* stream_; io::ZeroCopyOutputStream* stream_;
void* buffer_;
int buffer_size_;
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyStreamByteSink); GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyStreamByteSink);
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment