Draft
Conversation
Before this change "\uD834\uDD1E" would result in "�DD1E" but should have resulted in "��", due to both being unmerged surrogates.
Previously time spent for parsing 100MB JSON input (600ms) took 60ms in a number of unnecessary bound checks: CALL runtime.panicBounds(SB), now reduced to 20ms by moving explicit bound checks before indizes, reusing indexed slots and merging manual out of loop increments.
Reduced time taken in unescapeInPlace by 30ms (from 5.75% to 3.41%)
… fast but more correct
This commit replaces the need for hashing json object keys at parse time
by replacing the previously used map[string]any with the new obj struct:
| Benchmark | LibJson B/op | EncodingJson B/op | LibJson x Less Memory | LibJson Allocs | EncodingJson Allocs | LibJson x Fewer Allocs |
| --------- | ------------ | ----------------- | --------------------- | -------------- | ------------------- | ---------------------- |
| Naive | 29,632,671 | 42,744,497 | 1.44x | 450,023 | 1,050,031 | 2.33x |
| Escaped | 22,471,438 | 37,544,412 | 1.67x | 350,023 | 1,100,030 | 3.14x |
| Hard | 121,444,318 | 173,944,500 | 1.43x | 1,400,023 | 3,000,032 | 2.14x |
These changes result in a ~10-15% speedup and allows libjson to hit the
~2x faster than encoding/json milestone. For instance with 1MB, 5MB, 10MB
and 100MB sized files filled with:
{
"id": 12345,
"name": "very_long_string_with_escapes_and_unicode_abcdefghijklmnopqrstuvwxyz_0123456789",
"description": "This string contains\nmultiple\nlines\nand \"quotes\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"",
"nested": {
"level1": {
"level2": {
"level3": {
"level4": {
"array": [
"short",
"string_with_escape\\n",
"another\\tvalue",
"unicode\u2603",
"escaped_quote_\"_and_backslash_\\",
11234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,1234567890,234567890,
-1.2345e67,
3.1415926535897932384626433832795028841971,
True,
False,
None,
"\u0041\u0042\u0043\u00A9\u20AC\u0041\u0042\u0043\u00A9\u20AC\u0041\u0042\u0043\u00A9\u20AC\u0041\u0042\u0043\u00A9\u20AC\u0041\u0042\u0043\u00A9\u20AC\u0041\u0042\u0043\u00A9\u20AC\u0041\u0042\u0043\u00A9\u20AC\u0041\u0042\u0043\u00A9\u20AC\u0041\u0042\u0043\u00A9\u20AC\u0041\u0042\u0043\u00A9\u20AC\u0041\u0042\u0043\u00A9\u20AC",
"mix\\n\\t\\r\\\\\\\"end"
]
}
}
}
}
}
}
libjson now outperforms encoding/json:
$ cd ./benchmarks
$ ./bench.sh | rg "faster"
1.72 ± 0.15 times faster than ./test -s -libjson=false ./1MB.json
1.89 ± 0.11 times faster than ./test -s -libjson=false ./5MB.json
1.90 ± 0.06 times faster than ./test -s -libjson=false ./10MB.json
1.95 ± 0.05 times faster than ./test -s -libjson=false ./100MB.json
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.
This pr aims to rework the parser to remove intermediate values, omittable allocations and in general be faster due to less recursion and less gc pressure.
Goals:
Parser.parsebased on token type (this should also account for feat: recursive json object test #2, since the stack overflow for large recursion is now replaced by OOM 😹)io.ReadAllwith asyscall.Mmapin a newlibjson.FromFilefunctionadd(slowed things down due to multiple extra branches)t_string_escapesto only pay the cost of callingunescapeInPlacefor strings containing escapesPre pr Benchmarks:
encoding/json
nogc
gc
libjson
nogc
gc