From 25b4099eeca34bd8d301b594440d56f20ad813f4 Mon Sep 17 00:00:00 2001 From: Kresten Krab Thorup Date: Mon, 23 Apr 2012 04:23:52 +0200 Subject: [PATCH] Only compress if compressed size is smaller --- src/hanoi_util.erl | 21 ++++++++++++++------- src/hanoi_writer.erl | 7 ++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/hanoi_util.erl b/src/hanoi_util.erl index 5bcbe95..ba8e4c0 100644 --- a/src/hanoi_util.erl +++ b/src/hanoi_util.erl @@ -49,23 +49,30 @@ estimate_node_size_increment(_KVList,Key,Value) -> -define(SNAPPY_COMPRESSION, 1). -define(GZIP_COMPRESSION, 2). -encode_index_node(Level, KVList, Compress) -> +encode_index_node(KVList, Compress) -> TermData = erlang:term_to_binary(KVList), case Compress of snappy -> {ok, Snappied} = snappy:compress(TermData), - CompressedData = [?SNAPPY_COMPRESSION|Snappied]; + if byte_size(Snappied) > byte_size(TermData) -> + OutData = [?NO_COMPRESSION|TermData]; + true -> + OutData = [?SNAPPY_COMPRESSION|Snappied] + end; gzip -> - CompressedData = [?GZIP_COMPRESSION|zlib:gzip(TermData)]; + GZipData = zlib:gzip(TermData); + if byte_size(GZipData) > byte_size(TermData) -> + OutData = [?NO_COMPRESSION|TermData]; + true -> + OutData = [?GZIP_COMPRESSION|GZipData] + end; _ -> - CompressedData = [?NO_COMPRESSION|TermData] + OutData = [?NO_COMPRESSION|TermData] end, - Size = erlang:iolist_size(CompressedData), - - {ok, Size+6, [ <<(Size+2):32/unsigned, Level:16/unsigned>> | CompressedData ] }. + {ok, OutData}. decode_index_node(Level, <>) -> diff --git a/src/hanoi_writer.erl b/src/hanoi_writer.erl index 131a146..50e6bea 100644 --- a/src/hanoi_writer.erl +++ b/src/hanoi_writer.erl @@ -187,8 +187,13 @@ add_record(Level, Key, Value, #state{ nodes=Nodes }=State) -> close_node(#state{nodes=[#node{ level=Level, members=NodeMembers }|RestNodes], compress=Compress} = State) -> OrderedMembers = lists:reverse(NodeMembers), - {ok, DataSize, Data} = hanoi_util:encode_index_node(Level, OrderedMembers, Compress), + {ok, BlockData} = hanoi_util:encode_index_node(OrderedMembers, Compress), NodePos = State#state.index_file_pos, + + BlockSize = byte_size(BlockData), + Data = [ <<(BlockSize+2):32/unsigned, Level:16/unsigned>> | BlockData ], + DataSize = BlockData + 6, + ok = file:write(State#state.index_file, Data), {FirstKey, _} = hd(OrderedMembers),