saait

Simple static page generator
git clone https://git.sinitax.com/codemadness/saait
Log | Files | Refs | README | LICENSE | Upstream | sfeed.txt

commit c8d25c5c048770baa20b974084a77da70d130ffc
parent 7c19117912a82008cd2e65a62c399ec7f6c439ab
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sun,  4 Dec 2016 13:48:53 +0100

improve readfile(): reduce memory usage, optimize for small files, remove extra copy (memcpy).

Diffstat:
MTODO | 25+++++++------------------
Msaait.c | 20++++++++++----------
2 files changed, 17 insertions(+), 28 deletions(-)

diff --git a/TODO b/TODO @@ -1,19 +1,11 @@ -optimize readfile: don't copy data twice (memcpy). - -reduce memory usage. - -allow to override output filename (for no prefix number). - -writepage() improve error messages for filenames (error on line ...). +x optimize readfile: don't copy data twice (memcpy). +x reduce memory usage. +x allow to override output filename (for no prefix number). +- writepage() improve error messages for filenames (error on line ...). show template name and filename. - -cfgfile: spaces around = skip - -don't use strndup or wrap it. - -simplify date parsing. - -improve documentation +- cfgfile: spaces around = skip +x don't use strndup or wrap it. +- improve documentation make template structure: @@ -21,17 +13,14 @@ make template structure: templates/name/item.ext templates/name/footer.ext - add able to check unused variables (-w option?). add used counter to variable struct. - must work on: Linux, OpenBSD, MingW (Windows). glibc musl OpenBSD libc - - show line number in error improve - about 500 LOC diff --git a/saait.c b/saait.c @@ -106,24 +106,24 @@ char * readfile(const char *file) { FILE *fp; - char buf[BUFSIZ], *s = NULL; + char *buf = NULL; size_t n, len = 0, size = 0; fp = efopen(file, "rb"); - s = ecalloc(1, size + 1); + buf = ecalloc(1, size + 1); /* always allocate an empty buffer */ while (!feof(fp)) { - if (!(n = fread(buf, 1, sizeof(buf), fp))) - break; - if (len + n > size) { - size = size ? size * 2 : sizeof(buf); /* greedy allocation */ - if (!(s = realloc(s, size + 1))) { + if (len + BUFSIZ + 1 > size) { + /* non-greedy allocation (common case is small textfiles) */ + size += BUFSIZ; + if (!(buf = realloc(buf, size + 1))) { fprintf(stderr, "realloc: %s\n", strerror(errno)); exit(1); } } - memcpy(&s[len], buf, n); + if (!(n = fread(&buf[len], 1, BUFSIZ, fp))) + break; len += n; - s[len] = '\0'; + buf[len] = '\0'; } if (ferror(fp)) { fprintf(stderr, "fread: file: %s %s\n", file, strerror(errno)); @@ -131,7 +131,7 @@ readfile(const char *file) } fclose(fp); - return s; + return buf; } struct variable *