Improve speed of gzprintf() in transparent mode.
This commit is contained in:
2
gzguts.h
2
gzguts.h
@@ -170,7 +170,7 @@ typedef struct {
|
|||||||
char *path; /* path or fd for error messages */
|
char *path; /* path or fd for error messages */
|
||||||
unsigned size; /* buffer size, zero if not allocated yet */
|
unsigned size; /* buffer size, zero if not allocated yet */
|
||||||
unsigned want; /* requested buffer size, default is GZBUFSIZE */
|
unsigned want; /* requested buffer size, default is GZBUFSIZE */
|
||||||
unsigned char *in; /* input buffer */
|
unsigned char *in; /* input buffer (double-sized when writing) */
|
||||||
unsigned char *out; /* output buffer (double-sized when reading) */
|
unsigned char *out; /* output buffer (double-sized when reading) */
|
||||||
int direct; /* 0 if processing gzip, 1 if transparent */
|
int direct; /* 0 if processing gzip, 1 if transparent */
|
||||||
/* just for reading */
|
/* just for reading */
|
||||||
|
|||||||
2
gzlib.c
2
gzlib.c
@@ -331,6 +331,8 @@ int ZEXPORT gzbuffer(file, size)
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* check and set requested size */
|
/* check and set requested size */
|
||||||
|
if ((size << 1) < size)
|
||||||
|
return -1; /* need to be able to double it */
|
||||||
if (size < 2)
|
if (size < 2)
|
||||||
size = 2; /* need two bytes to check magic header */
|
size = 2; /* need two bytes to check magic header */
|
||||||
state->want = size;
|
state->want = size;
|
||||||
|
|||||||
110
gzwrite.c
110
gzwrite.c
@@ -18,8 +18,8 @@ local int gz_init(state)
|
|||||||
int ret;
|
int ret;
|
||||||
z_streamp strm = &(state->strm);
|
z_streamp strm = &(state->strm);
|
||||||
|
|
||||||
/* allocate input buffer */
|
/* allocate input buffer (double size for gzprintf) */
|
||||||
state->in = (unsigned char *)malloc(state->want);
|
state->in = (unsigned char *)malloc(state->want << 1);
|
||||||
if (state->in == NULL) {
|
if (state->in == NULL) {
|
||||||
gz_error(state, Z_MEM_ERROR, "out of memory");
|
gz_error(state, Z_MEM_ERROR, "out of memory");
|
||||||
return -1;
|
return -1;
|
||||||
@@ -309,7 +309,8 @@ int ZEXPORT gzputs(file, str)
|
|||||||
/* -- see zlib.h -- */
|
/* -- see zlib.h -- */
|
||||||
int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
|
int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
|
||||||
{
|
{
|
||||||
int size, len;
|
unsigned len, left;
|
||||||
|
char *next;
|
||||||
gz_statep state;
|
gz_statep state;
|
||||||
z_streamp strm;
|
z_streamp strm;
|
||||||
|
|
||||||
@@ -334,39 +335,47 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* consume whatever's left in the input buffer */
|
/* do the printf() into the input buffer, put length in len -- the input
|
||||||
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
|
buffer is double-sized just for this function, so there is guaranteed to
|
||||||
return 0;
|
be state->size bytes available after the current contents */
|
||||||
|
if (strm->avail_in == 0)
|
||||||
/* do the printf() into the input buffer, put length in len */
|
strm->next_in = state->in;
|
||||||
size = (int)(state->size);
|
next = (char *)(strm->next_in + strm->avail_in);
|
||||||
state->in[size - 1] = 0;
|
next[state->size - 1] = 0;
|
||||||
#ifdef NO_vsnprintf
|
#ifdef NO_vsnprintf
|
||||||
# ifdef HAS_vsprintf_void
|
# ifdef HAS_vsprintf_void
|
||||||
(void)vsprintf((char *)(state->in), format, va);
|
(void)vsprintf(next, format, va);
|
||||||
for (len = 0; len < size; len++)
|
for (len = 0; len < state->size; len++)
|
||||||
if (state->in[len] == 0) break;
|
if (next[len] == 0) break;
|
||||||
# else
|
# else
|
||||||
len = vsprintf((char *)(state->in), format, va);
|
len = vsprintf(next, format, va);
|
||||||
# endif
|
# endif
|
||||||
#else
|
#else
|
||||||
# ifdef HAS_vsnprintf_void
|
# ifdef HAS_vsnprintf_void
|
||||||
(void)vsnprintf((char *)(state->in), size, format, va);
|
(void)vsnprintf(next, state->size, format, va);
|
||||||
len = strlen((char *)(state->in));
|
len = strlen(next);
|
||||||
# else
|
# else
|
||||||
len = vsnprintf((char *)(state->in), size, format, va);
|
len = vsnprintf(next, state->size, format, va);
|
||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* check that printf() results fit in buffer */
|
/* check that printf() results fit in buffer */
|
||||||
if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
|
if (len == 0 || len >= state->size || next[state->size - 1] != 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* update buffer and position, defer compression until needed */
|
/* update buffer and position, compress first half if past that */
|
||||||
strm->avail_in = (unsigned)len;
|
strm->avail_in += len;
|
||||||
strm->next_in = state->in;
|
|
||||||
state->x.pos += len;
|
state->x.pos += len;
|
||||||
return len;
|
if (strm->avail_in >= state->size) {
|
||||||
|
left = strm->avail_in - state->size;
|
||||||
|
strm->avail_in = state->size;
|
||||||
|
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
||||||
|
return 0;
|
||||||
|
memcpy(state->in, state->in + state->size, left);
|
||||||
|
strm->next_in = state->in;
|
||||||
|
strm->avail_in = left;
|
||||||
|
}
|
||||||
|
return (int)len;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
|
int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
|
||||||
@@ -390,7 +399,8 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
|
|||||||
int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
|
int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
|
||||||
a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
|
a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
|
||||||
{
|
{
|
||||||
int size, len;
|
unsigned len, left;
|
||||||
|
char *next;
|
||||||
gz_statep state;
|
gz_statep state;
|
||||||
z_streamp strm;
|
z_streamp strm;
|
||||||
|
|
||||||
@@ -419,44 +429,52 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* consume whatever's left in the input buffer */
|
/* do the printf() into the input buffer, put length in len -- the input
|
||||||
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
|
buffer is double-sized just for this function, so there is guaranteed to
|
||||||
return 0;
|
be state->size bytes available after the current contents */
|
||||||
|
if (strm->avail_in == 0)
|
||||||
/* do the printf() into the input buffer, put length in len */
|
strm->next_in = state->in;
|
||||||
size = (int)(state->size);
|
next = (char *)(strm->next_in + strm->avail_in);
|
||||||
state->in[size - 1] = 0;
|
next[state->size - 1] = 0;
|
||||||
#ifdef NO_snprintf
|
#ifdef NO_snprintf
|
||||||
# ifdef HAS_sprintf_void
|
# ifdef HAS_sprintf_void
|
||||||
sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
|
sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
|
||||||
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
a13, a14, a15, a16, a17, a18, a19, a20);
|
||||||
for (len = 0; len < size; len++)
|
for (len = 0; len < size; len++)
|
||||||
if (state->in[len] == 0) break;
|
if (next[len] == 0)
|
||||||
|
break;
|
||||||
# else
|
# else
|
||||||
len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
|
len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
|
||||||
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
||||||
# endif
|
# endif
|
||||||
#else
|
#else
|
||||||
# ifdef HAS_snprintf_void
|
# ifdef HAS_snprintf_void
|
||||||
snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
|
snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
|
||||||
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
||||||
len = strlen((char *)(state->in));
|
len = strlen(next);
|
||||||
# else
|
# else
|
||||||
len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6,
|
len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
|
||||||
a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
|
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
||||||
a19, a20);
|
|
||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* check that printf() results fit in buffer */
|
/* check that printf() results fit in buffer */
|
||||||
if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
|
if (len == 0 || len >= state->size || next[state->size - 1] != 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* update buffer and position, defer compression until needed */
|
/* update buffer and position, compress first half if past that */
|
||||||
strm->avail_in = (unsigned)len;
|
strm->avail_in += len;
|
||||||
strm->next_in = state->in;
|
|
||||||
state->x.pos += len;
|
state->x.pos += len;
|
||||||
return len;
|
if (strm->avail_in >= state->size) {
|
||||||
|
left = strm->avail_in - state->size;
|
||||||
|
strm->avail_in = state->size;
|
||||||
|
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
||||||
|
return 0;
|
||||||
|
memcpy(state->in, state->in + state->size, left);
|
||||||
|
strm->next_in = state->in;
|
||||||
|
strm->avail_in = left;
|
||||||
|
}
|
||||||
|
return (int)len;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
7
zlib.h
7
zlib.h
@@ -1291,10 +1291,9 @@ ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size));
|
|||||||
default buffer size is 8192 bytes. This function must be called after
|
default buffer size is 8192 bytes. This function must be called after
|
||||||
gzopen() or gzdopen(), and before any other calls that read or write the
|
gzopen() or gzdopen(), and before any other calls that read or write the
|
||||||
file. The buffer memory allocation is always deferred to the first read or
|
file. The buffer memory allocation is always deferred to the first read or
|
||||||
write. Two buffers are allocated, either both of the specified size when
|
write. Three times that size in buffer space is allocated. A larger buffer
|
||||||
writing, or one of the specified size and the other twice that size when
|
size of, for example, 64K or 128K bytes will noticeably increase the speed
|
||||||
reading. A larger buffer size of, for example, 64K or 128K bytes will
|
of decompression (reading).
|
||||||
noticeably increase the speed of decompression (reading).
|
|
||||||
|
|
||||||
The new buffer size also affects the maximum length for gzprintf().
|
The new buffer size also affects the maximum length for gzprintf().
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user