From 3c439e0a8410d713488b16af5842ad8ef0cddb04 Mon Sep 17 00:00:00 2001 From: Dmitry Antipov Date: Mon, 7 Oct 2013 11:15:37 +0400 Subject: [PATCH] * alloc.c (Fmake_string): For ASCII char initializer, prefer memset to explicit loop. Otherwise copy largest possible chunk from initialized to uninitialized part, thus allowing the longer memcpy runs and reducing the number of loop iterations. --- src/ChangeLog | 7 +++++++ src/alloc.c | 26 +++++++++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 07af64ab1a1..4be64c13993 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2013-10-07 Dmitry Antipov + + * alloc.c (Fmake_string): For ASCII char initializer, prefer + memset to explicit loop. Otherwise copy largest possible chunk + from initialized to uninitialized part, thus allowing the longer + memcpy runs and reducing the number of loop iterations. + 2013-10-06 Jan Djärv * nsterm.m (ns_update_begin): If native fullscreen and no toolbar, diff --git a/src/alloc.c b/src/alloc.c index 0a7fda6815c..56a9763db15 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -1973,7 +1973,6 @@ INIT must be an integer that represents a character. */) (Lisp_Object length, Lisp_Object init) { register Lisp_Object val; - register unsigned char *p, *end; int c; EMACS_INT nbytes; @@ -1985,31 +1984,36 @@ INIT must be an integer that represents a character. */) { nbytes = XINT (length); val = make_uninit_string (nbytes); - p = SDATA (val); - end = p + SCHARS (val); - while (p != end) - *p++ = c; + memset (SDATA (val), c, nbytes); + SDATA (val)[nbytes] = 0; } else { unsigned char str[MAX_MULTIBYTE_LENGTH]; int len = CHAR_STRING (c, str); EMACS_INT string_len = XINT (length); + unsigned char *p, *beg, *end; if (string_len > STRING_BYTES_MAX / len) string_overflow (); nbytes = len * string_len; val = make_uninit_multibyte_string (string_len, nbytes); - p = SDATA (val); - end = p + nbytes; - while (p != end) + for (beg = SDATA (val), p = beg, end = beg + nbytes; p < end; p += len) { - memcpy (p, str, len); - p += len; + /* First time we just copy `str' to the data of `val'. */ + if (p == beg) + memcpy (p, str, len); + else + { + /* Next time we copy largest possible chunk from + initialized to uninitialized part of `val'. */ + len = min (p - beg, end - p); + memcpy (p, beg, len); + } } + *p = 0; } - *p = 0; return val; } -- 2.39.2