From 3bfbce8312b93ef76555da77ac4967d30aeec9fa Mon Sep 17 00:00:00 2001 From: Giorgi Tchankvetadze Date: Sat, 6 Dec 2025 14:56:33 +0400 Subject: cmp: stop skipping bytes if EOF is reached Currently, if a skip value is larger than the file size, cmp will continue to loop and call getc() until the skip count is satisfied, even though EOF has been reached. For large skip values (e.g., 1 billion), this results in significant CPU waste and execution time (e.g., 11 seconds on a modern CPU). This patch checks for EOF inside the skip loop and breaks early. Test case: $ touch empty.txt $ time ./busybox cmp empty.txt empty.txt 1000000000 1000000000 Before: real 0m10.937s After: real 0m0.002s function old new delta cmp_main 594 616 +22 Signed-off-by: Giorgi Tchankvetadze Signed-off-by: Denys Vlasenko --- editors/cmp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/editors/cmp.c b/editors/cmp.c index 54f347508..332af6170 100644 --- a/editors/cmp.c +++ b/editors/cmp.c @@ -113,8 +113,8 @@ int cmp_main(int argc UNUSED_PARAM, char **argv) fmt = fmt_differ; if (ENABLE_DESKTOP) { - while (skip1) { getc(fp1); skip1--; } - while (skip2) { getc(fp2); skip2--; } + while (skip1) { if (getc(fp1) == EOF) break; skip1--; } + while (skip2) { if (getc(fp2) == EOF) break; skip2--; } } do { if (max_count >= 0 && --max_count < 0) -- cgit v1.2.3-55-g6feb