libbb: experimental faster string reading routines.

This commit is contained in:
Denis Vlasenko 2008-07-15 10:33:12 +00:00
parent bb13079c8e
commit 2132e02213
2 changed files with 75 additions and 1 deletions

View File

@ -607,6 +607,8 @@ extern void xopen_xwrite_close(const char* file, const char *str) FAST_FUNC;
/* Reads and prints to stdout till eof, then closes FILE. Exits on error: */ /* Reads and prints to stdout till eof, then closes FILE. Exits on error: */
extern void xprint_and_close_file(FILE *file) FAST_FUNC; extern void xprint_and_close_file(FILE *file) FAST_FUNC;
extern char *bb_get_chunk_from_file(FILE *file, int *end) FAST_FUNC;
/* Reads up to (and including) TERMINATING_STRING: */ /* Reads up to (and including) TERMINATING_STRING: */
extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC; extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC;
/* Chops off TERMINATING_STRING from the end: */ /* Chops off TERMINATING_STRING from the end: */
@ -615,7 +617,9 @@ extern char *xmalloc_fgetline_str(FILE *file, const char *terminating_string) FA
extern char *xmalloc_fgets(FILE *file) FAST_FUNC; extern char *xmalloc_fgets(FILE *file) FAST_FUNC;
/* Chops off '\n' from the end, unlike fgets: */ /* Chops off '\n' from the end, unlike fgets: */
extern char *xmalloc_fgetline(FILE *file) FAST_FUNC; extern char *xmalloc_fgetline(FILE *file) FAST_FUNC;
extern char *bb_get_chunk_from_file(FILE *file, int *end) FAST_FUNC; /* Same, but doesn't try to conserve space (may have some slack after the end) */
extern char *xmalloc_fgetline_fast(FILE *file) FAST_FUNC;
extern void die_if_ferror(FILE *file, const char *msg) FAST_FUNC; extern void die_if_ferror(FILE *file, const char *msg) FAST_FUNC;
extern void die_if_ferror_stdout(void) FAST_FUNC; extern void die_if_ferror_stdout(void) FAST_FUNC;
extern void xfflush_stdout(void) FAST_FUNC; extern void xfflush_stdout(void) FAST_FUNC;

View File

@ -67,3 +67,73 @@ char* FAST_FUNC xmalloc_fgetline(FILE *file)
return c; return c;
} }
/* Faster routines (~twice as fast). +170 bytes. Unused as of 2008-07.
*
* NB: they stop at NUL byte too.
* Performance is important here. Think "grep 50gigabyte_file"...
* Iironically, grep can't use it because of NUL issue.
* We sorely need C lib to provide fgets which reports size!
*/
static char* xmalloc_fgets_internal(FILE *file, int *sizep)
{
int len;
int idx = 0;
char *linebuf = NULL;
while (1) {
char *r;
linebuf = xrealloc(linebuf, idx + 0x100);
r = fgets(&linebuf[idx], 0x100, file);
if (!r) {
/* need to terminate in case this is error
* (EOF puts NUL itself) */
linebuf[idx] = '\0';
break;
}
/* stupid. fgets knows the len, it should report it somehow */
len = strlen(&linebuf[idx]);
idx += len;
if (len != 0xff || linebuf[idx - 1] == '\n')
break;
}
*sizep = idx;
if (idx) {
/* xrealloc(linebuf, idx + 1) is up to caller */
return linebuf;
}
free(linebuf);
return NULL;
}
/* Get line, remove trailing \n */
char* FAST_FUNC xmalloc_fgetline_fast(FILE *file)
{
int sz;
char *r = xmalloc_fgets_internal(file, &sz);
if (r && r[sz - 1] == '\n')
r[--sz] = '\0';
return r; /* not xrealloc(r, sz + 1)! */
}
#if 0
char* FAST_FUNC xmalloc_fgets(FILE *file)
{
int sz;
return xmalloc_fgets_internal(file, &sz);
}
/* Get line, remove trailing \n */
char* FAST_FUNC xmalloc_fgetline(FILE *file)
{
int sz;
char *r = xmalloc_fgets_internal(file, &sz);
if (!r)
return r;
if (r[sz - 1] == '\n')
r[--sz] = '\0';
return xrealloc(r, sz + 1);
}
#endif