Commit b3e3f8c
Don't do characterwise-compares if not needed.
When attempting to locate a field number for a JSON document, some code
paths will find themselves needing to strip `[` and `]` from a String.
Right now they do this by grabbing `first` and `last` from the String
and then comparing the `Character` to a `String` containing the given
characters.
This is a fairly heavyweight operation: it invokes unicode
normalisation, among other things. This means that with specially
crafted field names it is possible to cause us to spend a surprisingly
long time normalizing strings unnecessarily.
Given that we're looking for UTF-8 characters, and we know these strings
will already be UTF-8, the easier fix is to just do the check on the
UTF-8 view. The result is a vastly shortened runtime, taking the
duration of a clusterfuzz test case (also submitted with this patch)
down from 60s to 186ms: a 300x speedup.
Note that this patch is not terribly _rigorous_: I haven't gone
searching for other places where we do direct String comparisons that
can be done on the UTF-8 view. That would likely be a productive line of
enquiry for performance enhancement in the JSON codepath at the very
least.1 parent 1d45734 commit b3e3f8c
File tree
2 files changed
+4
-2
lines changed- FuzzTesting/FailCases
- Sources/SwiftProtobuf
2 files changed
+4
-2
lines changedLines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1276 | 1276 | | |
1277 | 1277 | | |
1278 | 1278 | | |
1279 | | - | |
1280 | | - | |
| 1279 | + | |
| 1280 | + | |
1281 | 1281 | | |
1282 | 1282 | | |
1283 | 1283 | | |
| |||
0 commit comments