summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnders F Björklund <anders.f.bjorklund@gmail.com>2018-08-12 18:10:59 +0200
committerAnders F Björklund <anders.f.bjorklund@gmail.com>2018-08-12 18:14:39 +0200
commit92f8b27d4e8fbeb618dfe23652cf868430617f4b (patch)
treeaac6f346e16d7c38b8f396166bccbf32f9ba6963
parentc5cdf144aea3d6f11617b7ea475ff34c4c55de30 (diff)
downloadccache-92f8b27d4e8fbeb618dfe23652cf868430617f4b.tar.gz
Avoid using strcat and small realloc
For performance reasons with large strings, we don't want to call strlen multiple times or to realloc one byte at a time.
-rw-r--r--src/execute.c14
-rw-r--r--src/util.c15
2 files changed, 20 insertions, 9 deletions
diff --git a/src/execute.c b/src/execute.c
index e56526f1..c36f6416 100644
--- a/src/execute.c
+++ b/src/execute.c
@@ -358,11 +358,17 @@ format_command(char **argv)
len += strlen(argv[i]);
}
len += 1;
- char *buf = x_calloc(1, len + 1);
+ char *buf = x_malloc(len + 1);
+ char *p = buf;
for (int i = 0; argv[i]; i++) {
- strcat(buf, (i == 0) ? "" : " ");
- strcat(buf, argv[i]);
+ if (i != 0) {
+ *p++ = ' ';
+ }
+ for (char *q = argv[i]; *q != '\0'; q++) {
+ *p++ = *q;
+ }
}
- strcat(buf, "\n");
+ *p++ = '\n';
+ *p++ = '\0';
return buf;
}
diff --git a/src/util.c b/src/util.c
index d9143cea..40e553ea 100644
--- a/src/util.c
+++ b/src/util.c
@@ -35,8 +35,11 @@
static FILE *logfile;
static char *logbuffer;
+static size_t logbufsize;
static size_t logsize;
+#define LOGBUFSIZ 1024
+
static bool
init_log(void)
{
@@ -47,7 +50,8 @@ init_log(void)
}
assert(conf);
if (conf->debug) {
- logbuffer = x_calloc(1, 1);
+ logbufsize = LOGBUFSIZ;
+ logbuffer = x_malloc(logbufsize);
logsize = 0;
}
if (str_eq(conf->log_file, "")) {
@@ -68,11 +72,12 @@ static void
append_log(const char *s, size_t len)
{
assert(logbuffer);
- if (logsize + len > logsize) {
- logbuffer = x_realloc(logbuffer, logsize + len + 1);
- logsize = logsize + len;
+ if (logsize + len + 1 > logbufsize) {
+ logbufsize = logbufsize + len + 1 + LOGBUFSIZ;
+ logbuffer = x_realloc(logbuffer, logbufsize);
}
- strncat(logbuffer, s, len);
+ strncpy(logbuffer + logsize, s, len);
+ logsize += len;
}
static void