This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH][PING] Improve stpncpy performance
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: Wilco Dijkstra <wdijkstr at arm dot com>
- Cc: libc-alpha at sourceware dot org
- Date: Thu, 9 Jul 2015 13:06:55 +0200
- Subject: Re: [PATCH][PING] Improve stpncpy performance
- Authentication-results: sourceware.org; auth=none
- References: <000301d0b7df$02f253d0$08d6fb70$ at com>
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.