[PATCH][PING] Improve stpncpy performance

Ondřej Bílka neleai@seznam.cz
Thu Jul 9 13:21:00 GMT 2015


On Mon, Jul 06, 2015 at 12:29:24PM +0100, Wilco Dijkstra wrote:
> > Wilco wrote:
> > ping
> > 
> > > > -----Original Message-----
> > > > From: Wilco Dijkstra [mailto:wdijkstr@arm.com]
> > > > Sent: 12 January 2015 15:09
> > > > To: 'libc-alpha@sourceware.org'
> > > > Subject: [PATCH] Improve stpncpy performance
> > > >
> > > > Like strncpy, this patch improves stpncpy performance by using strnlen/memcpy/memset
> > rather
> > > > than a byte loop. Performance on bench-stpncpy is ~2x faster on average.
> > > >
> > > > +  size_t size = __strnlen (src, n);
> > > > +  memcpy (dest, src, size);
> > > > +  dest += size;
> > > > +  if (size == n)
> > > >      return dest;
> > > > -
> > > > -  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;
> > > > +  return memset (dest, '\0', n - size);
> > > >  }
> > > >  #ifdef weak_alias
> > > >  libc_hidden_def (__stpncpy)
> > > > --
> > > > 1.9.1
> 
You don't have to use special case

if (size == n)
  return dest;

as it should be handled by

return memset (dest, '\0', 0);

That could improve performance a bit if its rare case. That doesn't
matter much as memset makes that function slow and it shouldn't be 
used in performance sensitive code.

Otherwise ok for me.



More information about the Libc-alpha mailing list