Optimize JSON escaping of ASCII strings
Summary: `escapeString` is very slow even when there's very little to nothing to escape. This diff adds a fast path to copy sequences of bytes that don't need escaping. It also optimizes appending escape sequences: `string::push_back` is slow because it has to do a capacity check for every character. Before: ``` ============================================================================ folly/test/JsonOtherTest.cpp relative time/iter iters/s ============================================================================ jsonSerialize 818.55ns 1.22M jsonSerializeWithNonAsciiEncoding 1.35us 738.06K jsonSerializeWithUtf8Validation 1.42us 705.60K jsonSerializeAsciiWithUtf8Validation 3.27us 306.06K parseSmallStringWithUtf 1.91us 522.38K parseNormalString 1.51us 660.27K parseBigString 384.44ns 2.60M toJson 480.54ns 2.08M ============================================================================ ``` After: ``` ============================================================================ folly/test/JsonOtherTest.cpp relative time/iter iters/s ============================================================================ jsonSerialize 781.69ns 1.28M jsonSerializeWithNonAsciiEncoding 847.68ns 1.18M jsonSerializeWithUtf8Validation 928.68ns 1.08M jsonSerializeAsciiWithUtf8Validation 199.85ns 5.00M parseSmallStringWithUtf 1.93us 518.39K parseNormalString 1.45us 689.11K parseBigString 378.66ns 2.64M toJson 446.38ns 2.24M ============================================================================ ``` All string escaping benchmarks are slightly faster, and ASCII-only with no escapes is 8x faster. Reviewed By: luciang, evilmucedin, yfeldblum Differential Revision: D4793233 fbshipit-source-id: c40d07708bd787799c8c00f9f23a417b862ee9ae
Showing
Please register or sign in to comment