grep extremely slow

Rich Felker dalias at aerifal.cx
Fri Apr 7 21:28:57 UTC 2006


On Fri, Apr 07, 2006 at 03:24:03AM -0400, Rich Felker wrote:
> I've observed that busybox grep is still 20x slower than gnu grep,
> even with the regcomp issue I reported a while back fixed. The problem
> seems to be the bb_get_chunk_from_file function, which reads a single
> character at a time from the file using getc. Not sure what's the best
> way to fix it without breaking semantics needed by other applets..
> anyone care to take a look?

Here's an optimized bb_get_chomped_line_from_file:

char *bb_get_chomped_line_from_file(FILE *file)
{
	size_t idx, len;
	char *buf;

	for (idx=len=0, buf = NULL; !buf || buf[len-2] != '\n'; ) {
		idx = len;
		buf = xrealloc(buf, len += 80);
		buf[len-2] = '\n';
		if (!fgets(buf+idx, len-idx, file)) {
			if (!idx) {
				free(buf);
				return NULL;
			}
			break;
		}
	}
	for (; buf[idx] && buf[idx] != '\n'; idx++);
	buf[idx] = 0;
	return buf;
}

This seems to improve performance by several times, but it's still
about 6-8x slower than gnu grep from my testing...

Rich




More information about the busybox mailing list