Skip to content

Conversation

@svenmuennich
Copy link
Member

This PR improves how JSON handles its content. Previously the content was unwrapped when initializing a new JSON value. This was done to support nesting values of type JSON into e.g. dictionaries. However, when unwrapping a nested JSON value its underlying raw value (i.e. object) was extracted, loosing all information previously gather about its content. This approach only improved performance when working with the raw underlying values of JSON, e.g. when using object, arrayObject or dictionaryObject, which should be the exception.

With this PR, the Content is strongly typed to only store raw primitives or array/dictionaries of Content values. This requires resolving the whole JSON upon initializing a new JSON value once. All subsequent accesses to any of its values (e.g. via subscript) are super cheap now, as we can just re-use the previously extracted content value. Resolving the JSON upon initialization is also cheaper now when working with nested JSON values, as we can just re-use their content as well.

To see the performance improvements this PR brings you can copy the new performance test testLongNestedArrayPerformance to a checkout of master to get a baseline timing value for comparison.

Note on the code style: I tried to stay as close as possible to the code style of the upstream repository and only apply minimal fixes.

@florianrhein florianrhein self-assigned this Jul 26, 2022
@@ -139,9 +150,21 @@ public struct JSON {
if let data = jsonString.data(using: .utf8) {
self.init(data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.init(data)
self.init(data)

I know we should stick to the style of the author, but this indentation is most likely accidentally, as other ifs are also indented with four spaces

@svenmuennich svenmuennich merged commit 99b6af7 into master Jul 26, 2022
@svenmuennich svenmuennich deleted the svenmuennich/improve-performance branch July 26, 2022 09:23
svenmuennich added a commit that referenced this pull request Jul 26, 2022
florianrhein pushed a commit that referenced this pull request Jul 27, 2022
svenmuennich added a commit that referenced this pull request Jul 28, 2022
felixbrucker pushed a commit that referenced this pull request Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants