svn commit: r322582 - in stable/11: share/man/man5 share/mk tools/build/options usr.bin/grep

Kyle Evans kevans at FreeBSD.org
Wed Aug 16 17:38:39 UTC 2017


Author: kevans
Date: Wed Aug 16 17:38:37 2017
New Revision: 322582
URL: https://svnweb.freebsd.org/changeset/base/322582

Log:
  MFC r317254: bsdgrep: add BSD_GREP_FASTMATCH knob for built-in fastmatch
  
  Bugs have been found in the fastmatch implementation as used in bsdgrep.
  Some have been fixed (r316495) while fixes for others are in review
  (D10098).
  
  In comparison with the fastmatch implementation, Kyle Evans found that:
  
  - regex(3)'s performance with literal expressions offers a speed
    improvement over fastmatch
  
  - regex(3)'s performance, both with simple BREs and EREs, seems to be
    comparable
  
  The regex implementation was imported in r226035, and the commit message
  reports:
  
      This is a temporary solution until the whole regex library is
      not replaced so that BSD grep development can continue and the
      backported code gets some review and testing. This change only
      improves scalability slightly, there is no big performance boost
      yet but several minor bugs have been found and fixed.
  
  Introduce a WITH_/WITHOUT_BSD_GREP_FASTMATCH knob to support testing
  of both approaches.
  
  Regenerate src.conf(5) as per the original commit
  
  PR:		175314, 194823
  Approved by:	emaste (mentor, blanket MFC)

Added:
  stable/11/tools/build/options/WITHOUT_BSD_GREP_FASTMATCH
     - copied unchanged from r317254, head/tools/build/options/WITHOUT_BSD_GREP_FASTMATCH
Modified:
  stable/11/share/man/man5/src.conf.5
  stable/11/share/mk/src.opts.mk
  stable/11/usr.bin/grep/Makefile
  stable/11/usr.bin/grep/grep.c
  stable/11/usr.bin/grep/grep.h
  stable/11/usr.bin/grep/util.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/share/man/man5/src.conf.5
==============================================================================
--- stable/11/share/man/man5/src.conf.5	Wed Aug 16 17:22:42 2017	(r322581)
+++ stable/11/share/man/man5/src.conf.5	Wed Aug 16 17:38:37 2017	(r322582)
@@ -1,6 +1,6 @@
 .\" DO NOT EDIT-- this file is generated by tools/build/options/makeman.
 .\" $FreeBSD$
-.Dd August 5, 2017
+.Dd August 16, 2017
 .Dt SRC.CONF 5
 .Os
 .Sh NAME
@@ -214,6 +214,11 @@ Set to not build the BSD licensed version of cpio base
 .Xr libarchive 3 .
 .It Va WITH_BSD_GREP
 Install BSD-licensed grep as '[ef]grep' instead of GNU grep.
+.It Va WITHOUT_BSD_GREP_FASTMATCH
+Set this option to exclude the fastmatch implementation from
+.Xr bsdgrep 1 ,
+instead using only
+.Xr regex 3 .
 .It Va WITHOUT_BSNMP
 Set to not build or install
 .Xr bsnmpd 1

Modified: stable/11/share/mk/src.opts.mk
==============================================================================
--- stable/11/share/mk/src.opts.mk	Wed Aug 16 17:22:42 2017	(r322581)
+++ stable/11/share/mk/src.opts.mk	Wed Aug 16 17:38:37 2017	(r322582)
@@ -62,6 +62,7 @@ __DEFAULT_YES_OPTIONS = \
     BOOTPARAMD \
     BOOTPD \
     BSD_CPIO \
+    BSD_GREP_FASTMATCH \
     BSDINSTALL \
     BSNMP \
     BZIP2 \

Copied: stable/11/tools/build/options/WITHOUT_BSD_GREP_FASTMATCH (from r317254, head/tools/build/options/WITHOUT_BSD_GREP_FASTMATCH)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/tools/build/options/WITHOUT_BSD_GREP_FASTMATCH	Wed Aug 16 17:38:37 2017	(r322582, copy of r317254, head/tools/build/options/WITHOUT_BSD_GREP_FASTMATCH)
@@ -0,0 +1,5 @@
+.\" $FreeBSD$
+Set this option to exclude the fastmatch implementation from
+.Xr bsdgrep 1 ,
+instead using only
+.Xr regex 3 .

Modified: stable/11/usr.bin/grep/Makefile
==============================================================================
--- stable/11/usr.bin/grep/Makefile	Wed Aug 16 17:22:42 2017	(r322581)
+++ stable/11/usr.bin/grep/Makefile	Wed Aug 16 17:38:37 2017	(r322582)
@@ -15,10 +15,14 @@ bsdgrep.1: grep.1
 .endif
 SRCS=	file.c grep.c queue.c util.c
 
-# Extra files ported backported form some regex improvements
+.if ${MK_BSD_GREP_FASTMATCH} == "yes"
+# Extra files ported backported for some regex improvements
 .PATH: ${.CURDIR}/regex
 SRCS+=	fastmatch.c hashtable.c tre-compile.c tre-fastmatch.c
 CFLAGS+=-I${.CURDIR}/regex
+.else
+CFLAGS+= -DWITHOUT_FASTMATCH
+.endif
 
 CFLAGS.gcc+= --param max-inline-insns-single=500
 

Modified: stable/11/usr.bin/grep/grep.c
==============================================================================
--- stable/11/usr.bin/grep/grep.c	Wed Aug 16 17:22:42 2017	(r322581)
+++ stable/11/usr.bin/grep/grep.c	Wed Aug 16 17:38:37 2017	(r322582)
@@ -50,7 +50,9 @@ __FBSDID("$FreeBSD$");
 #include <string.h>
 #include <unistd.h>
 
+#ifndef WITHOUT_FASTMATCH
 #include "fastmatch.h"
+#endif
 #include "grep.h"
 
 #ifndef WITHOUT_NLS
@@ -87,7 +89,9 @@ unsigned int	 patterns;
 static unsigned int pattern_sz;
 struct pat	*pattern;
 regex_t		*r_pattern;
+#ifndef WITHOUT_FASTMATCH
 fastmatch_t	*fg_pattern;
+#endif
 
 /* Filename exclusion/inclusion patterns */
 unsigned int	fpatterns, dpatterns;
@@ -716,20 +720,25 @@ main(int argc, char *argv[])
 		usage();
 	}
 
+#ifndef WITHOUT_FASTMATCH
 	fg_pattern = grep_calloc(patterns, sizeof(*fg_pattern));
+#endif
 	r_pattern = grep_calloc(patterns, sizeof(*r_pattern));
 
 	/* Check if cheating is allowed (always is for fgrep). */
 	for (i = 0; i < patterns; ++i) {
+#ifndef WITHOUT_FASTMATCH
+		/* Attempt compilation with fastmatch regex and fallback to
+		   regex(3) if it fails. */
 		if (fastncomp(&fg_pattern[i], pattern[i].pat,
-		    pattern[i].len, cflags) != 0) {
-			/* Fall back to full regex library */
-			c = regcomp(&r_pattern[i], pattern[i].pat, cflags);
-			if (c != 0) {
-				regerror(c, &r_pattern[i], re_error,
-				    RE_ERROR_BUF);
-				errx(2, "%s", re_error);
-			}
+		    pattern[i].len, cflags) == 0)
+			continue;
+#endif
+		c = regcomp(&r_pattern[i], pattern[i].pat, cflags);
+		if (c != 0) {
+			regerror(c, &r_pattern[i], re_error,
+			    RE_ERROR_BUF);
+			errx(2, "%s", re_error);
 		}
 	}
 

Modified: stable/11/usr.bin/grep/grep.h
==============================================================================
--- stable/11/usr.bin/grep/grep.h	Wed Aug 16 17:22:42 2017	(r322581)
+++ stable/11/usr.bin/grep/grep.h	Wed Aug 16 17:38:37 2017	(r322582)
@@ -36,7 +36,9 @@
 #include <stdio.h>
 #include <zlib.h>
 
+#ifndef WITHOUT_FASTMATCH
 #include "fastmatch.h"
+#endif
 
 #ifdef WITHOUT_NLS
 #define	getstr(n)	 errstr[n]
@@ -127,7 +129,9 @@ extern unsigned int dpatterns, fpatterns, patterns;
 extern struct pat *pattern;
 extern struct epat *dpattern, *fpattern;
 extern regex_t	*er_pattern, *r_pattern;
+#ifndef WITHOUT_FASTMATCH
 extern fastmatch_t *fg_pattern;
+#endif
 
 /* For regex errors  */
 #define	RE_ERROR_BUF	512

Modified: stable/11/usr.bin/grep/util.c
==============================================================================
--- stable/11/usr.bin/grep/util.c	Wed Aug 16 17:22:42 2017	(r322581)
+++ stable/11/usr.bin/grep/util.c	Wed Aug 16 17:38:37 2017	(r322582)
@@ -49,7 +49,9 @@ __FBSDID("$FreeBSD$");
 #include <wchar.h>
 #include <wctype.h>
 
+#ifndef WITHOUT_FASTMATCH
 #include "fastmatch.h"
+#endif
 #include "grep.h"
 
 static int	 linesqueued;
@@ -317,10 +319,12 @@ procline(struct str *l, int nottext)
 		for (i = 0; i < patterns; i++) {
 			pmatch.rm_so = st;
 			pmatch.rm_eo = l->len;
+#ifndef WITHOUT_FASTMATCH
 			if (fg_pattern[i].pattern)
 				r = fastexec(&fg_pattern[i],
 				    l->dat, 1, &pmatch, leflags);
 			else
+#endif
 				r = regexec(&r_pattern[i], l->dat, 1,
 				    &pmatch, leflags);
 			r = (r == 0) ? 0 : REG_NOMATCH;
@@ -332,7 +336,11 @@ procline(struct str *l, int nottext)
 				    (size_t)pmatch.rm_eo != l->len)
 					r = REG_NOMATCH;
 			/* Check for whole word match */
+#ifndef WITHOUT_FASTMATCH
 			if (r == 0 && (wflag || fg_pattern[i].word)) {
+#else
+			if (r == 0 && wflag) {
+#endif
 				wchar_t wbegin, wend;
 
 				wbegin = wend = L' ';


More information about the svn-src-all mailing list