{"thread":{"id":"45935","subject":"[PATCH 0/7] PCRE v2, PCRE v1 JIT, log -P & fixes","startedAt":"2017-05-11T17:02:10Z","lastAt":"2017-05-12T18:46:16Z","messageCount":10,"participants":["Ævar Arnfjörð Bjarmason","Brandon Williams"],"isPatch":true,"patchVersion":1,"patchTotal":7},"messages":[{"id":"319414","messageId":"20170511170142.15934-1-avarab@gmail.com","threadId":"45935","inReplyTo":null,"subject":"[PATCH 0/7] PCRE v2, PCRE v1 JIT, log -P & fixes","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-05-11T17:01:35Z","receivedAt":"2017-05-11T17:02:10Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"This goes on top of the 29 patch series of \"Easy to review grep &\npre-PCRE changes\" (<20170511091829.5634-1-avarab@gmail.com>;\nhttps://public-inbox.org/git/20170511091829.5634-1-avarab@gmail.com/).\n\nThis could be split into 3 unrelated things, but I have think it's\nprobably easier for everyone to bundle these up, since they all go on\ntop of the other series. Comments below:\n\nÆvar Arnfjörð Bjarmason (7):\n  grep: don't redundantly compile throwaway patterns under threading\n  grep: skip pthreads overhead when using one thread\n\nInternal changes to grep to not redundantly spawn threads. No\nfunctional changes, just internal cleanup.\n\n  log: add -P as a synonym for --perl-regexp\n\nTrivial change to add -P.\n\n  grep: add support for the PCRE v1 JIT API\n  grep: un-break building with PCRE < 8.32\n  grep: un-break building with PCRE < 8.20\n\nI tested ancient versions of PCRE, which turned up build issues that\nare fixed this time around.\n\n  grep: add support for PCRE v2\n\nThe main point of this whole thing.\n\n Documentation/rev-list-options.txt |   1 +\n Makefile                           |  30 +++++--\n builtin/grep.c                     |  16 +++-\n configure.ac                       |  77 +++++++++++++---\n grep.c                             | 180 ++++++++++++++++++++++++++++++++++++-\n grep.h                             |  31 +++++++\n revision.c                         |   2 +-\n t/t4202-log.sh                     |  12 +++\n t/test-lib.sh                      |   2 +-\n 9 files changed, 327 insertions(+), 24 deletions(-)\n\n-- \n2.11.0\n\n"},{"id":"319415","messageId":"20170511170142.15934-2-avarab@gmail.com","threadId":"45935","inReplyTo":"20170511170142.15934-1-avarab@gmail.com","subject":"[PATCH 1/7] grep: don't redundantly compile throwaway patterns under threading","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-05-11T17:01:36Z","receivedAt":"2017-05-11T17:02:18Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Change the pattern compilation logic under threading so that grep\ndoesn't compile a pattern it never ends up using on the non-threaded\ncode path, only to compile it again N times for N threads which will\neach use their own copy, ignoring the initially compiled pattern.\n\nThis redundant compilation dates back to the initial introduction of\nthe threaded grep in commit 5b594f457a (\"Threaded grep\",\n2010-01-25).\n\nThere was never any reason for doing this redundant work other than an\noversight in the initial commit. Jeff King suggested on-list in\n<20170414212325.fefrl3qdjigwyitd@sigill.intra.peff.net> that this\nmight be needed to check the pattern for sanity before threaded\nexecution commences.\n\nThat's not the case. The pattern is compiled under threading in\nstart_threads() before any concurrent execution has started by calling\npthread_create(), so if the pattern contains an error we still do the\nright thing. I.e. die with one error before any threaded execution has\ncommenced, instead of e.g. spewing out an error for each N threads,\nwhich could be a regression a change like this might inadvertently\nintroduce.\n\nThe undocumented --debug mode added in commit 17bf35a3c7 (\"grep: teach\n--debug option to dump the parse tree\", 2012-09-13) still works\nproperly with this change. It only emits debugging info during pattern\ncompilation, which is now dumped by the pattern compiled just before\nthe first thread is started.\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n builtin/grep.c | 14 +++++++++++---\n 1 file changed, 11 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex 50e4bd2cd2..7baa4778b7 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -224,7 +224,8 @@ static void start_threads(struct grep_opt *opt)\n \t\tint err;\n \t\tstruct grep_opt *o = grep_opt_dup(opt);\n \t\to->output = strbuf_out;\n-\t\to->debug = 0;\n+\t\tif (i)\n+\t\t\to->debug = 0;\n \t\tcompile_grep_patterns(o);\n \t\terr = pthread_create(&threads[i], NULL, run, o);\n \n@@ -1169,8 +1170,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \tif (!opt.fixed && opt.ignore_case)\n \t\topt.regflags |= REG_ICASE;\n \n-\tcompile_grep_patterns(&opt);\n-\n \t/*\n \t * We have to find \"--\" in a separate pass, because its presence\n \t * influences how we will parse arguments that come before it.\n@@ -1247,6 +1246,15 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \tnum_threads = 0;\n #endif\n \n+\tif (!num_threads)\n+\t\t/*\n+\t\t * The compiled patterns on the main path are only\n+\t\t * used when not using threading. Otherwise\n+\t\t * start_threads() below calls compile_grep_patterns()\n+\t\t * for each thread.\n+\t\t */\n+\t\tcompile_grep_patterns(&opt);\n+\n #ifndef NO_PTHREADS\n \tif (num_threads) {\n \t\tif (!(opt.name_only || opt.unmatch_name_only || opt.count)\n-- \n2.11.0\n\n"},{"id":"319416","messageId":"20170511170142.15934-3-avarab@gmail.com","threadId":"45935","inReplyTo":"20170511170142.15934-1-avarab@gmail.com","subject":"[PATCH 2/7] grep: skip pthreads overhead when using one thread","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-05-11T17:01:37Z","receivedAt":"2017-05-11T17:02:24Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Skip the administrative overhead of using pthreads when only using one\nthread. Instead take the non-threaded path which would be taken under\nNO_PTHREADS.\n\nThe threading support was initially added in commit\n5b594f457a (\"Threaded grep\", 2010-01-25) with a hardcoded compile-time\nnumber of 8 threads. Later the number of threads was made configurable\nin commit 89f09dd34e (\"grep: add --threads=<num> option and\ngrep.threads configuration\", 2015-12-15).\n\nThat change did not add any special handling for --threads=1. Now we\ntake a slightly faster path by skipping thread handling entirely when\n1 thread is requested.\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n builtin/grep.c | 2 ++\n 1 file changed, 2 insertions(+)\n\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex 7baa4778b7..9c0d1ecc12 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -1240,6 +1240,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t\tnum_threads = GREP_NUM_THREADS_DEFAULT;\n \telse if (num_threads < 0)\n \t\tdie(_(\"invalid number of threads specified (%d)\"), num_threads);\n+\tif (num_threads == 1)\n+\t\tnum_threads = 0;\n #else\n \tif (num_threads)\n \t\twarning(_(\"no threads support, ignoring --threads\"));\n-- \n2.11.0\n\n"},{"id":"319417","messageId":"20170511170142.15934-4-avarab@gmail.com","threadId":"45935","inReplyTo":"20170511170142.15934-1-avarab@gmail.com","subject":"[PATCH 3/7] log: add -P as a synonym for --perl-regexp","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-05-11T17:01:38Z","receivedAt":"2017-05-11T17:02:32Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Add a short -P option as a synonym for the longer --perl-regexp, for\nconsistency with the options the corresponding grep invocations\naccept.\n\nThis was intentionally omitted in commit 727b6fc3ed (\"log --grep:\naccept --basic-regexp and --perl-regexp\", 2012-10-03) for unspecified\nfuture use.\n\nMake it consistent with \"grep\" rather than to keep it open for future\nuse, and to avoid the confusion of -P meaning different things for\ngrep & log, as is the case with the -G option.\n\nAs noted in the aforementioned commit the --basic-regexp option can't\nhave a corresponding -G argument, as the log command already uses that\nfor -G<regex>.\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n Documentation/rev-list-options.txt |  1 +\n revision.c                         |  2 +-\n t/t4202-log.sh                     | 12 ++++++++++++\n 3 files changed, 14 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/rev-list-options.txt b/Documentation/rev-list-options.txt\nindex a46f70c2b1..9c44eae55d 100644\n--- a/Documentation/rev-list-options.txt\n+++ b/Documentation/rev-list-options.txt\n@@ -91,6 +91,7 @@ endif::git-rev-list[]\n \tConsider the limiting patterns to be fixed strings (don't interpret\n \tpattern as a regular expression).\n \n+-P::\n --perl-regexp::\n \tConsider the limiting patterns to be Perl-compatible regular\n \texpressions.\ndiff --git a/revision.c b/revision.c\nindex 7ff61ff5f7..03a3a012de 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -1995,7 +1995,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg\n \t\tDIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);\n \t} else if (!strcmp(arg, \"--fixed-strings\") || !strcmp(arg, \"-F\")) {\n \t\trevs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;\n-\t} else if (!strcmp(arg, \"--perl-regexp\")) {\n+\t} else if (!strcmp(arg, \"--perl-regexp\") || !strcmp(arg, \"-P\")) {\n \t\trevs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;\n \t} else if (!strcmp(arg, \"--all-match\")) {\n \t\trevs->grep_filter.all_match = 1;\ndiff --git a/t/t4202-log.sh b/t/t4202-log.sh\nindex b0122a1991..a87b7f6089 100755\n--- a/t/t4202-log.sh\n+++ b/t/t4202-log.sh\n@@ -331,8 +331,20 @@ test_expect_success 'log with various grep.patternType configurations & command-\n \t\t\t--grep=\"(1|2)\" >actual.fixed.short-arg &&\n \t\tgit log --pretty=tformat:%s -E \\\n \t\t\t--grep=\"\\|2\" >actual.extended.short-arg &&\n+\t\tif test_have_prereq PCRE\n+\t\tthen\n+\t\t\tgit log --pretty=tformat:%s -P \\\n+\t\t\t\t--grep=\"[\\d]\\|\" >actual.perl.short-arg\n+\t\telse\n+\t\t\ttest_must_fail git log -P \\\n+\t\t\t\t--grep=\"[\\d]\\|\"\n+\t\tfi &&\n \t\ttest_cmp expect.fixed actual.fixed.short-arg &&\n \t\ttest_cmp expect.extended actual.extended.short-arg &&\n+\t\tif test_have_prereq PCRE\n+\t\tthen\n+\t\t\ttest_cmp expect.perl actual.perl.short-arg\n+\t\tfi &&\n \n \t\tgit log --pretty=tformat:%s --fixed-strings \\\n \t\t\t--grep=\"(1|2)\" >actual.fixed.long-arg &&\n-- \n2.11.0\n\n"},{"id":"319418","messageId":"20170511170142.15934-5-avarab@gmail.com","threadId":"45935","inReplyTo":"20170511170142.15934-1-avarab@gmail.com","subject":"[PATCH 4/7] grep: add support for the PCRE v1 JIT API","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-05-11T17:01:39Z","receivedAt":"2017-05-11T17:02:37Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Change the grep PCRE v1 code to use JIT when available. When PCRE\nsupport was initially added in commit 63e7e9d8b6 (\"git-grep: Learn\nPCRE\", 2011-05-09) PCRE had no JIT support, it was integrated into\n8.20 released on 2011-10-21.\n\nEnabling JIT support usually improves performance by more than\n40%. The pattern compilation times are relatively slower, but those\nrelative numbers are tiny, and are easily made back in all but the\nmost trivial cases of grep. Detailed benchmarks & overview of\ncompilation times is at: http://sljit.sourceforge.net/pcre.html\n\nWith this change the difference in a t/perf/p7820-grep-engines.sh run\nis, with just the /perl/ tests shown:\n\n    $ GIT_PERF_REPEAT_COUNT=30 GIT_PERF_LARGE_REPO=~/g/linux GIT_PERF_MAKE_OPTS='-j8 USE_LIBPCRE=YesPlease CC=~/perl5/installed/bin/gcc NO_R_TO_GCC_LINKER=YesPlease CFLAGS=-O3 LIBPCREDIR=/home/avar/g/pcre/inst LDFLAGS=-Wl,-rpath,/home/avar/g/pcre/inst/lib' ./run HEAD~ HEAD p7820-grep-engines.sh\n    Test                                           HEAD~             HEAD\n    ---------------------------------------------------------------------------------------\n    7820.3: perl grep how.to                       0.31(1.11+0.44)   0.20(0.35+0.57) -35.5%\n    7820.7: perl grep ^how to                      0.57(2.66+0.38)   0.23(0.65+0.46) -59.6%\n    7820.11: perl grep [how] to                    0.55(2.54+0.43)   0.29(0.86+0.52) -47.3%\n    7820.15: perl grep (e.t[^ ]*|v.ry) rare        1.05(5.54+0.33)   0.30(1.10+0.44) -71.4%\n    7820.19: perl grep m(ú|u)ult.b(æ|y)te          0.37(1.53+0.43)   0.24(0.70+0.47) -35.1%\n\nThe conditional support for JIT is implemented as suggested in the\npcrejit(3) man page. E.g. defining PCRE_STUDY_JIT_COMPILE to 0 if it's\nnot present.\n\nThe implementation is relatively verbose because even if\nPCRE_CONFIG_JIT is defined only a call to pcre_config() can determine\nif the JIT is available, and if so the faster pcre_jit_exec() function\nshould be called instead of pcre_exec(), and a different (but not\ncomplimentary!) function needs to be called to free pcre1_extra_info.\n\nThere's no graceful fallback if pcre_jit_stack_alloc() fails under\nPCRE_CONFIG_JIT, instead the program will simply abort. I don't think\nthis is worth handling gracefully, it'll only fail in cases where\nmalloc() doesn't work, in which case we're screwed anyway.\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n grep.c | 37 ++++++++++++++++++++++++++++++++++++-\n grep.h |  6 ++++++\n 2 files changed, 42 insertions(+), 1 deletion(-)\n\ndiff --git a/grep.c b/grep.c\nindex 5c808f8971..593e72f92a 100644\n--- a/grep.c\n+++ b/grep.c\n@@ -350,6 +350,9 @@ static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)\n \tconst char *error;\n \tint erroffset;\n \tint options = PCRE_MULTILINE;\n+#ifdef PCRE_CONFIG_JIT\n+\tint canjit;\n+#endif\n \n \tif (opt->ignore_case) {\n \t\tif (has_non_ascii(p->pattern))\n@@ -364,9 +367,20 @@ static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)\n \tif (!p->pcre1_regexp)\n \t\tcompile_regexp_failed(p, error);\n \n-\tp->pcre1_extra_info = pcre_study(p->pcre1_regexp, 0, &error);\n+\tp->pcre1_extra_info = pcre_study(p->pcre1_regexp, PCRE_STUDY_JIT_COMPILE, &error);\n \tif (!p->pcre1_extra_info && error)\n \t\tdie(\"%s\", error);\n+\n+#ifdef PCRE_CONFIG_JIT\n+\tpcre_config(PCRE_CONFIG_JIT, &canjit);\n+\tif (canjit == 1) {\n+\t\tp->pcre1_jit_stack = pcre_jit_stack_alloc(1, 1024 * 1024);\n+\t\tif (!p->pcre1_jit_stack)\n+\t\t\tdie(\"BUG: Couldn't allocate PCRE JIT stack\");\n+\t\tpcre_assign_jit_stack(p->pcre1_extra_info, NULL, p->pcre1_jit_stack);\n+\t\tp->pcre1_jit_on = 1;\n+\t}\n+#endif\n }\n \n static int pcre1match(struct grep_pat *p, const char *line, const char *eol,\n@@ -377,8 +391,20 @@ static int pcre1match(struct grep_pat *p, const char *line, const char *eol,\n \tif (eflags & REG_NOTBOL)\n \t\tflags |= PCRE_NOTBOL;\n \n+#ifdef PCRE_CONFIG_JIT\n+\tif (p->pcre1_jit_on)\n+\t\tret = pcre_jit_exec(p->pcre1_regexp, p->pcre1_extra_info, line,\n+\t\t\t\t    eol - line, 0, flags, ovector,\n+\t\t\t\t    ARRAY_SIZE(ovector), p->pcre1_jit_stack);\n+\telse\n+\t\tret = pcre_exec(p->pcre1_regexp, p->pcre1_extra_info, line,\n+\t\t\t\teol - line, 0, flags, ovector,\n+\t\t\t\tARRAY_SIZE(ovector));\n+#else\n \tret = pcre_exec(p->pcre1_regexp, p->pcre1_extra_info, line, eol - line,\n \t\t\t0, flags, ovector, ARRAY_SIZE(ovector));\n+#endif\n+\n \tif (ret < 0 && ret != PCRE_ERROR_NOMATCH)\n \t\tdie(\"pcre_exec failed with error code %d\", ret);\n \tif (ret > 0) {\n@@ -393,7 +419,16 @@ static int pcre1match(struct grep_pat *p, const char *line, const char *eol,\n static void free_pcre1_regexp(struct grep_pat *p)\n {\n \tpcre_free(p->pcre1_regexp);\n+#ifdef PCRE_CONFIG_JIT\n+\tif (p->pcre1_jit_on) {\n+\t\tpcre_free_study(p->pcre1_extra_info);\n+\t\tpcre_jit_stack_free(p->pcre1_jit_stack);\n+\t} else {\n+\t\tpcre_free(p->pcre1_extra_info);\n+\t}\n+#else\n \tpcre_free(p->pcre1_extra_info);\n+#endif\n \tpcre_free((void *)p->pcre1_tables);\n }\n #else /* !USE_LIBPCRE1 */\ndiff --git a/grep.h b/grep.h\nindex 38ac82b638..14f47189f9 100644\n--- a/grep.h\n+++ b/grep.h\n@@ -3,9 +3,13 @@\n #include \"color.h\"\n #ifdef USE_LIBPCRE1\n #include <pcre.h>\n+#ifndef PCRE_STUDY_JIT_COMPILE\n+#define PCRE_STUDY_JIT_COMPILE 0\n+#endif\n #else\n typedef int pcre;\n typedef int pcre_extra;\n+typedef int pcre_jit_stack;\n #endif\n #include \"kwset.h\"\n #include \"thread-utils.h\"\n@@ -48,7 +52,9 @@ struct grep_pat {\n \tregex_t regexp;\n \tpcre *pcre1_regexp;\n \tpcre_extra *pcre1_extra_info;\n+\tpcre_jit_stack *pcre1_jit_stack;\n \tconst unsigned char *pcre1_tables;\n+\tint pcre1_jit_on;\n \tkwset_t kws;\n \tunsigned fixed:1;\n \tunsigned ignore_case:1;\n-- \n2.11.0\n\n"},{"id":"319419","messageId":"20170511170142.15934-6-avarab@gmail.com","threadId":"45935","inReplyTo":"20170511170142.15934-1-avarab@gmail.com","subject":"[PATCH 5/7] grep: un-break building with PCRE < 8.32","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-05-11T17:01:40Z","receivedAt":"2017-05-11T17:02:41Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Amend my change earlier in this series (\"grep: add support for the\nPCRE v1 JIT API\", 2017-04-11) to un-break the build on PCRE v1\nversions earlier than 8.32.\n\nThe JIT support was added in version 8.20 released on 2011-10-21, but\nit wasn't until 8.32 released on 2012-11-30 that the fast code path to\nuse the JIT via pcre_jit_exec() was added[1] (see also [2]).\n\nThis means that versions 8.20 through 8.31 could still use the JIT,\nbut supporting it on those versions would add to the already verbose\nmacro soup around JIT support it, and I don't expect that the use-case\nof compiling a brand new git against a 5 year old PCRE is particularly\ncommon, and if someone does that they can just get the existing\npre-JIT slow codepath.\n\nSo just take the easy way out and disable the JIT on any version older\nthan 8.32.\n\nThe reason this change isn't part of the initial change PCRE JIT\nsupport is because possibly slightly annoying someone who's bisecting\nwith an ancient PCRE is worth it to have a cleaner history showing\nwhich parts of the implementation are only used for ancient PCRE\nversions. This also makes it easier to revert this change if we ever\ndecide to stop supporting those old versions.\n\n1. http://www.pcre.org/original/changelog.txt (\"28. Introducing a\n   native interface for JIT. Through this interface, the\n   compiled[...]\")\n2. https://bugs.exim.org/show_bug.cgi?id=2121\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n grep.c | 8 ++++----\n grep.h | 5 +++++\n 2 files changed, 9 insertions(+), 4 deletions(-)\n\ndiff --git a/grep.c b/grep.c\nindex 593e72f92a..9ba9aab1a9 100644\n--- a/grep.c\n+++ b/grep.c\n@@ -350,7 +350,7 @@ static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)\n \tconst char *error;\n \tint erroffset;\n \tint options = PCRE_MULTILINE;\n-#ifdef PCRE_CONFIG_JIT\n+#ifdef GIT_PCRE1_CAN_DO_MODERN_JIT\n \tint canjit;\n #endif\n \n@@ -371,7 +371,7 @@ static void compile_pcre1_regexp(struct grep_pat *p, const struct grep_opt *opt)\n \tif (!p->pcre1_extra_info && error)\n \t\tdie(\"%s\", error);\n \n-#ifdef PCRE_CONFIG_JIT\n+#ifdef GIT_PCRE1_CAN_DO_MODERN_JIT\n \tpcre_config(PCRE_CONFIG_JIT, &canjit);\n \tif (canjit == 1) {\n \t\tp->pcre1_jit_stack = pcre_jit_stack_alloc(1, 1024 * 1024);\n@@ -391,7 +391,7 @@ static int pcre1match(struct grep_pat *p, const char *line, const char *eol,\n \tif (eflags & REG_NOTBOL)\n \t\tflags |= PCRE_NOTBOL;\n \n-#ifdef PCRE_CONFIG_JIT\n+#ifdef GIT_PCRE1_CAN_DO_MODERN_JIT\n \tif (p->pcre1_jit_on)\n \t\tret = pcre_jit_exec(p->pcre1_regexp, p->pcre1_extra_info, line,\n \t\t\t\t    eol - line, 0, flags, ovector,\n@@ -419,7 +419,7 @@ static int pcre1match(struct grep_pat *p, const char *line, const char *eol,\n static void free_pcre1_regexp(struct grep_pat *p)\n {\n \tpcre_free(p->pcre1_regexp);\n-#ifdef PCRE_CONFIG_JIT\n+#ifdef GIT_PCRE1_CAN_DO_MODERN_JIT\n \tif (p->pcre1_jit_on) {\n \t\tpcre_free_study(p->pcre1_extra_info);\n \t\tpcre_jit_stack_free(p->pcre1_jit_stack);\ndiff --git a/grep.h b/grep.h\nindex 14f47189f9..73ef0ef8ec 100644\n--- a/grep.h\n+++ b/grep.h\n@@ -3,6 +3,11 @@\n #include \"color.h\"\n #ifdef USE_LIBPCRE1\n #include <pcre.h>\n+#ifdef PCRE_CONFIG_JIT\n+#if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32\n+#define GIT_PCRE1_CAN_DO_MODERN_JIT\n+#endif\n+#endif\n #ifndef PCRE_STUDY_JIT_COMPILE\n #define PCRE_STUDY_JIT_COMPILE 0\n #endif\n-- \n2.11.0\n\n"},{"id":"319420","messageId":"20170511170142.15934-7-avarab@gmail.com","threadId":"45935","inReplyTo":"20170511170142.15934-1-avarab@gmail.com","subject":"[PATCH 6/7] grep: un-break building with PCRE < 8.20","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-05-11T17:01:41Z","receivedAt":"2017-05-11T17:02:44Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Amend my change earlier in this series (\"grep: add support for the\nPCRE v1 JIT API\", 2017-04-11) to un-break the build on PCRE v1\nversions earlier than 8.20.\n\nThe 8.20 release was the first release to have JIT & pcre_jit_stack in\nthe headers, so a mock type needs to be provided for it on those\nreleases.\n\nNow git should compile with all PCRE versions that it supported before\nmy JIT change.\n\nI've tested it as far back as version 7.5 released on 2008-01-10, once\nI got down to version 7.0 it wouldn't build anymore with GCC 7.1.1,\nand I couldn't be bothered to anything older than 7.5 as I'm confident\nthat if the build breaks on those older versions it's not because of\nmy JIT change.\n\nSee the \"un-break\" change in this series (\"grep: un-break building\nwith PCRE < 8.32\", 2017-05-10) for why this isn't squashed into the\nmain PCRE JIT commit.\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n grep.h | 3 +++\n 1 file changed, 3 insertions(+)\n\ndiff --git a/grep.h b/grep.h\nindex 73ef0ef8ec..b7b9d487b0 100644\n--- a/grep.h\n+++ b/grep.h\n@@ -11,6 +11,9 @@\n #ifndef PCRE_STUDY_JIT_COMPILE\n #define PCRE_STUDY_JIT_COMPILE 0\n #endif\n+#if PCRE_MAJOR <= 8 && PCRE_MINOR < 20\n+typedef int pcre_jit_stack;\n+#endif\n #else\n typedef int pcre;\n typedef int pcre_extra;\n-- \n2.11.0\n\n"},{"id":"319421","messageId":"20170511170142.15934-8-avarab@gmail.com","threadId":"45935","inReplyTo":"20170511170142.15934-1-avarab@gmail.com","subject":"[PATCH 7/7] grep: add support for PCRE v2","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-05-11T17:01:42Z","receivedAt":"2017-05-11T17:02:48Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Add support for v2 of the PCRE API. This is a new major version of\nPCRE that came out in early 2015[1].\n\nThe regular expression syntax is the same, but while the API is\nsimilar, pretty much every function is either renamed or takes\ndifferent arguments. Thus using it via entirely new functions makes\nsense, as opposed to trying to e.g. have one compile_pcre_pattern()\nthat would call either PCRE v1 or v2 functions.\n\nGit can now be compiled with either USE_LIBPCRE1=YesPlease or\nUSE_LIBPCRE2=YesPlease, with USE_LIBPCRE=YesPlease currently being a\nsynonym for the former. Providing both is a compile-time error.\n\nWith earlier patches to enable JIT for PCRE v1 the performance of the\nrelease versions of both libraries is almost exactly the same, with\nPCRE v2 being around 1% slower.\n\nHowever after I reported this to the pcre-dev mailing list[2] I got a\nlot of help with the API use from Zoltán Herczeg, he subsequently\noptimized some of the JIT functionality in v2 of the library.\n\nRunning the p7820-grep-engines.sh performance test against the latest\nSubversion trunk of both, with both them and git compiled as -O3, and\nthe test run against linux.git, gives the following results. Just the\n/perl/ tests shown:\n\n    $ GIT_PERF_REPEAT_COUNT=30 GIT_PERF_LARGE_REPO=~/g/linux GIT_PERF_MAKE_COMMAND='grep -q LIBPCRE2 Makefile && make -j8 USE_LIBPCRE2=YesPlease CC=~/perl5/installed/bin/gcc NO_R_TO_GCC_LINKER=YesPlease CFLAGS=-O3 LIBPCREDIR=/home/avar/g/pcre2/inst LDFLAGS=-Wl,-rpath,/home/avar/g/pcre2/inst/lib || make -j8 USE_LIBPCRE=YesPlease CC=~/perl5/installed/bin/gcc NO_R_TO_GCC_LINKER=YesPlease CFLAGS=-O3 LIBPCREDIR=/home/avar/g/pcre/inst LDFLAGS=-Wl,-rpath,/home/avar/g/pcre/inst/lib' ./run HEAD~2 HEAD~ HEAD p7820-grep-engines.sh\n    [...]\n    Test                                           HEAD~2            HEAD~                    HEAD\n    ----------------------------------------------------------------------------------------------------------------\n    7820.2: perl grep how.to                       0.30(1.07+0.48)   0.20(0.36+0.54) -33.3%   0.20(0.26+0.60) -33.3%\n    7820.5: perl grep ^how to                      0.56(2.62+0.42)   0.25(0.61+0.48) -55.4%   0.19(0.27+0.61) -66.1%\n    7820.8: perl grep [how] to                     0.57(2.54+0.41)   0.29(0.95+0.43) -49.1%   0.22(0.36+0.58) -61.4%\n    7820.11: perl grep (e.t[^ ]*|v.ry) rare        1.03(5.57+0.34)   0.30(1.07+0.47) -70.9%   0.23(0.54+0.44) -77.7%\n    7820.14: perl grep m(ú|u)ult.b(æ|y)te          0.37(1.60+0.37)   0.25(0.73+0.44) -32.4%   0.20(0.29+0.55) -45.9%\n\nSee commit (\"perf: add a performance comparison test of grep -G, -E\nand -P\", 2017-04-19) for further details on the machine the above test\nrun was executed on.\n\nHere HEAD~2 is git with PCRE v1 without JIT, HEAD~ is PCRE v1 with\nJIT, and HEAD is PCRE v2 (also with JIT). See previous commits of mine\nmentioning p7820-grep-engines.sh for more details on the test setup.\n\nFor ease of readability, a different run just of HEAD~ (PCRE v1 with\nJIT v.s. PCRE v2), again with just the /perl/ tests shown:\n\n    Test                                           HEAD~             HEAD\n    ---------------------------------------------------------------------------------------\n    7820.2: perl grep how.to                       0.20(0.40+0.48)   0.20(0.31+0.55) +0.0%\n    7820.5: perl grep ^how to                      0.24(0.62+0.50)   0.19(0.30+0.58) -20.8%\n    7820.8: perl grep [how] to                     0.29(0.98+0.39)   0.22(0.35+0.61) -24.1%\n    7820.11: perl grep (e.t[^ ]*|v.ry) rare        0.30(1.16+0.38)   0.22(0.50+0.50) -26.7%\n    7820.14: perl grep m(ú|u)ult.b(æ|y)te          0.24(0.74+0.43)   0.20(0.31+0.54) -16.7%\n\nI.e. the two are either neck-to-neck, but PCRE v2 usually pulls ahead,\nwhen it does it's around 20% faster.\n\nA brief note on thread safety: As noted in pcre2api(3) & pcre2jit(3)\nthe compiled pattern can be shared between threads, but not some of\nthe JIT context, however the grep threading support does all pattern &\nJIT compilation in separate threads, so this code doesn't need to\nconcern itself with thread safety.\n\nSee commit 63e7e9d8b6 (\"git-grep: Learn PCRE\", 2011-05-09) for the\ninitial addition of PCRE v1. This change follows some of the same\npatterns it did (and which were discussed on list at the time),\ne.g. mocking up types with typedef instead of ifdef-ing them out when\nUSE_LIBPCRE2 isn't defined. This adds some trivial memory use to the\nprogram, but makes the code look nicer.\n\n1. https://lists.exim.org/lurker/message/20150105.162835.0666407a.en.html\n2. https://lists.exim.org/lurker/thread/20170419.172322.833ee099.en.html\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n Makefile      |  30 +++++++++---\n configure.ac  |  77 ++++++++++++++++++++++++++-----\n grep.c        | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n grep.h        |  17 +++++++\n t/test-lib.sh |   2 +-\n 5 files changed, 250 insertions(+), 19 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex a79274e5e6..d77ca4c1a5 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -29,7 +29,12 @@ all::\n # Perl-compatible regular expressions instead of standard or extended\n # POSIX regular expressions.\n #\n-# Define LIBPCREDIR=/foo/bar if your libpcre header and library files are in\n+# Currently USE_LIBPCRE is a synonym for USE_LIBPCRE1, define\n+# USE_LIBPCRE2 instead if you'd like to use version 2 of the PCRE\n+# library. The USE_LIBPCRE flag will likely be changed to mean v2 by\n+# default in future releases.\n+#\n+# Define LIBPCREDIR=/foo/bar if your PCRE header and library files are in\n # /foo/bar/include and /foo/bar/lib directories.\n #\n # Define HAVE_ALLOCA_H if you have working alloca(3) defined in that header.\n@@ -1087,15 +1092,27 @@ ifdef NO_LIBGEN_H\n \tCOMPAT_OBJS += compat/basename.o\n endif\n \n-ifdef USE_LIBPCRE\n-\tBASIC_CFLAGS += -DUSE_LIBPCRE1\n-\tifdef LIBPCREDIR\n-\t\tBASIC_CFLAGS += -I$(LIBPCREDIR)/include\n-\t\tEXTLIBS += -L$(LIBPCREDIR)/$(lib) $(CC_LD_DYNPATH)$(LIBPCREDIR)/$(lib)\n+USE_LIBPCRE1 ?= $(USE_LIBPCRE)\n+\n+ifneq (,$(USE_LIBPCRE1))\n+\tifdef USE_LIBPCRE2\n+$(error Only set USE_LIBPCRE1 (or its alias USE_LIBPCRE) or USE_LIBPCRE2, not both!)\n \tendif\n+\n+\tBASIC_CFLAGS += -DUSE_LIBPCRE1\n \tEXTLIBS += -lpcre\n endif\n \n+ifdef USE_LIBPCRE2\n+\tBASIC_CFLAGS += -DUSE_LIBPCRE2\n+\tEXTLIBS += -lpcre2-8\n+endif\n+\n+ifdef LIBPCREDIR\n+\tBASIC_CFLAGS += -I$(LIBPCREDIR)/include\n+\tEXTLIBS += -L$(LIBPCREDIR)/$(lib) $(CC_LD_DYNPATH)$(LIBPCREDIR)/$(lib)\n+endif\n+\n ifdef HAVE_ALLOCA_H\n \tBASIC_CFLAGS += -DHAVE_ALLOCA_H\n endif\n@@ -2241,6 +2258,7 @@ GIT-BUILD-OPTIONS: FORCE\n \t@echo NO_CURL=\\''$(subst ','\\'',$(subst ','\\'',$(NO_CURL)))'\\' >>$@+\n \t@echo NO_EXPAT=\\''$(subst ','\\'',$(subst ','\\'',$(NO_EXPAT)))'\\' >>$@+\n \t@echo USE_LIBPCRE1=\\''$(subst ','\\'',$(subst ','\\'',$(USE_LIBPCRE)))'\\' >>$@+\n+\t@echo USE_LIBPCRE2=\\''$(subst ','\\'',$(subst ','\\'',$(USE_LIBPCRE2)))'\\' >>$@+\n \t@echo NO_PERL=\\''$(subst ','\\'',$(subst ','\\'',$(NO_PERL)))'\\' >>$@+\n \t@echo NO_PTHREADS=\\''$(subst ','\\'',$(subst ','\\'',$(NO_PTHREADS)))'\\' >>$@+\n \t@echo NO_PYTHON=\\''$(subst ','\\'',$(subst ','\\'',$(NO_PYTHON)))'\\' >>$@+\ndiff --git a/configure.ac b/configure.ac\nindex deeb968daa..11d083fbe0 100644\n--- a/configure.ac\n+++ b/configure.ac\n@@ -255,21 +255,61 @@ GIT_PARSE_WITH([openssl]))\n # Perl-compatible regular expressions instead of standard or extended\n # POSIX regular expressions.\n #\n-# Define LIBPCREDIR=/foo/bar if your libpcre header and library files are in\n+# Currently USE_LIBPCRE is a synonym for USE_LIBPCRE1, define\n+# USE_LIBPCRE2 instead if you'd like to use version 2 of the PCRE\n+# library. The USE_LIBPCRE flag will likely be changed to mean v2 by\n+# default in future releases.\n+#\n+# Define LIBPCREDIR=/foo/bar if your PCRE header and library files are in\n # /foo/bar/include and /foo/bar/lib directories.\n #\n AC_ARG_WITH(libpcre,\n-AS_HELP_STRING([--with-libpcre],[support Perl-compatible regexes (default is NO)])\n+AS_HELP_STRING([--with-libpcre],[synonym for --with-libpcre1]),\n+    if test \"$withval\" = \"no\"; then\n+\tUSE_LIBPCRE1=\n+    elif test \"$withval\" = \"yes\"; then\n+\tUSE_LIBPCRE1=YesPlease\n+    else\n+\tUSE_LIBPCRE1=YesPlease\n+\tLIBPCREDIR=$withval\n+\tAC_MSG_NOTICE([Setting LIBPCREDIR to $LIBPCREDIR])\n+        dnl USE_LIBPCRE1 can still be modified below, so don't substitute\n+        dnl it yet.\n+\tGIT_CONF_SUBST([LIBPCREDIR])\n+    fi)\n+\n+AC_ARG_WITH(libpcre1,\n+AS_HELP_STRING([--with-libpcre1],[support Perl-compatible regexes via libpcre1 (default is NO)])\n+AS_HELP_STRING([],           [ARG can be also prefix for libpcre library and headers]),\n+    if test \"$withval\" = \"no\"; then\n+\tUSE_LIBPCRE1=\n+    elif test \"$withval\" = \"yes\"; then\n+\tUSE_LIBPCRE1=YesPlease\n+    else\n+\tUSE_LIBPCRE1=YesPlease\n+\tLIBPCREDIR=$withval\n+\tAC_MSG_NOTICE([Setting LIBPCREDIR to $LIBPCREDIR])\n+        dnl USE_LIBPCRE1 can still be modified below, so don't substitute\n+        dnl it yet.\n+\tGIT_CONF_SUBST([LIBPCREDIR])\n+    fi)\n+\n+AC_ARG_WITH(libpcre2,\n+AS_HELP_STRING([--with-libpcre2],[support Perl-compatible regexes via libpcre2 (default is NO)])\n AS_HELP_STRING([],           [ARG can be also prefix for libpcre library and headers]),\n+    if test -n \"$USE_LIBPCRE1\"; then\n+        AC_MSG_ERROR([Only supply one of --with-libpcre1 or --with-libpcre2!])\n+    fi\n+\n     if test \"$withval\" = \"no\"; then\n-\tUSE_LIBPCRE=\n+\tUSE_LIBPCRE2=\n     elif test \"$withval\" = \"yes\"; then\n-\tUSE_LIBPCRE=YesPlease\n+\tUSE_LIBPCRE2=YesPlease\n     else\n-\tUSE_LIBPCRE=YesPlease\n+\tUSE_LIBPCRE2=YesPlease\n \tLIBPCREDIR=$withval\n \tAC_MSG_NOTICE([Setting LIBPCREDIR to $LIBPCREDIR])\n-        dnl USE_LIBPCRE can still be modified below, so don't substitute\n+        dnl USE_LIBPCRE2 can still be modified below, so don't substitute\n         dnl it yet.\n \tGIT_CONF_SUBST([LIBPCREDIR])\n     fi)\n@@ -501,13 +541,11 @@ GIT_CONF_SUBST([NEEDS_SSL_WITH_CRYPTO])\n GIT_CONF_SUBST([NO_OPENSSL])\n \n #\n-# Define USE_LIBPCRE if you have and want to use libpcre. Various\n-# commands such as log and grep offer runtime options to use\n-# Perl-compatible regular expressions instead of standard or extended\n-# POSIX regular expressions.\n+# Handle the USE_LIBPCRE1 and USE_LIBPCRE2 options potentially set\n+# above.\n #\n \n-if test -n \"$USE_LIBPCRE\"; then\n+if test -n \"$USE_LIBPCRE1\"; then\n \n GIT_STASH_FLAGS($LIBPCREDIR)\n \n@@ -517,7 +555,22 @@ AC_CHECK_LIB([pcre], [pcre_version],\n \n GIT_UNSTASH_FLAGS($LIBPCREDIR)\n \n-GIT_CONF_SUBST([USE_LIBPCRE])\n+GIT_CONF_SUBST([USE_LIBPCRE1])\n+\n+fi\n+\n+\n+if test -n \"$USE_LIBPCRE2\"; then\n+\n+GIT_STASH_FLAGS($LIBPCREDIR)\n+\n+AC_CHECK_LIB([pcre2-8], [pcre2_config_8],\n+[USE_LIBPCRE2=YesPlease],\n+[USE_LIBPCRE2=])\n+\n+GIT_UNSTASH_FLAGS($LIBPCREDIR)\n+\n+GIT_CONF_SUBST([USE_LIBPCRE2])\n \n fi\n \ndiff --git a/grep.c b/grep.c\nindex 9ba9aab1a9..3855428dfb 100644\n--- a/grep.c\n+++ b/grep.c\n@@ -179,22 +179,36 @@ static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, st\n \tcase GREP_PATTERN_TYPE_BRE:\n \t\topt->fixed = 0;\n \t\topt->pcre1 = 0;\n+\t\topt->pcre2 = 0;\n \t\tbreak;\n \n \tcase GREP_PATTERN_TYPE_ERE:\n \t\topt->fixed = 0;\n \t\topt->pcre1 = 0;\n+\t\topt->pcre2 = 0;\n \t\topt->regflags |= REG_EXTENDED;\n \t\tbreak;\n \n \tcase GREP_PATTERN_TYPE_FIXED:\n \t\topt->fixed = 1;\n \t\topt->pcre1 = 0;\n+\t\topt->pcre2 = 0;\n \t\tbreak;\n \n \tcase GREP_PATTERN_TYPE_PCRE:\n \t\topt->fixed = 0;\n+#ifdef USE_LIBPCRE2\n+\t\topt->pcre1 = 0;\n+\t\topt->pcre2 = 1;\n+#else\n+\t\t/* It's important that pcre1 always be assigned to\n+\t\t * even when there's no USE_LIBPCRE* defined. We still\n+\t\t * call the PCRE stub function, it just dies with\n+\t\t * \"cannot use Perl-compatible regexes[...]\".\n+\t\t */\n \t\topt->pcre1 = 1;\n+\t\topt->pcre2 = 0;\n+#endif\n \t\tbreak;\n \t}\n }\n@@ -448,6 +462,126 @@ static void free_pcre1_regexp(struct grep_pat *p)\n }\n #endif /* !USE_LIBPCRE1 */\n \n+#ifdef USE_LIBPCRE2\n+static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)\n+{\n+\tint error;\n+\tPCRE2_UCHAR errbuf[256];\n+\tPCRE2_SIZE erroffset;\n+\tint options = PCRE2_MULTILINE;\n+\tconst uint8_t *character_tables = NULL;\n+\tuint32_t canjit;\n+\tint jitret;\n+\n+\tassert(opt->pcre2);\n+\n+\tp->pcre2_compile_context = NULL;\n+\n+\tif (opt->ignore_case) {\n+\t\tif (has_non_ascii(p->pattern)) {\n+\t\t\tcharacter_tables = pcre2_maketables(NULL);\n+\t\t\tp->pcre2_compile_context = pcre2_compile_context_create(NULL);\n+\t\t\tpcre2_set_character_tables(p->pcre2_compile_context, character_tables);\n+\t\t}\n+\t\toptions |= PCRE2_CASELESS;\n+\t}\n+\tif (is_utf8_locale() && has_non_ascii(p->pattern))\n+\t\toptions |= PCRE2_UTF;\n+\n+\tp->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,\n+\t\t\t\t\t p->patternlen, options, &error, &erroffset,\n+\t\t\t\t\t p->pcre2_compile_context);\n+\n+\tif (p->pcre2_pattern) {\n+\t\tp->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, NULL);\n+\t\tif (!p->pcre2_match_data)\n+\t\t\tdie(\"BUG: Couldn't allocate PCRE2 match data\");\n+\t} else {\n+\t\tpcre2_get_error_message(error, errbuf, sizeof(errbuf));\n+\t\tcompile_regexp_failed(p, (const char *)&errbuf);\n+\t}\n+\n+\tpcre2_config(PCRE2_CONFIG_JIT, &canjit);\n+\tif (canjit == 1) {\n+\t\tjitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE);\n+\t\tif (!jitret)\n+\t\t\tp->pcre2_jit_on = 1;\n+\t\telse\n+\t\t\tdie(\"BUG: Couldn't JIT the PCRE2 pattern '%s', got '%d'\\n\", p->pattern, jitret);\n+\t\tp->pcre2_jit_stack = pcre2_jit_stack_create(1, 1024 * 1024, NULL);\n+\t\tif (!p->pcre2_jit_stack)\n+\t\t\tdie(\"BUG: Couldn't allocate PCRE2 JIT stack\");\n+\t\tp->pcre2_match_context = pcre2_match_context_create(NULL);\n+\t\tif (!p->pcre2_jit_stack)\n+\t\t\tdie(\"BUG: Couldn't allocate PCRE2 match context\");\n+\t\tpcre2_jit_stack_assign(p->pcre2_match_context, NULL, p->pcre2_jit_stack);\n+\t}\n+}\n+\n+static int pcre2match(struct grep_pat *p, const char *line, const char *eol,\n+\t\tregmatch_t *match, int eflags)\n+{\n+\tint ret, flags = 0;\n+\tPCRE2_SIZE *ovector;\n+\tPCRE2_UCHAR errbuf[256];\n+\n+\tif (eflags & REG_NOTBOL)\n+\t\tflags |= PCRE2_NOTBOL;\n+\n+\tif (p->pcre2_jit_on)\n+\t\tret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line,\n+\t\t\t\t      eol - line, 0, flags, p->pcre2_match_data,\n+\t\t\t\t      NULL);\n+\telse\n+\t\tret = pcre2_match(p->pcre2_pattern, (unsigned char *)line,\n+\t\t\t\t  eol - line, 0, flags, p->pcre2_match_data,\n+\t\t\t\t  NULL);\n+\n+\tif (ret < 0 && ret != PCRE2_ERROR_NOMATCH) {\n+\t\tpcre2_get_error_message(ret, errbuf, sizeof(errbuf));\n+\t\tdie(\"%s failed with error code %d: %s\",\n+\t\t    (p->pcre2_jit_on ? \"pcre2_jit_match\" : \"pcre2_match\"), ret,\n+\t\t    errbuf);\n+\t}\n+\tif (ret > 0) {\n+\t\tovector = pcre2_get_ovector_pointer(p->pcre2_match_data);\n+\t\tret = 0;\n+\t\tmatch->rm_so = (int)ovector[0];\n+\t\tmatch->rm_eo = (int)ovector[1];\n+\t}\n+\n+\treturn ret;\n+}\n+\n+static void free_pcre2_pattern(struct grep_pat *p)\n+{\n+\tpcre2_compile_context_free(p->pcre2_compile_context);\n+\tpcre2_code_free(p->pcre2_pattern);\n+\tpcre2_match_data_free(p->pcre2_match_data);\n+\tpcre2_jit_stack_free(p->pcre2_jit_stack);\n+\tpcre2_match_context_free(p->pcre2_match_context);\n+}\n+#else /* !USE_LIBPCRE2 */\n+static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)\n+{\n+\t/* Unreachable until USE_LIBPCRE2 becomes synonymous with\n+\t * USE_LIBPCRE. See the sibling comment in\n+\t * grep_set_pattern_type_option().\n+\t */\n+\tdie(\"cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE\");\n+}\n+\n+static int pcre2match(struct grep_pat *p, const char *line, const char *eol,\n+\t\tregmatch_t *match, int eflags)\n+{\n+\treturn 1;\n+}\n+\n+static void free_pcre2_pattern(struct grep_pat *p)\n+{\n+}\n+#endif /* !USE_LIBPCRE2 */\n+\n static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)\n {\n \tstruct strbuf sb = STRBUF_INIT;\n@@ -511,6 +645,11 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)\n \t\treturn;\n \t}\n \n+\tif (opt->pcre2) {\n+\t\tcompile_pcre2_pattern(p, opt);\n+\t\treturn;\n+\t}\n+\n \tif (opt->pcre1) {\n \t\tcompile_pcre1_regexp(p, opt);\n \t\treturn;\n@@ -870,6 +1009,8 @@ void free_grep_patterns(struct grep_opt *opt)\n \t\t\t\tkwsfree(p->kws);\n \t\t\telse if (p->pcre1_regexp)\n \t\t\t\tfree_pcre1_regexp(p);\n+\t\t\telse if (p->pcre2_pattern)\n+\t\t\t\tfree_pcre2_pattern(p);\n \t\t\telse\n \t\t\t\tregfree(&p->regexp);\n \t\t\tfree(p->pattern);\n@@ -950,6 +1091,8 @@ static int patmatch(struct grep_pat *p, char *line, char *eol,\n \t\thit = !fixmatch(p, line, eol, match);\n \telse if (p->pcre1_regexp)\n \t\thit = !pcre1match(p, line, eol, match, eflags);\n+\telse if (p->pcre2_pattern)\n+\t\thit = !pcre2match(p, line, eol, match, eflags);\n \telse\n \t\thit = !regexec_buf(&p->regexp, line, eol - line, 1, match,\n \t\t\t\t   eflags);\ndiff --git a/grep.h b/grep.h\nindex b7b9d487b0..b40afc2e2f 100644\n--- a/grep.h\n+++ b/grep.h\n@@ -19,6 +19,16 @@ typedef int pcre;\n typedef int pcre_extra;\n typedef int pcre_jit_stack;\n #endif\n+#ifdef USE_LIBPCRE2\n+#define PCRE2_CODE_UNIT_WIDTH 8\n+#include <pcre2.h>\n+#else\n+typedef int pcre2_code;\n+typedef int pcre2_match_data;\n+typedef int pcre2_compile_context;\n+typedef int pcre2_match_context;\n+typedef int pcre2_jit_stack;\n+#endif\n #include \"kwset.h\"\n #include \"thread-utils.h\"\n #include \"userdiff.h\"\n@@ -63,6 +73,12 @@ struct grep_pat {\n \tpcre_jit_stack *pcre1_jit_stack;\n \tconst unsigned char *pcre1_tables;\n \tint pcre1_jit_on;\n+\tpcre2_code *pcre2_pattern;\n+\tpcre2_match_data *pcre2_match_data;\n+\tpcre2_compile_context *pcre2_compile_context;\n+\tpcre2_match_context *pcre2_match_context;\n+\tpcre2_jit_stack *pcre2_jit_stack;\n+\tint pcre2_jit_on;\n \tkwset_t kws;\n \tunsigned fixed:1;\n \tunsigned ignore_case:1;\n@@ -126,6 +142,7 @@ struct grep_opt {\n \tint extended;\n \tint use_reflog_filter;\n \tint pcre1;\n+\tint pcre2;\n \tint relative;\n \tint pathname;\n \tint null_following_name;\ndiff --git a/t/test-lib.sh b/t/test-lib.sh\nindex ab92c0ebaa..44d4679384 100644\n--- a/t/test-lib.sh\n+++ b/t/test-lib.sh\n@@ -1011,7 +1011,7 @@ esac\n test -z \"$NO_PERL\" && test_set_prereq PERL\n test -z \"$NO_PTHREADS\" && test_set_prereq PTHREADS\n test -z \"$NO_PYTHON\" && test_set_prereq PYTHON\n-test -n \"$USE_LIBPCRE1\" && test_set_prereq PCRE\n+test -n \"$USE_LIBPCRE1$USE_LIBPCRE2\" && test_set_prereq PCRE\n test -z \"$NO_GETTEXT\" && test_set_prereq GETTEXT\n \n # Can we rely on git's output in the C locale?\n-- \n2.11.0\n\n"},{"id":"319550","messageId":"CACBZZX7x=9Pvuhy7Q9=ecnhvVmjt664enZOjk4B8LXErVL4oEg@mail.gmail.com","threadId":"45935","inReplyTo":"20170512173527.GD98586@google.com","subject":"Re: [PATCH 1/7] grep: don't redundantly compile throwaway patterns under threading","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2017-05-12T18:17:48Z","receivedAt":"2017-05-12T18:46:03Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"On Fri, May 12, 2017 at 7:35 PM, Brandon Williams <bmwill@google.com> wrote:\n> On 05/11, Ęvar Arnfjörš Bjarmason wrote:\n>> Change the pattern compilation logic under threading so that grep\n>> doesn't compile a pattern it never ends up using on the non-threaded\n>> code path, only to compile it again N times for N threads which will\n>> each use their own copy, ignoring the initially compiled pattern.\n>\n> Is there a reason each thread needs to compile the patterns as opposed\n> to them being compiled a single time and being copies N time for N\n> threads?\n\nNot really, just simplicity.\n\nI'll amend the commit message to mention that I did this not as an\noptimization, but just so the code is easier to reason about and\ndebug, when debugging this I found that there were two different\nstacktraces to get to where I was compiling the pattern, and e.g.\n\"give each compilation/execution an id\" for ad-hoc debugging would\nalways end up with one unused pattern, confusingly.\n\nThe reason it's like this is because it's a side-effect of duplicating\nthe whole grep_opt structure, which is not thread safe, writable, and\nmunged during execution, that's where the pattern is stored.\n\nWe could re-use the compiled regexp itself for POSIX, for PCRE you can\npre-JIT, post-JIT you can only partially do it, i.e. the pattern is\nconst, thread safe and can be shared, but you need to also marshal\naround mutable per-thread for the JIT stack etc.\n\nI looked into e.g. splitting the API into some \"do & alloc threadsafe\nstuff\", \"spawn thread\", \"do and alloc non-threadsafe stuff\", but the\nexecution time of grep_opt_dup() & pattern compilation is trivial\ncompared to actually executing the grep, so there was no point. Even\nwith the more expensive JIT some of the most expensive PCRE patterns\ntake something like 0.0X milliseconds to compile at most[1].\n\n1. http://sljit.sourceforge.net/pcre.html\n"},{"id":"319558","messageId":"20170512173527.GD98586@google.com","threadId":"45935","inReplyTo":"20170511170142.15934-2-avarab@gmail.com","subject":"Re: [PATCH 1/7] grep: don't redundantly compile throwaway patterns under threading","fromName":"Brandon Williams","fromEmail":"bmwill@google.com","sentAt":"2017-05-12T17:35:27Z","receivedAt":"2017-05-12T18:46:16Z","isPatch":true,"sender":{"key":"bwilliams.eng@gmail.com","avatar":null},"body":"On 05/11, Ævar Arnfjörð Bjarmason wrote:\n> Change the pattern compilation logic under threading so that grep\n> doesn't compile a pattern it never ends up using on the non-threaded\n> code path, only to compile it again N times for N threads which will\n> each use their own copy, ignoring the initially compiled pattern.\n\nIs there a reason each thread needs to compile the patterns as opposed\nto them being compiled a single time and being copies N time for N\nthreads?\n\n-- \nBrandon Williams\n"}]}