Reduce encoder memory allocations#301
Merged
Merged
Conversation
Flush the buffer into iolist segments with a chunk size that doubles (up to
64KB). Large documents should now have O(log n) allocation.
We can use tprof to check allocation with OTP 27+. Example of memory profiling
before and after with `canada.json` from bench data:
```
{ok, B} = file:read_file("data/canada.json").
T = jiffy:decode(B, [return_maps]).
tprof:profile(jiffy, encode, [T], #{type => call_memory}).
```
Before
```
****** Process <0.114.0> -- 100.00% of total ***
FUNCTION CALLS WORDS PER CALL [ %]
lists:reverse/1 1 4 4.00 [ 0.03]
lists:reverse/2 1 2062 2062.00 [14.21]
jiffy:nif_encode_init/2 1 12441 12441.00 [85.76]
14507 [100.0]
```
After
```
****** Process <0.101.0> -- 100.00% of total ***
FUNCTION CALLS WORDS PER CALL [ %]
lists:reverse/1 1 4 4.00 [ 0.29]
lists:reverse/2 1 68 68.00 [ 4.88]
jiffy:nif_encode_init/2 1 1321 1321.00 [94.83]
```
This shows a good speed improvment on some benchmarks especially ones with
larger objects. Saw about ~6% speedup on on object-heavy benchmarks
(citm, pokedex, github):. Larger ones like "canada" showed about a ~1.25x
speedup.
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.
Flush the buffer into iolist segments with a chunk size that doubles (up to 64KB). Large documents should now have
O(log n)allocations.We can use tprof to check allocation with OTP 27+. Example of memory profiling before and after with
canada.jsonfrom bench data:Before
After
This shows a good speed improvment on some benchmarks especially ones with larger objects. Saw about ~6% speedup on on object-heavy benchmarks (citm, pokedex, github):. Larger ones like "canada" showed about a ~1.25x speedup.