Repository navigation
Share repeated object keys within a parse (#1091) - #1094
Open
labkey-jeckels wants to merge 1 commit into
Open
labkey-jeckels wants to merge 1 commit into
labkey-jeckels wants to merge 1 commit into
Conversation
|
Owner
|
What problem does this code solve? Risks
Neither of these are dealbreakers Changes to the Existing Behavior Changes to the API Will this require a new release? Should the documentation be updated? Unit Tests Refactoring Review status Starting 3-day comment window |
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.



Fixes #1091.
When a document repeats the same keys across many objects, such as an array of rows, each parsed
JSONObjectholds its own copy of every keyString. With this change,JSONObjectreuses the first instance of each key read by the sameJSONTokener. As discussed in the issue, it is on by default and controlled by a newJSONParserConfiguration.withMaxSharedKeys(int):1000(default): share up to the first 1000 distinct keys per parse0: no sharing, the behavior before this change-1(UNLIMITED_SHARED_KEYS): no limitThe limit bounds the extra memory for input with many unique keys. The map is held by the tokener and released with it.
Why the first N keys
Once the limit is reached, new keys are not added, and keys already in the map stay shared. I considered two alternatives:
Keeping the first N shares the most in the row case and is the simplest to reason about. Input that starts with many unique keys can raise the limit or use
-1.Inputs that behave differently
None. With any setting, every input parses to the same values, types, and
toString()output as before. The only observable difference isStringidentity: with sharing on, equal keys in different objects from the same parse are the same instance. Values are never shared.Measurements (JDK 17, retained heap after GC)
withMaxSharedKeys(0)New tests in
JSONParserConfigurationTestcover the setting; sharing across rows, nested objects, and arrays; values staying unshared; the limit; unlimited; separate parses; unchanged output; and strict and lenient syntax. All tests pass, including with thetest-strict-modeprofile.I dedicate this contribution to the public domain, consistent with the project's license.