question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optimize `JsonNodeDeserialization` wrt recursion

See original GitHub issue

(note: cleaved off of #2816, used to be bundled)

Current implementation JsonNodeDeserialization is expensive for deeply nested Object and Array values as it uses recursion: so for each small additional nesting level – for arrays, 2 bytes to encode [ and ] – a new stack frame gets created. In practical terms this means that it is possible to exhaust JVM heap usage with document that has nesting in order of ten thousand(s) levels, depending on settings.

It should be possible to replace basic recursion, however, with iteration, to at least significantly reduce amplification: to prevent cheapest potential DoS concerns.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
yawkatcommented, Apr 4, 2022

I can’t say that, it’s tatu’s decision.

However I’m not convinced a depth limit would help with “long texts take a long time to parse” completely. In general you can also allocate a lot of objects without very deep json, e.g. [[[... 4000 levels...]],[[... 4000 levels...]],... thousands of repetitions...]. This will not run into the depth limits, but will still be fairly slow to parse (simply because there’s lots of tokens).

There is one problem that is unique to deeply nested json in particular (as opposed to other ways of getting many tokens): This line limits the expansion of the ContainerStack to max 4000 elements, which means that you can get quite large allocations for every 4000 tokens. However there are always at most two of these arrays alive at a time, so it should not lead to overly large memory use, so it should not be a security risk. It does however reduce perf of parsing of that particular document.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialize recursive JSON object [closed] - Stack Overflow
I am trying to build a wpf application using mvvm pattern, would like to deserialize the json file recursively.
Read more >
Deeply Nested JSON Deserialization Using Recursion and ...
Therefore, I propose an alternative for the use of ExpandoObject to hold inner dictionaries by making use of Method Recursion. Using the Code....
Read more >
Allow increasing recursion limit · Issue #334 · serde-rs/json
I am parsing/serializing pretty large JSON files and I regularly encounter RecursionLimitExceeded . I need a way to instantiate a Serializer ...
Read more >
Using JSON.stringify() Replacer Function To Recursively ...
Ben Nadel demonstrates how the JSON.stringify() "replacer" function can be used to recursively serialize and sanitize log data.
Read more >
How to Flatten Deeply Nested JSON Objects in Non ...
It is dangerous to flatten deeply nested JSON objects with a recursive python solution. Because the python interpreter limits the depth of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found