Skip to content

Commit

Permalink
Eliminate some repeated code
Browse files Browse the repository at this point in the history
  • Loading branch information
tsee committed Oct 29, 2012
1 parent b9c1718 commit 3de80cd
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions Perl/Encoder/srl_encoder.c
Expand Up @@ -460,32 +460,32 @@ srl_dump_data_structure(pTHX_ srl_encoder_t *enc, SV *src)
} }
else { else {
ptrdiff_t sereal_header_len; ptrdiff_t sereal_header_len;
STRLEN uncompressed_body_length;


/* Alas, have to write entire packet first since the header length /* Alas, have to write entire packet first since the header length
* will determine offsets. */ * will determine offsets. */
srl_write_header(aTHX_ enc); srl_write_header(aTHX_ enc);
sereal_header_len = enc->pos - enc->buf_start; sereal_header_len = enc->pos - enc->buf_start;
srl_dump_sv(aTHX_ enc, src); srl_dump_sv(aTHX_ enc, src);
srl_fixup_weakrefs(aTHX_ enc); srl_fixup_weakrefs(aTHX_ enc);
uncompressed_body_length = BUF_POS_OFS(enc) - sereal_header_len;


/* Don't bother with snappy compression at all if we have less than $threshold bytes of payload */ /* Don't bother with snappy compression at all if we have less than $threshold bytes of payload */
if (enc->snappy_threshold > 0 if (enc->snappy_threshold > 0
&& enc->pos - enc->buf_start - sereal_header_len < enc->snappy_threshold) && uncompressed_body_length < (STRLEN)enc->snappy_threshold)
{ {
/* sizeof(const char *) includes a count ofr \0 */ /* sizeof(const char *) includes a count of \0 */
char *flags_and_version_byte = enc->buf_start + sizeof(SRL_MAGIC_STRING) - 1; char *flags_and_version_byte = enc->buf_start + sizeof(SRL_MAGIC_STRING) - 1;
/* disable snappy flag in header */ /* disable snappy flag in header */
*flags_and_version_byte = SRL_PROTOCOL_ENCODING_RAW | *flags_and_version_byte = SRL_PROTOCOL_ENCODING_RAW |
(*flags_and_version_byte & SRL_PROTOCOL_VERSION_MASK); (*flags_and_version_byte & SRL_PROTOCOL_VERSION_MASK);
} }
else { else {
STRLEN uncompressed_length;
char *old_buf; char *old_buf;
uint32_t dest_len; uint32_t dest_len;


/* Get uncompressed payload and total packet output (after compression) lengths */ /* Get uncompressed payload and total packet output (after compression) lengths */
uncompressed_length = BUF_POS_OFS(enc) - sereal_header_len; dest_len = csnappy_max_compressed_length(uncompressed_body_length) + sereal_header_len + 1;
dest_len = csnappy_max_compressed_length(uncompressed_length) + sereal_header_len + 1;


/* Lazy working buffer alloc */ /* Lazy working buffer alloc */
if (expect_false( enc->snappy_workmem == NULL )) { if (expect_false( enc->snappy_workmem == NULL )) {
Expand All @@ -510,10 +510,10 @@ srl_dump_data_structure(pTHX_ srl_encoder_t *enc, SV *src)
enc->pos += sereal_header_len; enc->pos += sereal_header_len;


/* /*
* fprintf(stderr, "'%u' %u %u\n", enc->pos - enc->buf_start, uncompressed_length, (uncompressed_length+sereal_header_len)); * fprintf(stderr, "'%u' %u %u\n", enc->pos - enc->buf_start, uncompressed_body_length, (uncompressed_body_length+sereal_header_len));
* fprintf(stdout, "%7s!%1s\n", old_buf, old_buf+6); * fprintf(stdout, "%7s!%1s\n", old_buf, old_buf+6);
*/ */
csnappy_compress(old_buf+sereal_header_len, (uint32_t)uncompressed_length, enc->pos, &dest_len, csnappy_compress(old_buf+sereal_header_len, (uint32_t)uncompressed_body_length, enc->pos, &dest_len,
enc->snappy_workmem, CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO); enc->snappy_workmem, CSNAPPY_WORKMEM_BYTES_POWER_OF_TWO);
/* fprintf(stderr, "%u, %u %u %u\n", dest_len, enc->pos[0], enc->pos[1], enc->pos[2]); */ /* fprintf(stderr, "%u, %u %u %u\n", dest_len, enc->pos[0], enc->pos[1], enc->pos[2]); */
Safefree(old_buf); Safefree(old_buf);
Expand Down

0 comments on commit 3de80cd

Please sign in to comment.