Module Name:    src
Committed By:   simonb
Date:           Sun Jan 12 06:38:21 UTC 2025

Modified Files:
        src/usr.bin/cmp: regular.c

Log Message:
Make cmp -s and cmp -l much faster by comparing blocks and skipping any
line number calculations not needed.  Approx 3-4x faster for two multi-GB
in-cache files on my Zen 3.

Restores the spirit of a NetII optimisation lost with 4.4lite2 import
in 1995.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/usr.bin/cmp/regular.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/cmp/regular.c
diff -u src/usr.bin/cmp/regular.c:1.25 src/usr.bin/cmp/regular.c:1.26
--- src/usr.bin/cmp/regular.c:1.25	Sat Jan  9 15:16:28 2021
+++ src/usr.bin/cmp/regular.c	Sun Jan 12 06:38:21 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: regular.c,v 1.25 2021/01/09 15:16:28 christos Exp $	*/
+/*	$NetBSD: regular.c,v 1.26 2025/01/12 06:38:21 simonb Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)regular.c	8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: regular.c,v 1.25 2021/01/09 15:16:28 christos Exp $");
+__RCSID("$NetBSD: regular.c,v 1.26 2025/01/12 06:38:21 simonb Exp $");
 #endif
 #endif /* not lint */
 
@@ -47,6 +47,7 @@ __RCSID("$NetBSD: regular.c,v 1.25 2021/
 #include <stdlib.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "extern.h"
 
@@ -88,18 +89,30 @@ c_regular(int fd1, const char *file1, of
 		}
 
 		blk_cnt = blk_sz;
-		for (; blk_cnt--; ++p1, ++p2, ++byte) {
-			if ((ch = *p1) != *p2) {
-				if (!lflag) {
-					diffmsg(file1, file2, byte, line);
-					/* NOTREACHED */
+		if ((lflag || sflag) && (memcmp(p1, p2, blk_sz) == 0)) {
+			/*
+			 * If the two blocks are the same and we are
+			 * using the -l or -s flags, we don't need to
+			 * count lines.  There is nothing else to do
+			 * except advance the pointers for munmap()
+			 * below.
+			 */
+			p1 += blk_sz;
+			p2 += blk_sz;
+		} else {
+			for (; blk_cnt--; ++p1, ++p2, ++byte) {
+				if ((ch = *p1) != *p2) {
+					if (!lflag) {
+						diffmsg(file1, file2, byte, line);
+						/* NOTREACHED */
+					}
+					dfound = 1;
+					(void)printf("%6lld %3o %3o\n",
+					    (long long)byte, ch, *p2);
 				}
-				dfound = 1;
-				(void)printf("%6lld %3o %3o\n",
-				    (long long)byte, ch, *p2);
+				if (ch == '\n')
+					++line;
 			}
-			if (ch == '\n')
-				++line;
 		}
 		munmap(p1 - blk_sz, blk_sz);
 		munmap(p2 - blk_sz, blk_sz);

Reply via email to