[PATCH] Huge performance boost for recursion (cp, du, find, ls, rm, , mv)

Rolf Eike Beer eb at emlix.com
Thu Apr 11 05:47:10 UTC 2024


On Mittwoch, 10. April 2024 21:36:06 CEST Jody Bruchon wrote:
> This patch uses pre-calculated name lengths to massively speed up various
> recursive operations. Three new *_fast variant functions are added along
> with get_d_namlen copied from libjodycode. Passing lengths allows use of
> memcpy() instead of strcpy()/strcat() and replacement of a particularly
> hot xasprintf(). Cachegrind shows CPU instructions on Linux x86_64 drop
> by 24% to 67% with similar reductions in data reads and writes.
> 
> Anything in BusyBox that uses a while(readdir()) loop or that calls
> concat_*path_file() or last_char_is() might benefit from adopting this
> optimization framework.

Completely untested, but how about this:

char* FAST_FUNC concat_path_file_fast(const char *path, const struct dirent 
*dirp)
{
	const char *filename = dirp->d_name;
	char *buf;
	int end_offset;
	int pathlen, namelen;

	if (!path) {
		path = "";
		pathlen = 0;
	} else {
		pathlen = strlen(path);
		if (last_char_is_fast(path, '/', pathlen) == NULL) 
pathlen--;
	}
	namelen = get_d_namlen(dirp);
	while (*filename == '/') {
		filename++;
		namelen--;
	}
	end_offset = pathlen + 1 + namelen;
	buf = (char *)malloc(end_offset + 1);
	if (!buf) return NULL;
	memcpy(buf, path, pathlen);
	*(buf + pathlen) = '/';
	memcpy(buf + pathlen + 1, filename, namelen);
	*(buf + end_offset) = '\0';
	return buf;
}

This avoids scanning an empty path for the trailing slash, and avoids the 
check for a trailing slash later entirely, saving one instruction and 2 
variables.

You also have quite some places where you have the old code still around and 
just commented out, I would have just removed them.

Generally I wonder if the length variables shouldn't be size_t or the like, 
which would not affect this function but all of them.

Eike
-- 
Besuchen Sie uns auf der Embedded World 2024
9. bis 11. April 2024 | Messe Nürnberg
Sie finden uns in Halle 4, Stand 336

--
Rolf Eike Beer

emlix GmbH
Headquarters: Berliner Str. 12, 37073 Göttingen, Germany
Phone +49 (0)551 30664-0, e-mail info at emlix.com
District Court of Göttingen, Registry Number HR B 3160
Managing Directors: Heike Jordan, Dr. Uwe Kracke
VAT ID No. DE 205 198 055
Office Berlin: Panoramastr. 1, 10178 Berlin, Germany
Office Bonn: Bachstr. 6, 53115 Bonn, Germany
http://www.emlix.com

emlix - your embedded Linux partner
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 313 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20240411/1c7173cc/attachment.asc>


More information about the busybox mailing list