* [PATCH] lib: string.c: A speed optimized for strncpy
@ 2014-10-05 13:29 Rickard Strandqvist
2014-10-05 13:29 ` Rickard Strandqvist
0 siblings, 1 reply; 5+ messages in thread
From: Rickard Strandqvist @ 2014-10-05 13:29 UTC (permalink / raw)
To: Rickard Strandqvist, Grant Likely
Cc: Andrew Morton, Andi Kleen, Dan Carpenter, linux-kernel
This variant is in my tests about 7-10% faster, and I also think
it is perhaps even clearer code than before.
I assume that more will do the testing, I do not know if we should do tests
on different types of hardware as well, my test was on a new Intel I7.
Rickard Strandqvist (1):
lib: string.c: A speed optimized for strncpy
lib/string.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
--
1.7.10.4
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH] lib: string.c: A speed optimized for strncpy
2014-10-05 13:29 [PATCH] lib: string.c: A speed optimized for strncpy Rickard Strandqvist
@ 2014-10-05 13:29 ` Rickard Strandqvist
2014-10-05 15:36 ` Joe Perches
0 siblings, 1 reply; 5+ messages in thread
From: Rickard Strandqvist @ 2014-10-05 13:29 UTC (permalink / raw)
To: Rickard Strandqvist, Grant Likely
Cc: Andrew Morton, Andi Kleen, Dan Carpenter, linux-kernel
This variant is in my tests about 7-10% faster, and also think
it is perhaps even clearer code than before.
Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
---
lib/string.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/lib/string.c b/lib/string.c
index f3c6ff5..6961229 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -123,12 +123,12 @@ char *strncpy(char *dest, const char *src, size_t count)
{
char *tmp = dest;
- while (count) {
- if ((*tmp = *src) != 0)
- src++;
- tmp++;
- count--;
- }
+ while (count && (*tmp++ = *src++))
+ --count;
+
+ while (count--)
+ *tmp++ = '\0';
+
return dest;
}
EXPORT_SYMBOL(strncpy);
--
1.7.10.4
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] lib: string.c: A speed optimized for strncpy
2014-10-05 13:29 ` Rickard Strandqvist
@ 2014-10-05 15:36 ` Joe Perches
2014-10-05 16:01 ` Rickard Strandqvist
0 siblings, 1 reply; 5+ messages in thread
From: Joe Perches @ 2014-10-05 15:36 UTC (permalink / raw)
To: Rickard Strandqvist
Cc: Grant Likely, Andrew Morton, Andi Kleen, Dan Carpenter,
linux-kernel
On Sun, 2014-10-05 at 15:29 +0200, Rickard Strandqvist wrote:
> This variant is in my tests about 7-10% faster, and also think
> it is perhaps even clearer code than before.
[]
> diff --git a/lib/string.c b/lib/string.c
[]
> @@ -123,12 +123,12 @@ char *strncpy(char *dest, const char *src, size_t count)
> {
> char *tmp = dest;
>
> - while (count) {
> - if ((*tmp = *src) != 0)
> - src++;
> - tmp++;
> - count--;
> - }
> + while (count && (*tmp++ = *src++))
> + --count;
> +
> + while (count--)
> + *tmp++ = '\0';
Perhaps it could be faster to use memset.
It might depend on the value of count.
{
while (count && (*tmp++ = *src++))
count--;
if (count > 0)
memset(tmp, 0, count);
}
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] lib: string.c: A speed optimized for strncpy
2014-10-05 15:36 ` Joe Perches
@ 2014-10-05 16:01 ` Rickard Strandqvist
2014-10-15 11:27 ` Dan Carpenter
0 siblings, 1 reply; 5+ messages in thread
From: Rickard Strandqvist @ 2014-10-05 16:01 UTC (permalink / raw)
To: Joe Perches
Cc: Grant Likely, Andrew Morton, Andi Kleen, Dan Carpenter,
linux-kernel@vger.kernel.org
Hi
Yes, it can be faster, even if it is as you say, probably a difference
depending on the size of the count.
And even greater need to test this on a variety of hardware :-/
But I try to do my test with the memset variant to.
Kind regards
Rickard Strandqvist
2014-10-05 17:36 GMT+02:00 Joe Perches <joe@perches.com>:
> On Sun, 2014-10-05 at 15:29 +0200, Rickard Strandqvist wrote:
>> This variant is in my tests about 7-10% faster, and also think
>> it is perhaps even clearer code than before.
> []
>> diff --git a/lib/string.c b/lib/string.c
> []
>> @@ -123,12 +123,12 @@ char *strncpy(char *dest, const char *src, size_t count)
>> {
>> char *tmp = dest;
>>
>> - while (count) {
>> - if ((*tmp = *src) != 0)
>> - src++;
>> - tmp++;
>> - count--;
>> - }
>> + while (count && (*tmp++ = *src++))
>> + --count;
>> +
>> + while (count--)
>> + *tmp++ = '\0';
>
> Perhaps it could be faster to use memset.
> It might depend on the value of count.
>
> {
> while (count && (*tmp++ = *src++))
> count--;
>
> if (count > 0)
> memset(tmp, 0, count);
> }
>
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH] lib: string.c: A speed optimized for strncpy
2014-10-05 16:01 ` Rickard Strandqvist
@ 2014-10-15 11:27 ` Dan Carpenter
0 siblings, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2014-10-15 11:27 UTC (permalink / raw)
To: Rickard Strandqvist
Cc: Joe Perches, Grant Likely, Andrew Morton, Andi Kleen,
linux-kernel@vger.kernel.org
On Sun, Oct 05, 2014 at 06:01:43PM +0200, Rickard Strandqvist wrote:
> Hi
>
> Yes, it can be faster, even if it is as you say, probably a difference
> depending on the size of the count.
> And even greater need to test this on a variety of hardware :-/
Most architectures (the notable exception is ARM) have an their own
optimized strncpy() function. Probably strzcpy() should just call it.
char *strzcpy(char *dest, const char *src, size_t count)
{
strncpy(dest, src, count);
if (count)
dest[count - 1] = '\0';
return dest;
}
regards,
dan carpenter
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-10-15 11:27 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-05 13:29 [PATCH] lib: string.c: A speed optimized for strncpy Rickard Strandqvist
2014-10-05 13:29 ` Rickard Strandqvist
2014-10-05 15:36 ` Joe Perches
2014-10-05 16:01 ` Rickard Strandqvist
2014-10-15 11:27 ` Dan Carpenter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox