-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some users of Jiffy have experienced issues when decoding large JSON documents. Normally Jiffy expects smallish documents and returns any strings as sub-binaries. When dealing with large documents these sub-binary references can keep a large amount of RAM around unless the user goes through and applies `binary:copy/1` on every string returned from Jiffy. This however causes a large amount of CPU usage to do something that Jiffy could do as it builds the JSON structure. The `copy_strings` decoder option does exactly this. Instead of returning sub-binaries Jiffy now copies every string into a newly allocated binary. Users report that this fixes the memory issues while also not negatively affecting performance significantly.
- Loading branch information
Showing
5 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
% This file is part of Jiffy released under the MIT license. | ||
% See the LICENSE file for more information. | ||
|
||
-module(jiffy_17_copy_strings_tests). | ||
|
||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
|
||
check_binaries({Props}) when is_list(Props) -> | ||
lists:all(fun({Key, Value}) -> | ||
check_binaries(Key) andalso check_binaries(Value) | ||
end, Props); | ||
check_binaries(Values) when is_list(Values) -> | ||
lists:all(fun(Value) -> | ||
check_binaries(Value) | ||
end, Values); | ||
check_binaries(Bin) when is_binary(Bin) -> | ||
io:format("~s :: ~p ~p", [Bin, byte_size(Bin), binary:referenced_byte_size(Bin)]), | ||
byte_size(Bin) == binary:referenced_byte_size(Bin); | ||
check_binaries(Bin) -> | ||
true. | ||
|
||
|
||
copy_strings_test_() -> | ||
Opts = [copy_strings], | ||
Cases = [ | ||
<<"\"foo\"">>, | ||
<<"[\"bar\"]">>, | ||
<<"{\"foo\":\"bar\"}">>, | ||
<<"{\"foo\":[\"bar\"]}">> | ||
], | ||
{"Test copy_strings", lists:map(fun(Json) -> | ||
EJson = jiffy:decode(Json, Opts), | ||
?_assert(check_binaries(EJson)) | ||
end, Cases)}. |