diff options
author | Paul Eggert <eggert@Penguin.CS.UCLA.EDU> | 2020-01-17 23:59:51 -0800 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2020-01-18 00:02:12 -0800 |
commit | c1b6d5c5b9f8eee8aa3a8071292e8b3281ecf28a (patch) | |
tree | f484fd77b1eec8659d17531c39e1bff1301c5b33 /src/data.c | |
parent | bce3d89a6042da8830199d912c3b26aefaf7288c (diff) | |
download | emacs-c1b6d5c5b9f8eee8aa3a8071292e8b3281ecf28a.tar.gz |
Improve performance when a string's byte count changes
* src/alloc.c (allocate_string_data): Now static.
Remove code for when Faset calls this function when S
already has data assigned, as that can no longer happen.
(resize_string_data): New function, which avoids relocation in
more cases than the old code did, by not bothering to relocate
when the size changes falls within the alignment slop.
* src/data.c (Faset): Use resize_string_data.
Change a while to a do-while since it must iterate at least once.
Diffstat (limited to 'src/data.c')
-rw-r--r-- | src/data.c | 36 |
1 files changed, 10 insertions, 26 deletions
diff --git a/src/data.c b/src/data.c index c8445e7d874..cd7db6a0bb9 100644 --- a/src/data.c +++ b/src/data.c @@ -2303,34 +2303,18 @@ bool-vector. IDX starts at 0. */) if (STRING_MULTIBYTE (array)) { - ptrdiff_t idxval_byte, nbytes; - int prev_bytes, new_bytes; - unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1; - - nbytes = SBYTES (array); - idxval_byte = string_char_to_byte (array, idxval); - p1 = SDATA (array) + idxval_byte; - prev_bytes = BYTES_BY_CHAR_HEAD (*p1); - new_bytes = CHAR_STRING (c, p0); + unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf; + ptrdiff_t idxval_byte = string_char_to_byte (array, idxval); + unsigned char *p1 = SDATA (array) + idxval_byte; + + int prev_bytes = BYTES_BY_CHAR_HEAD (*p1); + int new_bytes = CHAR_STRING (c, p0); if (prev_bytes != new_bytes) - { - /* We must relocate the string data. */ - ptrdiff_t nchars = SCHARS (array); - USE_SAFE_ALLOCA; - unsigned char *str = SAFE_ALLOCA (nbytes); - - memcpy (str, SDATA (array), nbytes); - allocate_string_data (XSTRING (array), nchars, - nbytes + new_bytes - prev_bytes, false); - memcpy (SDATA (array), str, idxval_byte); - p1 = SDATA (array) + idxval_byte; - memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes, - nbytes - (idxval_byte + prev_bytes)); - SAFE_FREE (); - clear_string_char_byte_cache (); - } - while (new_bytes--) + p1 = resize_string_data (array, idxval_byte, prev_bytes, new_bytes); + + do *p1++ = *p0++; + while (--new_bytes != 0); } else { |