diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2014-08-13 06:06:02 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2014-08-13 06:06:02 +0300 |
commit | aec76b2b6e79e36dafced60fd91a877f5db98c4e (patch) | |
tree | be14d8ef773e0d8290979a56ec92a985df00f4e4 | |
parent | 46810569c80b88bb468600eefe2eb460487ee0a1 (diff) | |
download | egawk-aec76b2b6e79e36dafced60fd91a877f5db98c4e.tar.gz egawk-aec76b2b6e79e36dafced60fd91a877f5db98c4e.tar.bz2 egawk-aec76b2b6e79e36dafced60fd91a877f5db98c4e.zip |
Minor improvement in gsub() memory management.
-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++; |