add pattern match to use smaller format

This commit is contained in:
UENISHI Kota 2013-03-16 23:31:14 +09:00
parent 7850ea6fd4
commit c8f29d1bef
2 changed files with 10 additions and 9 deletions

View File

@ -23,7 +23,7 @@
%% pack them all
-spec pack(msgpack:object(), option()) -> binary().
pack(I, _) when is_integer(I), I < 0 ->
pack(I, _) when is_integer(I) andalso I < 0 ->
pack_int(I);
pack(I, _) when is_integer(I) ->
pack_uint(I);
@ -39,7 +39,8 @@ pack(Bin, _) when is_binary(Bin) ->
pack_raw(Bin);
%% jiffy interface
pack({Map}, Opt = ?OPTION{interface=jiffy}) -> pack_map(Map, Opt);
pack({Map}, Opt = ?OPTION{interface=jiffy}) ->
pack_map(Map, Opt);
%% jsx interface
pack(Map, Opt = ?OPTION{interface=jsx}) when Map =:= [{}]->
@ -74,7 +75,7 @@ pack_int(N) ->
pack_uint(N) when N < 128 ->
<< 2#0:1, N:7 >>;
%% uint 8
pack_uint(N) when (N band 16#FF) ->
pack_uint(N) when (N band 16#FF) =:= N ->
<< 16#CC:8, N:8 >>;
%% uint 16
pack_uint(N) when (N band 16#FFFF) =:= N ->

View File

@ -35,19 +35,19 @@ benchmark0_test()->
Data=[test_data() || _ <- lists:seq(0, ?CNT)],
S=?debugTime(" serialize", msgpack:pack(Data, [jiffy])),
{ok, Data}=?debugTime("deserialize", msgpack:unpack(S, [jiffy])),
?debugFmt("for ~p KB test data(msgpack_jiffy).", [byte_size(S) div 1024]).
?debugFmt("for ~p KB test data(jiffy).", [byte_size(S) div 1024]).
benchmark1_test()->
Data=[test_data() || _ <- lists:seq(0, ?CNT)],
S=?debugTime(" serialize", msgpack:pack(Data, [jsx])),
{ok, Data}=?debugTime("deserialize", msgpack:unpack(S, [jsx])),
?debugFmt("for ~p KB test data(msgpack_jsx).", [byte_size(S) div 1024]).
?debugFmt("for ~p KB test data(jsx).", [byte_size(S) div 1024]).
benchmark2_test()->
Data=[test_data() || _ <- lists:seq(0, ?CNT)],
S=?debugTime(" serialize", msgpack_nif:pack(Data)),
{ok, Data}=?debugTime("deserialize", msgpack_nif:unpack(S)),
?debugFmt("for ~p KB test data(msgpack_nif).", [byte_size(S) div 1024]).
?debugFmt("for ~p KB test data(nif).", [byte_size(S) div 1024]).
benchmark3_test()->
Data=[test_data() || _ <- lists:seq(0, ?CNT)],
@ -93,7 +93,7 @@ multirunner(What, Pack, Unpack) ->
benchmark_p0_test_() ->
{timeout, 600,
?_assertEqual(ok,
multirunner("msgpack_jiffy",
multirunner("jiffy",
fun(Data) ->
msgpack:pack(Data, [jiffy])
end,
@ -104,7 +104,7 @@ benchmark_p0_test_() ->
benchmark_p1_test_() ->
{timeout, 600,
?_assertEqual(ok,
multirunner("msgpack_jsx",
multirunner("jsx",
fun(Data) ->
msgpack:pack(Data, [jsx])
end,
@ -115,7 +115,7 @@ benchmark_p1_test_() ->
benchmark_p2_test_() ->
{timeout, 600,
?_assertEqual(ok,
multirunner("msgpack_nif",
multirunner("nif",
fun(Data) ->
msgpack_nif:pack(Data)
end,