diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-07-22 18:15:59 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-07-22 18:15:59 +0200 |
commit | 1310d7b1d106d7ab0ec84ce88c12302cca934230 (patch) | |
tree | 5312b9f17e92e5c7946ae56e5a22d236677bbecc | |
parent | 0068ce2fa0e3dbc618b8ca87ab56e144731da784 (diff) | |
download | busybox-w32-1310d7b1d106d7ab0ec84ce88c12302cca934230.tar.gz busybox-w32-1310d7b1d106d7ab0ec84ce88c12302cca934230.tar.bz2 busybox-w32-1310d7b1d106d7ab0ec84ce88c12302cca934230.zip |
ash: speed up ${v//pattern/repl} if !ASH_OPTIMIZE_FOR_SIZE
function old new delta
subevalvar 1353 1447 +94
.rodata 104179 104184 +5
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 99/0) Total: 99 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/ash.c | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/shell/ash.c b/shell/ash.c index 2eac6e113..092f3bcc7 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -6970,7 +6970,8 @@ scanright(char *startp, char *rmesc, char *rmescend, | |||
6970 | * Logic: | 6970 | * Logic: |
6971 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, | 6971 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
6972 | * and on each iteration they go back two/one char until they reach the beginning. | 6972 | * and on each iteration they go back two/one char until they reach the beginning. |
6973 | * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. | 6973 | * We try to match "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
6974 | * If one of these matches, return pointer past last matched char in startp. | ||
6974 | */ | 6975 | */ |
6975 | /* TODO: document in what other circumstances we are called. */ | 6976 | /* TODO: document in what other circumstances we are called. */ |
6976 | 6977 | ||
@@ -7258,6 +7259,7 @@ subevalvar(char *start, char *str, int strloc, | |||
7258 | #if BASH_PATTERN_SUBST | 7259 | #if BASH_PATTERN_SUBST |
7259 | workloc = expdest - (char *)stackblock(); | 7260 | workloc = expdest - (char *)stackblock(); |
7260 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { | 7261 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
7262 | size_t no_meta_len; | ||
7261 | int len; | 7263 | int len; |
7262 | char *idx, *end; | 7264 | char *idx, *end; |
7263 | 7265 | ||
@@ -7275,16 +7277,38 @@ subevalvar(char *start, char *str, int strloc, | |||
7275 | if (str[0] == '\0') | 7277 | if (str[0] == '\0') |
7276 | goto out1; | 7278 | goto out1; |
7277 | 7279 | ||
7280 | no_meta_len = (ENABLE_ASH_OPTIMIZE_FOR_SIZE || strpbrk(str, "*?[\\")) ? 0 : strlen(str); | ||
7278 | len = 0; | 7281 | len = 0; |
7279 | idx = startp; | 7282 | idx = startp; |
7280 | end = str - 1; | 7283 | end = str - 1; |
7281 | while (idx <= end) { | 7284 | while (idx <= end) { |
7282 | try_to_match: | 7285 | try_to_match: |
7283 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); | 7286 | if (no_meta_len == 0) { |
7287 | /* pattern has meta chars, have to glob; or ENABLE_ASH_OPTIMIZE_FOR_SIZE */ | ||
7288 | loc = scanright(idx, rmesc, rmescend, str, quotes, /*match_at_start:*/ 1); | ||
7289 | } else { | ||
7290 | /* Testcase for very slow replace (performs about 22k replaces): | ||
7291 | * x=:::::::::::::::::::::: | ||
7292 | * x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;echo ${#x} | ||
7293 | * echo "${x//:/|}" | ||
7294 | */ | ||
7295 | size_t n; | ||
7296 | if (strncmp(rmesc, str, no_meta_len) != 0) | ||
7297 | goto no_match; | ||
7298 | n = no_meta_len; | ||
7299 | loc = idx; | ||
7300 | do { | ||
7301 | if (quotes && (unsigned char)*loc == CTLESC) | ||
7302 | loc++; | ||
7303 | loc++; | ||
7304 | } while (--n != 0); | ||
7305 | } | ||
7284 | //bb_error_msg("scanright('%s'):'%s'", str, loc); | 7306 | //bb_error_msg("scanright('%s'):'%s'", str, loc); |
7285 | if (!loc) { | 7307 | if (!loc) { |
7308 | char *restart_detect; | ||
7309 | no_match: | ||
7286 | /* No match, advance */ | 7310 | /* No match, advance */ |
7287 | char *restart_detect = stackblock(); | 7311 | restart_detect = stackblock(); |
7288 | skip_matching: | 7312 | skip_matching: |
7289 | if (idx >= end) | 7313 | if (idx >= end) |
7290 | break; | 7314 | break; |