mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: Sascha Hauer <sha@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 3/6] string: reduce strjoin runtime, drop trailing separator
Date: Thu, 27 Oct 2022 09:24:20 +0200	[thread overview]
Message-ID: <dc48c8c4-517c-349f-8582-1a718b4d699f@pengutronix.de> (raw)
In-Reply-To: <20221027065651.GQ6702@pengutronix.de>

Hi,

On 27.10.22 08:56, Sascha Hauer wrote:
> On Wed, Oct 26, 2022 at 08:42:02AM +0200, Ahmad Fatoum wrote:
>> The implementation of strjoin is a bit suboptimal. The destination
>> string is traversed from the beginning due to strcat and we have a
>> left-over separator at the end, while it should only be in-between.
>>
>> Fix this.
> 
> Rather than fixing a just introduced function I would expect a patch
> introducing strjoin() and then another one converting the time command
> over to use it.

What difference does it make?

>> +void *mempcpy(void *dest, const void *src, size_t count)
>> +{
>> +	return memcpy(dest, src, count) + count;
>> +}
>> +EXPORT_SYMBOL(mempcpy);
>>  
>>  #ifndef __HAVE_ARCH_MEMMOVE
>>  /**
>> @@ -943,7 +948,7 @@ char *strjoin(const char *separator, char **arr, size_t arrlen)
>>  {
>>  	size_t separatorlen;
>>  	int len = 1; /* '\0' */
>> -	char *buf;
>> +	char *buf, *p;
>>  	int i;
>>  
>>  	separatorlen = strlen(separator);
>> @@ -951,12 +956,14 @@ char *strjoin(const char *separator, char **arr, size_t arrlen)
>>  	for (i = 0; i < arrlen; i++)
>>  		len += strlen(arr[i]) + separatorlen;
>>  
>> -	buf = xzalloc(len);
>> +	p = buf = xmalloc(len);
>>  
>>  	for (i = 0; i < arrlen; i++) {
>> -		strcat(buf, arr[i]);
>> -		strcat(buf, separator);
>> +		p = stpcpy(p, arr[i]);
>> +		p = mempcpy(p, separator, separatorlen);
>>  	}
>>  
>> +	p[-separatorlen] = '\0';
> 
> That's a bit hard to read. How about:
> 
> 	for (i = 0; i < arrlen; i++) {
> 		p = stpcpy(p, arr[i]);
> 		if (i < arrlen - 1)
> 			p = stpcpy(p, separator);
> 	}
> 
> That would also allow you to optimize the allocated buffer size above.

I like my version more. I dislike these once-only checks inside loops.

Cheers,
Ahmad

> 
> Sascha
> 


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



  reply	other threads:[~2022-10-27  7:25 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-26  6:42 [PATCH 1/6] commands: add new uptime command Ahmad Fatoum
2022-10-26  6:42 ` [PATCH 2/6] commands: time: refactor into new strjoin Ahmad Fatoum
2022-10-26  6:42 ` [PATCH 3/6] string: reduce strjoin runtime, drop trailing separator Ahmad Fatoum
2022-10-27  6:56   ` Sascha Hauer
2022-10-27  7:24     ` Ahmad Fatoum [this message]
2022-10-27  7:33       ` Sascha Hauer
2022-10-27  7:53         ` Ahmad Fatoum
2022-10-26  6:42 ` [PATCH 4/6] test: self: add strjoin tests Ahmad Fatoum
2022-10-26  6:42 ` [PATCH 5/6] commands: drvinfo: support filtering by driver Ahmad Fatoum
2022-10-27  7:29   ` Sascha Hauer
2022-10-27  7:51     ` Ahmad Fatoum
2022-10-27  8:49       ` Sascha Hauer
2022-10-26  6:42 ` [PATCH 6/6] test: self: only include ramfs selftest when CONFIG_SELFTEST_FS_RAMFS=y Ahmad Fatoum
2022-10-27  7:11 ` [PATCH 1/6] commands: add new uptime command Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dc48c8c4-517c-349f-8582-1a718b4d699f@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=sha@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox