There are many ways to implement Z_RLE. The simplest approach that does not impact performance and makes use of the existing code, is the following:
s->strategy == Z_RLE
in the functions
deflate_fast()
and deflate_slow()
,
longest_match()
only if
s->strstart - hash_head == 1
.
This means that the match is one step behind.
longest_match()
, but
try the match only once. There are two possibilities to do this:
FASTEST
version of longest_match()
.
#ifdef FASTEST
guard,
renamed to longest_match_fast()
, and invoked
if (s->strategy == Z_RLE && s->strstart - hash_head == 1)
".
longest_match()
is necessary;
the implementation may benefit from the speed gained by an ASM version;
longest_match()
(including the ASM versions) must be
slightly modified
[not implemented for the 1.2.0 release candidate]:
longest_match()
to attempt a single
match, it is necessary to set chain_length
to 1.
This may be achieved, for instance, in lm_init()
and
deflateParams()
:
- s->max_chain_length = configuration_table[s->level].max_chain; + s->max_chain_length = (s->strategy != Z_RLE) ? + configuration_table[s->level].max_chain : 1; |
longest_match()
,
which must be fixed in order to avoid having UINT_MAX
loops!
/* Do not waste too much time if we already have a good match: */ if (s->prev_length >= s->good_match) { - chain_length >>= 2; + chain_length = (chain_length >> 2) + 1; } |
longest_match()
.
I wish to mention that I intended to implement a faster version of Z_HUFFMAN_ONLY and Z_RLE.
It is quite likely that the user might not want to change the strategy
parameter from Z_HUFFMAN_ONLY or Z_RLE while compressing one file
(i.e. the user might not want to call deflateParams()
).
This means that maintaining s->prev
and s->head
in the deflate_state
would be unnecessary.
In this case, the user might notify deflate, which would run a specialized
function that completely avoids the Ziv-Lempel searches, and simply sends
literals or run-lengths instead, to _tr_tally
.
Also note that, since s->prev
and s->head
are
not used, they don't need to occupy memory.
Unfortunately, the lack of time prevented me from doing this. It might not be
such a big problem, however, because these particular memory and time savings
don't seem to matter nowadays; apparently, even the palmtops are running Java
with java.util.zip
inside, without problems.