diff options
| author | Tom Lane | 2020-09-05 20:20:04 +0000 |
|---|---|---|
| committer | Tom Lane | 2020-09-05 20:20:04 +0000 |
| commit | e0f05cd5ba76a75e2ce3b85ba050e48e857dca00 (patch) | |
| tree | 5c0ba4e65ab145e66f8c1ccadf026a3f4cbea957 /src/bin/initdb | |
| parent | a5cc4dab6d1d694f113912a2aca7012a95262f0b (diff) | |
Improve some ancient, crufty code in bootstrap + initdb.
At some point back in the last century, somebody felt that reading
all of pg_type twice was cheaper, or at least easier, than using
repalloc() to resize the Typ[] array dynamically. That seems like an
entirely wacko proposition, so rewrite the code to do it the other
way. (To add insult to injury, there were two not-quite-identical
copies of said code.)
initdb.c's readfile() function had the same disease of preferring
to do double the I/O to avoid resizing its output array. Here,
we can make things easier by using the just-invented pg_get_line()
function to handle reading individual lines without a predetermined
notion of how long they are.
On my machine, it's difficult to detect any net change in the
overall runtime of initdb from these changes; but they should
help on slower buildfarm machines (especially since a buildfarm
cycle involves a lot of initdb's these days).
My attention was drawn to these places by scan-build complaints,
but on inspection they needed a lot more work than just suppressing
dead stores :-(
Diffstat (limited to 'src/bin/initdb')
| -rw-r--r-- | src/bin/initdb/initdb.c | 46 |
1 files changed, 14 insertions, 32 deletions
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index 73ddf408654..861b8817b93 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -468,14 +468,11 @@ filter_lines_with_token(char **lines, const char *token) static char ** readfile(const char *path) { + char **result; FILE *infile; - int maxlength = 1, - linelen = 0; - int nlines = 0; + int maxlines; int n; - char **result; - char *buffer; - int c; + char *ln; if ((infile = fopen(path, "r")) == NULL) { @@ -483,39 +480,24 @@ readfile(const char *path) exit(1); } - /* pass over the file twice - the first time to size the result */ + maxlines = 1024; + result = (char **) pg_malloc(maxlines * sizeof(char *)); - while ((c = fgetc(infile)) != EOF) + n = 0; + while ((ln = pg_get_line(infile)) != NULL) { - linelen++; - if (c == '\n') + /* make sure there will be room for a trailing NULL pointer */ + if (n >= maxlines - 1) { - nlines++; - if (linelen > maxlength) - maxlength = linelen; - linelen = 0; + maxlines *= 2; + result = (char **) pg_realloc(result, maxlines * sizeof(char *)); } - } - - /* handle last line without a terminating newline (yuck) */ - if (linelen) - nlines++; - if (linelen > maxlength) - maxlength = linelen; - /* set up the result and the line buffer */ - result = (char **) pg_malloc((nlines + 1) * sizeof(char *)); - buffer = (char *) pg_malloc(maxlength + 1); - - /* now reprocess the file and store the lines */ - rewind(infile); - n = 0; - while (fgets(buffer, maxlength + 1, infile) != NULL && n < nlines) - result[n++] = pg_strdup(buffer); + result[n++] = ln; + } + result[n] = NULL; fclose(infile); - free(buffer); - result[n] = NULL; return result; } |
