[git commit] ash.c: speedup ${s:} substring (no quotes)

Denys Vlasenko vda.linux at googlemail.com
Sat Oct 9 00:15:52 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=21e8dbfd9d11a461ed7f91b495fa39d8a9131b28
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

This trivial patch makes ${s:...} at least as fast as ${s#??..}
in simple tests. It's probably faster for longer substrings,
but then one wouldn't use ${s#"1024???s"} anyway -
one would switch away from sh.

function                                             old     new   delta
subevalvar                                          1457    1503     +46

Signed-off-by: Alin Mr <almr.oss at outlook.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/ash.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index 7b85981ec..e8ec0b1a6 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -7185,14 +7185,19 @@ subevalvar(char *start, char *str, int strloc,
 		if ((unsigned)len > (orig_len - pos))
 			len = orig_len - pos;
 
-		for (vstr = startp; pos; vstr++, pos--) {
-			if (quotes && (unsigned char)*vstr == CTLESC)
+		if (!quotes) {
+			loc = mempcpy(startp, startp + pos, len);
+		} else {
+			for (vstr = startp; pos != 0; pos--) {
+				if ((unsigned char)*vstr == CTLESC)
+					vstr++;
 				vstr++;
-		}
-		for (loc = startp; len; len--) {
-			if (quotes && (unsigned char)*vstr == CTLESC)
+			}
+			for (loc = startp; len != 0; len--) {
+				if ((unsigned char)*vstr == CTLESC)
+					*loc++ = *vstr++;
 				*loc++ = *vstr++;
-			*loc++ = *vstr++;
+			}
 		}
 		*loc = '\0';
 		goto out;


More information about the busybox-cvs mailing list