Improve stpncpy performance by using __strnlen/memcpy/memset rather than a

byte loop. Performance on bench-stpncpy is ~2x faster on average.
This commit is contained in:
Wilco Dijkstra 2015-08-19 16:28:21 +01:00
parent a08e80d114
commit 48497aba8e
2 changed files with 10 additions and 54 deletions

View File

@ -1,3 +1,8 @@
2015-08-19 Wilco Dijkstra <wdijkstr@arm.com>
* string/stpncpy.c (stpncpy): Improve performance using
__strnlen/memcpy/memset.
2015-08-19 Andrew Senkevich <andrew.senkevich@intel.com> 2015-08-19 Andrew Senkevich <andrew.senkevich@intel.com>
[BZ #18796] [BZ #18796]

View File

@ -15,8 +15,6 @@
License along with the GNU C Library; if not, see License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */ <http://www.gnu.org/licenses/>. */
/* This is almost copied from strncpy.c, written by Torbjorn Granlund. */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include <config.h> # include <config.h>
#endif #endif
@ -41,59 +39,12 @@ weak_alias (__stpncpy, stpncpy)
char * char *
STPNCPY (char *dest, const char *src, size_t n) STPNCPY (char *dest, const char *src, size_t n)
{ {
char c; size_t size = __strnlen (src, n);
char *s = dest; memcpy (dest, src, size);
dest += size;
if (n >= 4) if (size == n)
{
size_t n4 = n >> 2;
for (;;)
{
c = *src++;
*dest++ = c;
if (c == '\0')
break;
c = *src++;
*dest++ = c;
if (c == '\0')
break;
c = *src++;
*dest++ = c;
if (c == '\0')
break;
c = *src++;
*dest++ = c;
if (c == '\0')
break;
if (--n4 == 0)
goto last_chars;
}
n -= dest - s;
goto zero_fill;
}
last_chars:
n &= 3;
if (n == 0)
return dest; return dest;
return memset (dest, '\0', n - size);
for (;;)
{
c = *src++;
--n;
*dest++ = c;
if (c == '\0')
break;
if (n == 0)
return dest;
}
zero_fill:
while (n-- > 0)
dest[n] = '\0';
return dest - 1;
} }
#ifdef weak_alias #ifdef weak_alias
libc_hidden_def (__stpncpy) libc_hidden_def (__stpncpy)