Browse Source

Improves performance of json_stream_parser.cc by factor 1000

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.
wsw2016 5 years ago
parent
commit
b96241b1b7
1 changed files with 7 additions and 0 deletions
  1. 7 0
      src/google/protobuf/stubs/strutil.h

+ 7 - 0
src/google/protobuf/stubs/strutil.h

@@ -35,6 +35,7 @@
 
 
 #include <stdlib.h>
 #include <stdlib.h>
 #include <vector>
 #include <vector>
+#include <cstring>
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/stringpiece.h>
 #include <google/protobuf/stubs/stringpiece.h>
 
 
@@ -117,6 +118,12 @@ inline bool HasPrefixString(const string& str,
   return str.size() >= prefix.size() &&
   return str.size() >= prefix.size() &&
          str.compare(0, prefix.size(), prefix) == 0;
          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) {
 inline string StripPrefixString(const string& str, const string& prefix) {
   if (HasPrefixString(str, prefix)) {
   if (HasPrefixString(str, prefix)) {