Skip to content

Commit

Permalink
Improves performance of json_stream_parser.cc by factor 1000
Browse files Browse the repository at this point in the history
JsonStreamParser::GetNextTokenType() uses HasPrefixString a lot on StringPiece as input. For each call two std::strings are constructed, compared and destroyed. Parsing of json-files with 50-60 MB in debug mode takes minutes.
  • Loading branch information
wsw2016 authored and acozzette committed Feb 19, 2020
1 parent 1370c73 commit b96241b
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/google/protobuf/stubs/strutil.h
Expand Up @@ -35,6 +35,7 @@

#include <stdlib.h>
#include <vector>
#include <cstring>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/stringpiece.h>

Expand Down Expand Up @@ -117,6 +118,12 @@ inline bool HasPrefixString(const string& str,
return str.size() >= prefix.size() &&
str.compare(0, prefix.size(), prefix) == 0;
}

inline bool HasPrefixString(StringPiece str,
StringPiece prefix) {
return str.size() >= prefix.size() &&
memcmp(str.data(), prefix.data(), prefix.size()) == 0;
}

inline string StripPrefixString(const string& str, const string& prefix) {
if (HasPrefixString(str, prefix)) {
Expand Down

0 comments on commit b96241b

Please sign in to comment.