Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize encoding performance in Erlang #150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ Data Format
[true, 1.0] -> [true, 1.0] -> [true, 1.0]
{[]} -> {} -> {[]}
{[{foo, bar}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
{[{123, bar}]} -> {"123": "bar"} -> {[{<<"123">>, <<"bar">>}]}
{[{<<"foo">>, <<"bar">>}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
#{<<"foo">> => <<"bar">>} -> {"foo": "bar"} -> #{<<"foo">> => <<"bar">>}
#{123 => <<"bar">>} -> {"123": "bar"} -> #{<<"123">> => <<"bar">>}

N.B. The last entry in this table is only valid for VM's that support
N.B. The last two entries in this table are only valid for VM's that support
the `maps` data type (i.e., 17.0 and newer) and client code must pass
the `return_maps` option to `jiffy:decode/2`.

Expand Down
21 changes: 18 additions & 3 deletions c_src/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,24 @@ encode_iter(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
ret = enc_obj_error(e, "invalid_object_member_arity", item);
goto done;
}
if(!enc_string(e, tuple[0])) {
ret = enc_obj_error(e, "invalid_object_member_key", tuple[0]);
goto done;
if(enif_get_int64(env, tuple[0], &lval)) {
if (!enc_char(e, '"')) {
ret = enc_error(e, "internal_error");
goto done;
}
if(!enc_long(e, lval)) {
ret = enc_obj_error(e, "invalid_object_member_key", tuple[0]);
goto done;
}
if (!enc_char(e, '"')) {
ret = enc_error(e, "internal_error");
goto done;
}
} else {
if(!enc_string(e, tuple[0])) {
ret = enc_obj_error(e, "invalid_object_member_key", tuple[0]);
goto done;
}
}
if(!enc_colon(e)) {
ret = enc_error(e, "internal_error");
Expand Down
13 changes: 12 additions & 1 deletion test/jiffy_07_compound_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@


compound_success_test_() ->
[gen(ok, Case) || Case <- cases(ok)].
[gen(ok, Case) || Case <- cases(ok)] ++
[gen(special_encoding, Case) || Case <- cases(special_encoding)].


compound_failure_test_() ->
Expand All @@ -24,6 +25,11 @@ gen(ok, {J1, E, J2}) ->
{"Encode", ?_assertEqual(J2, enc(E))}
]};

gen(special_encoding, {J, E}) ->
{msg("~s", [J]), [
{"Encode", ?_assertEqual(J, enc(E))}
]};

gen(error, J) ->
{msg("Error: ~s", [J]), [
?_assertThrow({error, _}, dec(J))
Expand Down Expand Up @@ -53,6 +59,11 @@ cases(ok) ->
}
];

cases(special_encoding) ->
[
{<<"{\"123\":\"foo\"}">>, {[{123, <<"foo">>}]}}
];

cases(error) ->
[
<<"[{}">>,
Expand Down
3 changes: 0 additions & 3 deletions test/jiffy_12_error_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,10 @@ enc_invalid_object_member_key_test_() ->
E1 = {1, true},
{"invalid_object_member_key", [
{"Bad string", enc_error(Type, <<143>>, {[{<<143>>, true}]})},
{"Basic", enc_error(Type, 1, {[{1, true}]})},
{"Basic", enc_error(Type, [1], {[{[1], true}]})},
{"Basic", enc_error(Type, {[{foo,bar}]}, {[{{[{foo,bar}]}, true}]})},
{"Second", enc_error(Type, 1, {[{bar, baz}, E1]})},
{"Nested", enc_error(Type, 1, {[{bar,{[E1]}}]})},
{"Nested", enc_error(Type, 1, {[{bar,{[{baz, 1}, E1]}}]})},
{"In List", enc_error(Type, 1, [{[E1]}])},
{"In List", enc_error(Type, 1, [{[{bang, true}, E1]}])}
]}.

Expand Down