Conversation
JSONDecoder.decoder(from:) parsed with `JSONParser(bytes: data.bytes)`. On Android, `Data.bytes` maps the backing ByteArray into a list of boxed UBytes and copies it into an Array, and `JSONParser(bytes:)` then rebuilds a Data and a String from that array. That is roughly 40 bytes of heap per byte of input before org.json sees a character, so decoding a few megabytes of JSON can exhaust a default Android heap. Add `JSONParser(data:)`, which builds the String straight from the ByteArray with the same UTF-8 fallback as `init(bytes:)`, and use it in JSONDecoder. Adds a test decoding multi-byte UTF-8 and a 20,000-element array. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Thank you for your pull request and welcome to the Skip community. We require contributors to sign our contributor license agreement (CLA), and we don't seem to have the user(s) @rnaud on file. In order for us to review and merge your code, for each noted user please add your GitHub username to Skip's .clabot file |
Author
|
CLA signed in skiptools/clabot-config#101 (with my employer's approval). I'll comment |
Author
|
recheck |
rnaud
marked this pull request as ready for review
September 25, 2026 11:53
Author
|
recheck |
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.
JSONDecoder.decoder(from:)parses withJSONParser(bytes: data.bytes). On Android that is expensive in memory:Data.bytesisArray(platformValue.map { $0.toUByte() }): themapproduces a list of boxedUBytes andArray(...)copies it again.JSONParser(bytes:)then rebuilds aDataand aStringfrom that array before handing it toorg.json.That comes to roughly 40 bytes of heap per byte of input before the tokenizer sees a character. We hit this in an app decoding a ~3.6 MB response: a heap dump showed ~5.8 M boxed
UBytes plus a ~4 M-slotObject[], and the process ran out of memory on a default 192 MB heap.Change
JSONParser.init(data:), which builds theStringdirectly from the data's backingByteArray(String(data:encoding:)), with the same UTF-8 fallback asinit(bytes:).JSONDecoder.decoder(from:)uses it. No behaviour change otherwise.TestJSON.testJSONDecodingMultiByteAndLargeInput, covering multi-byte UTF-8 and a 20,000-element array.With the change, a heap dump at the same point shows no boxed
UBytes. What remains is the parsed JSON itself.Not in this PR
JSONSerialization.jsonObject(with:)has the samedata.bytescall, but it uses the bytes to detect the encoding (BOM, UTF-16/32), so it needs a slightly larger change: detect from the first few bytes, then convert once. Happy to follow up with that if this approach looks right.Testing
swift test --filter SkipFoundationTests.TestJSON/(macOS): 5 passed.swift test --filter XCSkipTests(Robolectric, Java 25): 506 passed, 0 failed, 485 skipped.TestJSON5/5, including the new test.🤖 Generated with Claude Code