[PATCH 3/6] string: reduce strjoin runtime, drop trailing separator

Sascha Hauer sha at pengutronix.de
Thu Oct 27 00:33:34 PDT 2022


On Thu, Oct 27, 2022 at 09:24:20AM +0200, Ahmad Fatoum wrote:
> 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?

You wouldn't have to fixup code you just introduced.

> > 
> > 	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.

No problem ;)

	for (i = 0; i < arrlen - 1; i++) {
		p = stpcpy(p, arr[i]);
		p = stpcpy(p, separator);
	}

	stpcpy(p, arr[i]);

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 |



More information about the barebox mailing list