Parsing large object tree keeps a separate copy of every key in every object
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 45/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- java
- Lĩnh vực
- backend, performance
Hướng nghiên cứu
Start with JSONTokener.nextString, nextSimpleValue, and nextValue, then trace how JSONObject parses and stores keys; the issue explains the repeated-string memory cost and the existing subclass workaround. Compare the opt-in configuration, default behavior, and key-map size cap options, then add unit tests for key sharing and ensure values are not shared. Done means the chosen API behaves as documented and default parsing remains unchanged.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
What we were doing
We run a web application that takes bulk inserts as JSON. A request body has an array of row objects that all use the same keys, for example {"rows": [{"name": "a", "count": 1, ...}, {"name": "b", "count": 2, ...}, ...]}. We parse it with new JSONObject(new JSONTokener(reader)). A request can have thousands of rows with dozens of keys each.
Result
JSONTokener.nextString creates a new String for every key it reads. A key that appears in 10,000 rows is stored 10,000 times. A heap dump from one of our production servers with 41 bulk-insert requests in progress showed 1.88 GB of parsed JSON, about 108 bytes per cell. Approximately half of that was repeated copies of the same String keys.
Expected result
Each distinct key should be stored once per parse. Since your README includes "low memory footprint" as a project goal, we're hoping you'd entertain a change.
Current workaround
We subclass JSONTokener and override nextString to return one shared instance per distinct key: KeySharingJSONTokener.java. This depends on internal details: keys are read through nextSimpleValue → nextString, but quoted values go through the same nextString. To keep values out of the shared map, we also override nextValue and handle quoted values there. A future change to these internals could quietly stop keys from being shared, or start sharing values.
Proposal
Add an opt-in option, JSONParserConfiguration.withShareKeys(boolean), that is off by default. When it's on, JSONObject swaps each parsed key for an earlier identical key from the same parse. The map of seen keys is held only for the length of one parse, and it is capped so input with a huge number of unique keys can't make it grow without limit. With the default configuration, no input would parse differently.
Alternatively, if you like this approach (I don't see much downside) it could be enabled by default.
If you'd rather not add a configuration option, a documented protected method on JSONTokener that is called only for keys would also make our subclass more reliable.
We're happy to send a PR, with unit tests and Java 6-compatible code, for whichever approach you prefer. Our code linked above is Apache 2.0 but we are happy to submit a contribution here as public domain.
- Ngôn ngữ chính
- Java
- Star
- 4.7k
- Fork
- 2.6k
- Merge trung bình
- 4 ngày 11 giờ
- Pull request đã merge (30 ngày)
- 7
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của stleary/JSON-java
-
JSONObject.toString does not use an Enum's `toString()` but its `name()`Có thể làm lại được @XIAYM-gh đã nhận 45 ngày trước và không có pull request nào đang mở. Đang mởFix before the next release
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Awaiting further input from submitter No changes at this time
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
-
`XML.unescape` rejects the character references `XML.escape` produces for control charactersĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
-
Active discussion
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
-
JSONObject.toString() throws StackOverflowError (not JSONException) on self-referential cyclesCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mởActive discussion
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 56/100
Tất cả issue của stleary/JSON-java
Issue tương tự
-
BC remittance upload: a claim MSP refused (S03) is saved with a blank status instead of refused (F)Đang mởtype: bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
carlos-emr/carlos#4933 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
apache/rocketmq-dashboard#6925 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Vakore/ZappierGames#147 ·
-
Post 1.11.2 to SpigotMCĐang mởspigotmc
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
Dans-Plugins/Wild-Pets#356 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Post 1.16.2 to SpigotMCĐang mởspigotmc
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
Dans-Plugins/Medieval-Roleplay-Engine#371 ·
Maintainer thường phản hồi trong vòng 1 ngày