From 1d6ddfd9c0ed7119c0601474cded3a385068e886 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Thu, 24 Oct 2019 10:06:10 -0400 Subject: [PATCH] Optimize string concatenation. --- FIXES | 2 ++ run.c | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/FIXES b/FIXES index 1e8e2f4..1a9ae59 100644 --- a/FIXES +++ b/FIXES @@ -28,6 +28,8 @@ was sent to the printers in August, 1987. October 24, 2019: Import second round of code cleanups from NetBSD. Much thanks to Christos Zoulas (Github user zoulasc). Merges PR 53. + Add an optimization for string concatenation, also from + Christos. October 17, 2019: Import code cleanups from NetBSD. Much thanks to Christos diff --git a/run.c b/run.c index b05eb7c..f5bcdc5 100644 --- a/run.c +++ b/run.c @@ -1196,13 +1196,14 @@ Cell *cat(Node **a, int q) /* a[0] cat a[1] */ x = execute(a[0]); n1 = strlen(getsval(x)); - adjbuf(&s, &ssz, n1 + 1, recsize, 0, "cat1"); - (void) strncpy(s, x->sval, ssz); y = execute(a[1]); n2 = strlen(getsval(y)); - adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat2"); - (void) strncpy(s + n1, y->sval, ssz - n1); + + adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat"); + memcpy(s, x->sval, n1); + memcpy(s + n1, y->sval, n2); + s[n1 + n2] = '\0'; tempfree(x); tempfree(y);