summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko2008-07-15 10:33:12 +0000
committerDenis Vlasenko2008-07-15 10:33:12 +0000
commit2132e0221301d2634c265541e62e3efea0cce9d9 (patch)
tree9b819c8fb79b84660c048027a9fca2ec723db9c0
parentbb13079c8e9ca7bd86193a220baae1befb053fd6 (diff)
downloadbusybox-2132e0221301d2634c265541e62e3efea0cce9d9.zip
busybox-2132e0221301d2634c265541e62e3efea0cce9d9.tar.gz
libbb: experimental faster string reading routines.
-rw-r--r--include/libbb.h6
-rw-r--r--libbb/get_line_from_file.c70
2 files changed, 75 insertions, 1 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 671584b..2bd614c 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -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: */
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: */
extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string) FAST_FUNC;
/* 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;
/* Chops off '\n' from the end, unlike fgets: */
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_stdout(void) FAST_FUNC;
extern void xfflush_stdout(void) FAST_FUNC;
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c
index 3a76f49..7b65ced 100644
--- a/libbb/get_line_from_file.c
+++ b/libbb/get_line_from_file.c
@@ -67,3 +67,73 @@ char* FAST_FUNC xmalloc_fgetline(FILE *file)
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