Replace JSX with Thoas for JSON operations

Thoas is more efficient both in terms of encoding
time and peak memory footprint.

In the process we have discovered an issue:
https://github.com/lpil/thoas/issues/15

Pair: @pjk25
This commit is contained in:
Michael Klishin 2022-07-25 19:11:56 +04:00
parent 838499c326
commit 9c99f76579
No known key found for this signature in database
GPG Key ID: 8ADA141E1AD87C94
7 changed files with 26 additions and 23 deletions

View File

@ -127,9 +127,9 @@ erlang_package.git_package(
)
erlang_package.hex_package(
name = "jsx",
version = "3.1.0",
sha256 = "0c5cc8fdc11b53cc25cf65ac6705ad39e54ecc56d1c22e4adb8f5a53fb9427f3",
name = "thoas",
version = "0.2.0",
sha256 = "630aaee57fb3fde201578e787259e15e788a27733d49de8dcce1354db1885b8d",
)
erlang_package.git_package(
@ -272,7 +272,6 @@ use_repo(
"gun",
"inet_tcp_proxy_dist",
"jose",
"jsx",
"meck",
"observer_cli",
"osiris",
@ -287,6 +286,8 @@ use_repo(
"syslog",
"sysmon_handler",
"systemd",
"thoas",
"trust_store_http",
)
rbe = use_extension(

View File

@ -10,7 +10,7 @@ APP_MODULE = "rabbit_prelaunch_app"
RUNTIME_DEPS = [
"@cuttlefish//:erlang_app",
"@jsx//:erlang_app",
"@thoas//:erlang_app",
]
DEPS = [

View File

@ -18,7 +18,7 @@ format(
rabbit_logger_fmt_helpers:format_level(Level, Config)),
FormattedMeta = format_meta(Meta, Config),
%% We need to call `unicode:characters_to_binary()' here and several other
%% places because the JSON encoding library will format a string as a list of
%% places because JSON encoder will format a string as a list of
%% integers (we don't blame it for that, it makes sense).
FormattedMsg = unicode:characters_to_binary(
rabbit_logger_fmt_helpers:format_msg(Msg, Meta, Config)),

View File

@ -1383,7 +1383,9 @@ log_and_return_json_object(Context, Metadata, DecodeOpts) ->
Content,
"^.+\"" ++ RandomMsg ++ "\".+$",
ReOpts),
Term = rabbit_json:decode(Line, [{labels, attempt_atom} | DecodeOpts]),
Term0 = rabbit_json:decode(Line, DecodeOpts),
Term = rabbit_data_coercion:atomize_keys(Term0),
{RandomMsg, Term}.

View File

@ -52,7 +52,7 @@ genrule(
)
RUNTIME_DEPS = [
"@jsx//:erlang_app",
"@thoas//:erlang_app",
"@recon//:erlang_app",
"@credentials_obfuscation//:erlang_app",
]

View File

@ -13,24 +13,24 @@
-define(DEFAULT_DECODE_OPTIONS, [return_maps]).
-spec decode(jsx:json_text()) -> jsx:json_term().
-spec decode(iodata()) -> thoas:json_term().
decode(JSON) ->
decode(JSON, ?DEFAULT_DECODE_OPTIONS).
-spec decode(jsx:json_text(), jsx_to_term:config()) -> jsx:json_term().
-spec decode(iodata(), thoas:decode_options()) -> thoas:json_term().
decode(JSON, Opts) ->
jsx:decode(JSON, Opts).
thoas:decode(JSON, Opts).
-spec try_decode(jsx:json_text()) -> {ok, jsx:json_term()} |
-spec try_decode(iodata()) -> {ok, thoas:json_term()} |
{error, Reason :: term()}.
try_decode(JSON) ->
try_decode(JSON, ?DEFAULT_DECODE_OPTIONS).
-spec try_decode(jsx:json_text(), jsx_to_term:config()) ->
{ok, jsx:json_term()} | {error, Reason :: term()}.
-spec try_decode(iodata(), thoas:decode_options()) ->
{ok, thoas:json_term()} | {error, Reason :: term()}.
try_decode(JSON, Opts) ->
try
{ok, decode(JSON, Opts)}
@ -38,21 +38,21 @@ try_decode(JSON, Opts) ->
{error, Reason}
end.
-spec encode(jsx:json_term()) -> jsx:json_text().
-spec encode(thoas:json_term()) -> iodata().
encode(Term) ->
encode(Term, []).
-spec encode(jsx:json_term(), jsx_to_json:config()) -> jsx:json_text().
-spec encode(thoas:json_term(), thoas:encode_options()) -> iodata().
encode(Term, Opts) ->
jsx:encode(fixup_terms(Term), Opts).
thoas:encode(fixup_terms(Term), Opts).
-spec try_encode(jsx:json_term()) -> {ok, jsx:json_text()} |
-spec try_encode(thoas:json_term()) -> {ok, iodata()} |
{error, Reason :: term()}.
try_encode(Term) ->
try_encode(Term, []).
-spec try_encode(jsx:json_term(), jsx_to_term:config()) ->
{ok, jsx:json_text()} | {error, Reason :: term()}.
-spec try_encode(thoas:json_term(), thoas:decode_options()) ->
{ok, iodata()} | {error, Reason :: term()}.
try_encode(Term, Opts) ->
try
{ok, encode(Term, Opts)}

View File

@ -135,9 +135,9 @@ erlang_app(
)
hex_pm_erlang_app(
name = "jsx",
version = "3.1.0",
sha256 = "0c5cc8fdc11b53cc25cf65ac6705ad39e54ecc56d1c22e4adb8f5a53fb9427f3",
name = "thoas",
version = "0.2.0",
sha256 = "630aaee57fb3fde201578e787259e15e788a27733d49de8dcce1354db1885b8d",
)
github_erlang_app(