aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2018-03-25 11:50:36 +0800
committerBen Hutchings <ben@decadent.org.uk>2020-03-28 21:42:54 +0000
commit2e20c3194ef01b36e2975764fa23aae89d802138 (patch)
tree4ff382eee7e9c45fa4e5dd104b8403f02bc0c2bd
parent887c4118916c8ee1aff8cdefbb691bd835c6566e (diff)
downloadklibc-2e20c3194ef01b36e2975764fa23aae89d802138.tar.gz
[klibc] dash: builtin: Fix echo performance regression
[ dash commit 42b730b034eebd0e9da4aa014785a3ee5de436be ] The commit d6c0e1e2ffbf7913ab69d51cc794d48d41c8fcb1 ("[BUILTIN] Handle embedded NULs correctly in printf") caused a performance regression in the echo built-in because every echo call now goes through the printf %b slow path where the string is always printed twice to ensure the space padding is correct in the presence of NUL characters. In fact this regression applies to printf %b as well. This is easily fixed by making printf %b take the fast path when no precision/field width modifiers are present. This patch also changes the second strchurnul call to strspn which generates slightly better code. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
-rw-r--r--usr/dash/bltin/printf.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/usr/dash/bltin/printf.c b/usr/dash/bltin/printf.c
index 1f1fbde76f807..fe055fd26c9e3 100644
--- a/usr/dash/bltin/printf.c
+++ b/usr/dash/bltin/printf.c
@@ -100,20 +100,25 @@ static int print_escape_str(const char *f, int *param, int *array, char *s)
int total;
setstackmark(&smark);
- done = conv_escape_str(s, &p);
- q = stackblock();
- len = p - q;
+ done = conv_escape_str(s, &q);
+ p = stackblock();
+ len = q - p;
+ total = len - 1;
+
+ if (f[1] == 's')
+ goto easy;
- p = makestrspace(len, p);
- memset(p, 'X', len - 1);
- p[len - 1] = 0;
+ p = makestrspace(len, q);
+ memset(p, 'X', total);
+ p[total] = 0;
q = stackblock();
total = ASPF(&p, f, p);
len = strchrnul(p, 'X') - p;
- memcpy(p + len, q, strchrnul(p + len, ' ') - (p + len));
+ memcpy(p + len, q, strspn(p + len, "X"));
+easy:
out1mem(p, total);
popstackmark(&smark);