From 2b4dbe5fa8dc02d9cf4849fbda3197a522f16002 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 30 Dec 2022 20:51:59 +0000 Subject: libbb: speed up bb_get_chunk_from_file() Use _getc_nolock() in bb_get_chunk_from_file() and perform code page translation on the resulting string. This speeds up grep by another factor of two. (GitHub issue #278) --- libbb/get_line_from_file.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'libbb/get_line_from_file.c') diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c index 929bab78a..ce5c7810e 100644 --- a/libbb/get_line_from_file.c +++ b/libbb/get_line_from_file.c @@ -16,7 +16,11 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end) size_t idx = 0; char *linebuf = NULL; +#if ENABLE_PLATFORM_MINGW32 + while ((ch = _getc_nolock(file)) != EOF) { +#else while ((ch = getc(file)) != EOF) { +#endif /* grow the line buffer as necessary */ if (!(idx & 0xff)) { if (idx == ((size_t)-1) - 0xff) @@ -41,6 +45,11 @@ char* FAST_FUNC bb_get_chunk_from_file(FILE *file, size_t *end) linebuf = xrealloc(linebuf, idx + 1); linebuf[idx] = '\0'; } +#if ENABLE_PLATFORM_MINGW32 + if (idx && isatty(fileno(file)) && + GetStdHandle(STD_INPUT_HANDLE) != INVALID_HANDLE_VALUE) + OemToCharBuff(linebuf, linebuf, idx); +#endif return linebuf; } -- cgit v1.2.3-55-g6feb