From: Joel Jacobson <joel@compiler.org>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-hackers@lists.postgresql.org
Subject: Re: Some regular-expression performance hacking
Date: Sat, 20 Feb 2021 10:19:04 +0100
Message-ID: <7c8faebf-a9c3-4144-88cd-7ff81a1e8763@www.fastmail.com> (raw)
In-Reply-To: <3028106.1613748380@sss.pgh.pa.us>
References: <1340281.1613018383@sss.pgh.pa.us>
<85140bfb-516c-449b-8a03-7930abb42b88@www.fastmail.com>
<1813969.1613250697@sss.pgh.pa.us>
<913516b8-c3d7-4dea-bf60-d735cf4673f1@www.fastmail.com>
<2155152.1613358697@sss.pgh.pa.us>
<99f6cbe0-6e58-4e41-add0-977271a5cf84@www.fastmail.com>
<2752386.1613595636@sss.pgh.pa.us>
<5deabab3-a335-4b57-a046-8a9ad8f2f142@www.fastmail.com>
<12e6381c-adc1-4711-a619-864fb8df969c@www.fastmail.com>
<2840118.1613671847@sss.pgh.pa.us>
<2845172.1613674385@sss.pgh.pa.us>
<b16bccc0-3f98-47ad-81aa-699a3e00630d@www.fastmail.com>
<3028106.1613748380@sss.pgh.pa.us>
On Fri, Feb 19, 2021, at 16:26, Tom Lane wrote:
>"Joel Jacobson" <joel@compiler.org> writes:
>> On Thu, Feb 18, 2021, at 19:53, Tom Lane wrote:
>>> (Having said that, I can't help noticing that a very large fraction
>>> of those usages look like, eg, "[\w\W]". It seems to me that that's
>>> a very expensive and unwieldy way to spell ".". Am I missing
>>> something about what that does in Javascript?)
>
>> I think this is a non-POSIX hack to match any character, including newlines,
>> which are not included unless the "s" flag is set.
>
>> "foo\nbar".match(/([\w\W]+)/)[1];
>> "foo
>> bar"
>
>Oooh, that's very interesting. I guess the advantage of that over using
>the 's' flag is that you can have different behaviors at different places
>in the same regex.
I would guess the same thing.
>I was just wondering about this last night in fact, while hacking on
>the code to get it to accept \W etc in bracket expressions. I see that
>right now, our code thinks that NLSTOP mode ('n' switch, the opposite
>of 's') should cause \W \D \S to not match newline. That seems a little
>weird, not least because \S should probably be different from the other
>two, and it isn't. And now we see it'd mean that you couldn't use the 'n'
>switch to duplicate Javascript's default behavior in this area. Should we
>change it? (I wonder what Perl does.)
>
>regards, tom lane
To allow comparing PostgreSQL vs Javascript vs Perl,
I installed three helper-functions using plv8 and plperl,
and also one convenience function for PostgreSQL
to catch errors and return the error string instead:
The string used in this test is "foo!\n!bar",
which aims to detect differences in how new-lines
and non alpha-number characters are handled.
To allow PostgreSQL to be compared with Javascript and Perl,
the "n" flag is used for PostgreSQL when no flags are used for Javascript/Perl,
and no flag for PostgreSQL when the "s" flag is used for Javascript/Perl,
for the results to be comparable.
In Javascript, when a regex contains capture groups, the entire match
is always returns as the first array element.
To make it easier to visually compare the results,
the first element is removed from Javascript,
which works in this test since all regexes contain
exactly one capture group.
Here are the results:
$ psql -e -f not_alnum.sql regex
SELECT
regexp_match_pg(E'foo!\n!bar', '(.+)', 'n'),
(regexp_match_v8(E'foo!\n!bar', '(.+)', ''))[2:],
regexp_match_pl(E'foo!\n!bar', '(.+)', '')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
-----------------+-----------------+-----------------
{foo!} | {foo!} | {foo!}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '(.+)', ''),
(regexp_match_v8(E'foo!\n!bar', '(.+)', 's'))[2:],
regexp_match_pl(E'foo!\n!bar', '(.+)', 's')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
-----------------+-----------------+-----------------
{"foo! +| {"foo! +| {"foo! +
!bar"} | !bar"} | !bar"}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '([\w\W]+)', 'n'),
(regexp_match_v8(E'foo!\n!bar', '([\w\W]+)', ''))[2:],
regexp_match_pl(E'foo!\n!bar', '([\w\W]+)', '')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
------------------------------------------------------------+-----------------+-----------------
{"invalid regular expression: invalid escape \\ sequence"} | {"foo! +| {"foo! +
| !bar"} | !bar"}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '([\w\W]+)', ''),
(regexp_match_v8(E'foo!\n!bar', '([\w\W]+)', 's'))[2:],
regexp_match_pl(E'foo!\n!bar', '([\w\W]+)', 's')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
------------------------------------------------------------+-----------------+-----------------
{"invalid regular expression: invalid escape \\ sequence"} | {"foo! +| {"foo! +
| !bar"} | !bar"}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '([\w]+)', 'n'),
(regexp_match_v8(E'foo!\n!bar', '([\w]+)', ''))[2:],
regexp_match_pl(E'foo!\n!bar', '([\w]+)', '')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
-----------------+-----------------+-----------------
{foo} | {foo} | {foo}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '([\w]+)', ''),
(regexp_match_v8(E'foo!\n!bar', '([\w]+)', 's'))[2:],
regexp_match_pl(E'foo!\n!bar', '([\w]+)', 's')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
-----------------+-----------------+-----------------
{foo} | {foo} | {foo}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '([\W]+)', 'n'),
(regexp_match_v8(E'foo!\n!bar', '([\W]+)', ''))[2:],
regexp_match_pl(E'foo!\n!bar', '([\W]+)', '')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
------------------------------------------------------------+-----------------+-----------------
{"invalid regular expression: invalid escape \\ sequence"} | {"! +| {"! +
| !"} | !"}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '([\W]+)', ''),
(regexp_match_v8(E'foo!\n!bar', '([\W]+)', 's'))[2:],
regexp_match_pl(E'foo!\n!bar', '([\W]+)', 's')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
------------------------------------------------------------+-----------------+-----------------
{"invalid regular expression: invalid escape \\ sequence"} | {"! +| {"! +
| !"} | !"}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '(\w+)', 'n'),
(regexp_match_v8(E'foo!\n!bar', '(\w+)', ''))[2:],
regexp_match_pl(E'foo!\n!bar', '(\w+)', '')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
-----------------+-----------------+-----------------
{foo} | {foo} | {foo}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '(\w+)', ''),
(regexp_match_v8(E'foo!\n!bar', '(\w+)', 's'))[2:],
regexp_match_pl(E'foo!\n!bar', '(\w+)', 's')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
-----------------+-----------------+-----------------
{foo} | {foo} | {foo}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '(\W+)', 'n'),
(regexp_match_v8(E'foo!\n!bar', '(\W+)', ''))[2:],
regexp_match_pl(E'foo!\n!bar', '(\W+)', '')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
-----------------+-----------------+-----------------
{!} | {"! +| {"! +
| !"} | !"}
(1 row)
SELECT
regexp_match_pg(E'foo!\n!bar', '(\W+)', ''),
(regexp_match_v8(E'foo!\n!bar', '(\W+)', 's'))[2:],
regexp_match_pl(E'foo!\n!bar', '(\W+)', 's')
;
regexp_match_pg | regexp_match_v8 | regexp_match_pl
-----------------+-----------------+-----------------
{"! +| {"! +| {"! +
!"} | !"} | !"}
(1 row)
/Joel
Attachments:
not_alnum.sql (application/octet-stream, 2.6K) download
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: joel@compiler.org, tgl@sss.pgh.pa.us
Subject: Re: Some regular-expression performance hacking
In-Reply-To: <7c8faebf-a9c3-4144-88cd-7ff81a1e8763@www.fastmail.com>
References: <1340281.1613018383@sss.pgh.pa.us> <85140bfb-516c-449b-8a03-7930abb42b88@www.fastmail.com> <1813969.1613250697@sss.pgh.pa.us> <913516b8-c3d7-4dea-bf60-d735cf4673f1@www.fastmail.com> <2155152.1613358697@sss.pgh.pa.us> <99f6cbe0-6e58-4e41-add0-977271a5cf84@www.fastmail.com> <2752386.1613595636@sss.pgh.pa.us> <5deabab3-a335-4b57-a046-8a9ad8f2f142@www.fastmail.com> <12e6381c-adc1-4711-a619-864fb8df969c@www.fastmail.com> <2840118.1613671847@sss.pgh.pa.us> <2845172.1613674385@sss.pgh.pa.us> <b16bccc0-3f98-47ad-81aa-699a3e00630d@www.fastmail.com> <3028106.1613748380@sss.pgh.pa.us> <7c8faebf-a9c3-4144-88cd-7ff81a1e8763@www.fastmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox