Skip to content

Commit 82db992

Browse files
jimmodpgeorge
authored andcommitted
lib/uzlib/lz77: Always use separate history buffer.
Because we only use the streaming source, this is just extra code size. Saves 64 bytes on PYBV11. Signed-off-by: Jim Mussared <[email protected]>
1 parent c4feb80 commit 82db992

File tree

1 file changed

+9
-31
lines changed

1 file changed

+9
-31
lines changed

lib/uzlib/lz77.c

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ static size_t uzlib_lz77_search_max_match(struct uzlib_lz77_state *state, const
5656

5757
// Compress the given chunk of data.
5858
void uzlib_lz77_compress(struct uzlib_lz77_state *state, const uint8_t *src, unsigned len) {
59-
bool use_src_as_history = false;
60-
if (state->hist_buf == NULL) {
61-
use_src_as_history = true;
62-
state->hist_buf = (uint8_t *)src;
63-
state->hist_len = 0;
64-
}
65-
6659
const uint8_t *top = src + len;
6760
while (src < top) {
6861
// Look for a match in the history window.
@@ -77,31 +70,16 @@ void uzlib_lz77_compress(struct uzlib_lz77_state *state, const uint8_t *src, uns
7770
zlib_match(&state->outbuf, match_offset, match_len);
7871
}
7972

80-
// Advance the history window.
81-
if (use_src_as_history) {
82-
// Use src as the history, so advance it.
83-
state->hist_len += match_len;
84-
if (state->hist_len > state->hist_max) {
85-
state->hist_buf += state->hist_len - state->hist_max;
86-
state->hist_len = state->hist_max;
87-
}
88-
src += match_len;
89-
} else {
90-
// Push the bytes into the history buffer.
91-
size_t mask = state->hist_max - 1;
92-
while (match_len--) {
93-
uint8_t b = *src++;
94-
state->hist_buf[(state->hist_start + state->hist_len) & mask] = b;
95-
if (state->hist_len == state->hist_max) {
96-
state->hist_start = (state->hist_start + 1) & mask;
97-
} else {
98-
++state->hist_len;
99-
}
73+
// Push the bytes into the history buffer.
74+
size_t mask = state->hist_max - 1;
75+
while (match_len--) {
76+
uint8_t b = *src++;
77+
state->hist_buf[(state->hist_start + state->hist_len) & mask] = b;
78+
if (state->hist_len == state->hist_max) {
79+
state->hist_start = (state->hist_start + 1) & mask;
80+
} else {
81+
++state->hist_len;
10082
}
10183
}
10284
}
103-
104-
if (use_src_as_history) {
105-
state->hist_buf = NULL;
106-
}
10785
}

0 commit comments

Comments
 (0)