diff options
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | builtin.c | 12 |
2 files changed, 16 insertions, 5 deletions
@@ -1,3 +1,12 @@ +2014-08-13 Arnold D. Robbins <arnold@skeeve.com> + + * builtin.c (do_sub): Move initial allocation of the replacement + string down towards code to do the replacement, with a (we hope) + better guesstimate of how much to initially allocate. The idea + is to avoid unnecessary realloc() calls by making a better guess + at how much to allocate. This came up in an email discussion + with Tom Dickey about mawk's gsub(). + 2014-08-12 Arnold D. Robbins <arnold@skeeve.com> OFS being set should rebuild $0 using previous OFS if $0 @@ -2786,16 +2786,11 @@ set_how_many: text = t->stptr; textlen = t->stlen; - buflen = textlen + 2; repl = s->stptr; replend = repl + s->stlen; repllen = replend - repl; - emalloc(buf, char *, buflen + 2, "do_sub"); - buf[buflen] = '\0'; - buf[buflen + 1] = '\0'; - ampersands = 0; /* @@ -2854,6 +2849,13 @@ set_how_many: } lastmatchnonzero = false; + + /* guesstimate how much room to allocate; +2 forces > 0 */ + buflen = textlen + (ampersands + 1) * repllen + 2; + emalloc(buf, char *, buflen + 2, "do_sub"); + buf[buflen] = '\0'; + buf[buflen + 1] = '\0'; + bp = buf; for (current = 1;; current++) { matches++; |