Repository navigation
Replace regex number validation in JsonWriter with explicit logic - #3178
Open
eamonnmcmanus wants to merge 2 commits into
Open
eamonnmcmanus wants to merge 2 commits into
eamonnmcmanus wants to merge 2 commits into
Conversation
Replace `VALID_JSON_NUMBER_PATTERN.matcher(string).matches()` in `JsonWriter.value(Number)` with a zero-allocation `isValidJsonNumber` method implementing the same RFC 8259 section 6 number grammar. When JSON is parsed into a `JsonElement` hierarchy and later serialized back out, numeric values remain `LazilyParsedNumber` instances, which must be validated on every `JsonWriter.value(Number)` call. Replacing `java.util.regex.Matcher` avoids allocating a `Matcher` and its internal group arrays on every number and eliminates regex engine overhead.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace
VALID_JSON_NUMBER_PATTERN.matcher(string).matches()inJsonWriter.value(Number)with a zero-allocationisValidJsonNumber(String)method implementing the same RFC 8259 section 6 number grammar.In production profiling of Java services using Gson, we observed a very frequent use case of parsing JSON into a
JsonElementhierarchy (viaJsonParserorGson.fromJson(..., JsonElement.class)) and later serializingJsonElementvalues back out to JSON.When
JsonElementTypeAdapterparses a JSON number, it wraps the number string in aLazilyParsedNumberinside aJsonPrimitive. If thatJsonElement(or a subtree containing it) is subsequently written out viaJsonElementTypeAdapter.write, it callsJsonWriter.value(Number)with thatLazilyParsedNumber. Unlike standard JDK types (Integer,Long,BigDecimal,Double, etc.),LazilyParsedNumbercannot be included inalwaysCreatesValidJsonNumberbecausenew LazilyParsedNumber(String)is public andnew JsonPrimitive("...").getAsNumber()can wrap an arbitrary string. As a result, every number written from a parsedJsonElementtree (as well as anyNumberdeserialized with the defaultToNumberPolicy.LAZILY_PARSED_NUMBERand re-serialized, or any customNumbersubclass) hits the validation check inJsonWriter.value(Number).In production profiles,
VALID_JSON_NUMBER_PATTERN.matcher(string).matches()alone accounted for over half of the total CPU time ofJsonWriter.value(Number)(~3.6% of all CPU time spent in Gson and its direct JDK calls, with >95% of those calls coming fromJsonElementTypeAdapter.write) and ~5% of Gson's short-lived object allocations due to instantiating ajava.util.regex.Matcherand its internalint[]group arrays on every number.Because the RFC 8259 number grammar (
-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?) is regular, deterministic, and non-backtracking, replacing thePatternwith a small character-by-character scan inisValidJsonNumber(String)eliminates allMatcherallocations and regex engine dispatch while preserving identical validation behavior.(We already have a similar explicit DFA in
JsonReader.peekNumberbut the context is sufficiently different that it is probably not worth trying to share code.)