You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Jeff Trawick <tr...@gmail.com> on 2011/05/02 18:44:00 UTC

apr_fnmatch deltas

I just started playing and ran into this doc issue:

Index: include/apr_fnmatch.h
===================================================================
--- include/apr_fnmatch.h	(revision 1098590)
+++ include/apr_fnmatch.h	(working copy)
@@ -90,9 +90,9 @@
  *
  * PATTERN: [ followed by a class description followed by ]<br/>
  * MATCHES: A single character described by the class description.
- *          (Never matches, if the class description reaches until the
- *          end of the string without a ].) If the first character of
- *          the class description is ^ or !, the sense of the description
+ *          (If the initial [ character is not matched by a ], that [
+ *          character will treated as a literal.)  If the first character
+ *          of the class description is ^ or !, the sense of the description
  *          is reversed.  The rest of the class description is a list of
  *          single characters or pairs of characters separated by -. Any
  *          of those characters can have a backslash in front of them,

That issue affects this type of pattern string:

    res = apr_fnmatch("aaa[a*b", "aaa[a*b", 0);
    ABTS_INT_EQUAL(tc, 0, res);

With the old code (1.3.x branch), it does not match.  With the new
code, it does match.

Just sayin', for now.  I'll spend more time with the new code.

Re: apr_fnmatch deltas

Posted by "William A. Rowe Jr." <wr...@rowe-clan.net>.
On 5/2/2011 12:44 PM, Jeff Trawick wrote:
> 
> That issue affects this type of pattern string:
> 
>     res = apr_fnmatch("aaa[a*b", "aaa[a*b", 0);
>     ABTS_INT_EQUAL(tc, 0, res);
> 
> With the old code (1.3.x branch), it does not match.  With the new
> code, it does match.
> 
> Just sayin', for now.  I'll spend more time with the new code.

I did not refer to apr_fnmatch.h.  The language describing the various
behaviors may be BSD in origin and require copyright attribution.  Or
it may not; symbol names and bit values cannot be copyrighted.

Just sayin - I don't know if the copyright attribution in apr_fnmatch.h
belongs or not right now, but that file isn't properly branded as part
of an ASF project.

The behavior described was wrong; you can reproduce correct behavior
in BSD, Linux, and probably AIX and other shells as well.  Do not
refer to any stupidity in apr_fnmatch.h, but refer only to the specs
cited in apr_fnmatch.c.  I should have documented all irregularities
and defined all undefined behaviors, but if you find any ambiguity
please let me know!!!


Re: apr_fnmatch deltas

Posted by "William A. Rowe Jr." <wr...@rowe-clan.net>.
On 5/3/2011 12:12 PM, Jeff Trawick wrote:
> On Tue, May 3, 2011 at 1:20 AM, William A. Rowe Jr. <wr...@rowe-clan.net> wrote:
>> On 5/2/2011 6:24 PM, Jeff Trawick wrote:
>>> BTW, checked performance against previous version?
>>
>> For 1:1 testing of the patterns that exist in test/testfnmatch.c,
>> 100,000 iterations here on my box, 8626494 usec for the new vs.
>> 3674210 usec for the previous.
> 
> that proportion is in the ballpark of what I got with a different set
> of testcases
> 
> but they were contrived, the before/after builds weren't optimized, etc.

I was at Visual Studio 6, /O2 level, clean stack handling (not optimized).
I'll create a nice test utility timing fnmatch against old apr_fnmatch or
system fnmatch, for people to experiment with, once I'm caught up a bit.
It's amusing that your unoptimized build is near identical, I'm looking
forward to measuring against a modern compiler :)

>> This seems consistent with the retests of null, /, \/ in pattern
>> which are repeated all to frequently; this should be able to be
>> simplified with the use of a couple cautiously placed gotos and
>> some code coverage analysis (necessary to ensure we hit the entire
>> pattern space in our test patterns).  Also the fact that there is
>> so little recursion in the test cases, and so few calls to match
>> very common "*.txt" style patterns, which the new code should beat
>> the old hands down as trailing length is worked out, and we are
>> not redundantly testing failed pattern spaces.

My test code allows pattern string flag... I'm thinking the util
should just consume @patternfile @stringfile n-n range to allow
the user to abuse and clock some real patterns (find / > biglist).

>> Two things would radically affect the speed, which I am unsure
>> offhand what msvc did 10 yrs ago, inline expansion of **pattern
>> to &*pattern (it is an alias reference which should not be double
>> de-referenced), and aliasing all of the setup (slash in fnmatch_ch
>> is slash in fnmatch).  If the compiler is smart, there should be
>> no penalty for inlining the code as a function; if it is not, we
>> may want to consider duplicating [at least some of] the code (it
>> is invoked in two places, one for counting the trailing pattern
>> following a wildcard, one for performing the match).

I detest the idea of duplicating, we open potential de-synchronization
of the pattern walking logic from the pattern matching logic once we've
duplicated the code :(  I expect we will need to do this based on your
comments above.  We might also be able to collapse one level of looping
(and might not) based on judicious use of goto.  The depth of this logic
is an illustration of why "gotos considered harmful" can sometimes be
a destructive philosophy :)


Re: apr_fnmatch deltas

Posted by Jeff Trawick <tr...@gmail.com>.
On Tue, May 3, 2011 at 1:20 AM, William A. Rowe Jr. <wr...@rowe-clan.net> wrote:
> On 5/2/2011 6:24 PM, Jeff Trawick wrote:
>> BTW, checked performance against previous version?
>
> For 1:1 testing of the patterns that exist in test/testfnmatch.c,
> 100,000 iterations here on my box, 8626494 usec for the new vs.
> 3674210 usec for the previous.

that proportion is in the ballpark of what I got with a different set
of testcases

but they were contrived, the before/after builds weren't optimized, etc.

maybe I'll have time to play more after 1.4.3


>
> This seems consistent with the retests of null, /, \/ in pattern
> which are repeated all to frequently; this should be able to be
> simplified with the use of a couple cautiously placed gotos and
> some code coverage analysis (necessary to ensure we hit the entire
> pattern space in our test patterns).  Also the fact that there is
> so little recursion in the test cases, and so few calls to match
> very common "*.txt" style patterns, which the new code should beat
> the old hands down as trailing length is worked out, and we are
> not redundantly testing failed pattern spaces.
>
> The code is designed to move fairly painlessly to MBCS encodings.
> This incurs a design penalty in speed, although not nearly as bad
> as it will be once mbr*() calls are used in place of bytewise
> comparisons to advance characters, particularly for case insensitive
> comparison.
>
> Two things would radically affect the speed, which I am unsure
> offhand what msvc did 10 yrs ago, inline expansion of **pattern
> to &*pattern (it is an alias reference which should not be double
> de-referenced), and aliasing all of the setup (slash in fnmatch_ch
> is slash in fnmatch).  If the compiler is smart, there should be
> no penalty for inlining the code as a function; if it is not, we
> may want to consider duplicating [at least some of] the code (it
> is invoked in two places, one for counting the trailing pattern
> following a wildcard, one for performing the match).
>
> I'm rather more curious how it compares to real world BSD and Linux
> implementations today using SBCS "C" locale... as committed, as well
> as integrating fnmatch_ch() into the mainline code.  I will pull out
> the test patterns and test them against the BSD port (posted in
> p.a.o/~wrowe/ space) when some free time returns, I have some work-
> work I have to complete first.
>
> If anyone wants to spend cycles, the best place to start is to make
> a test suite list that corresponds to the real world, and clocks it
> in cpu spin instead of world clock time.  This also needs various
> flavors of range tests and false ranges (for which the old code
> disagreed with the modern fnmatch implementations).
>
> In any case, this closes a real world flaw, so I'd have no issue
> going with it for now as a correctness release once reviewers are
> satisfied, with more optimization work in the near term for the next
> release.

yeah

Re: apr_fnmatch deltas

Posted by "William A. Rowe Jr." <wr...@rowe-clan.net>.
On 5/2/2011 6:24 PM, Jeff Trawick wrote:
> BTW, checked performance against previous version?

For 1:1 testing of the patterns that exist in test/testfnmatch.c,
100,000 iterations here on my box, 8626494 usec for the new vs.
3674210 usec for the previous.

This seems consistent with the retests of null, /, \/ in pattern
which are repeated all to frequently; this should be able to be
simplified with the use of a couple cautiously placed gotos and
some code coverage analysis (necessary to ensure we hit the entire
pattern space in our test patterns).  Also the fact that there is
so little recursion in the test cases, and so few calls to match
very common "*.txt" style patterns, which the new code should beat
the old hands down as trailing length is worked out, and we are
not redundantly testing failed pattern spaces.

The code is designed to move fairly painlessly to MBCS encodings.
This incurs a design penalty in speed, although not nearly as bad
as it will be once mbr*() calls are used in place of bytewise
comparisons to advance characters, particularly for case insensitive
comparison.

Two things would radically affect the speed, which I am unsure
offhand what msvc did 10 yrs ago, inline expansion of **pattern
to &*pattern (it is an alias reference which should not be double
de-referenced), and aliasing all of the setup (slash in fnmatch_ch
is slash in fnmatch).  If the compiler is smart, there should be
no penalty for inlining the code as a function; if it is not, we
may want to consider duplicating [at least some of] the code (it
is invoked in two places, one for counting the trailing pattern
following a wildcard, one for performing the match).

I'm rather more curious how it compares to real world BSD and Linux
implementations today using SBCS "C" locale... as committed, as well
as integrating fnmatch_ch() into the mainline code.  I will pull out
the test patterns and test them against the BSD port (posted in
p.a.o/~wrowe/ space) when some free time returns, I have some work-
work I have to complete first.

If anyone wants to spend cycles, the best place to start is to make
a test suite list that corresponds to the real world, and clocks it
in cpu spin instead of world clock time.  This also needs various
flavors of range tests and false ranges (for which the old code
disagreed with the modern fnmatch implementations).

In any case, this closes a real world flaw, so I'd have no issue
going with it for now as a correctness release once reviewers are
satisfied, with more optimization work in the near term for the next
release.

Re: apr_fnmatch deltas

Posted by Jeff Trawick <tr...@gmail.com>.
BTW, checked performance against previous version?

Re: apr_fnmatch deltas

Posted by Jeff Trawick <tr...@gmail.com>.
Index: strings/apr_fnmatch.c
===================================================================
--- strings/apr_fnmatch.c	(revision 1098800)
+++ strings/apr_fnmatch.c	(working copy)
@@ -71,7 +71,7 @@
  * Both pattern and string are **char to support pointer increment of arbitrary
  * multibyte characters for the given locale, in a later iteration of this code
  */
-static __inline int fnmatch_ch(const char **pattern, const char
**string, int flags)
+static APR_INLINE int fnmatch_ch(const char **pattern, const char
**string, int flags)
 {
     const char * const mismatch = *pattern;
     const int nocase = !!(flags & APR_FNM_CASE_BLIND);

Re: apr_fnmatch deltas

Posted by "William A. Rowe Jr." <wr...@rowe-clan.net>.
On 5/2/2011 5:18 PM, Jeff Trawick wrote:
> 
> Index: strings/apr_fnmatch.c
> ===================================================================
> --- strings/apr_fnmatch.c	(revision 1098590)
> +++ strings/apr_fnmatch.c	(working copy)
> @@ -152,7 +152,7 @@
>      }
>      else if (**pattern == '?') {
>          /* Optimize '?' match before unescaping **pattern */
> -        if (!**string || (!slash || (**string != '/')))
> +        if (!**string || (slash && (**string == '/')))
>              return APR_FNM_NOMATCH;
>          result = 0;
>          goto fnmatch_ch_success;
> 
> fail if end of string being tested
> fail if we're trying to match a '/' but this is a pathname (wildcard
> '?' can't match '/' in that case)

Yup - that was where I went astray, I had moved typical '?'
processing into fnmatch_ch and constructed the test incorrectly
for its new context.  Glad we arrived at the same fix!

I fixed the one nested assignment alert, all that remains is the
'conditional expression is constant' out of the "for (; 1;)" loop
with no apparent fix.

Re: apr_fnmatch deltas

Posted by Jeff Trawick <tr...@gmail.com>.
On Mon, May 2, 2011 at 4:22 PM, Jeff Trawick <tr...@gmail.com> wrote:
> this fails unexpectedly:
>
>    res = apr_fnmatch("a?z", "a/z", 0);
>    ABTS_INT_EQUAL(tc, 0, res);

same as the other one, I just got there with a different thought

Index: strings/apr_fnmatch.c
===================================================================
--- strings/apr_fnmatch.c	(revision 1098590)
+++ strings/apr_fnmatch.c	(working copy)
@@ -152,7 +152,7 @@
     }
     else if (**pattern == '?') {
         /* Optimize '?' match before unescaping **pattern */
-        if (!**string || (!slash || (**string != '/')))
+        if (!**string || (slash && (**string == '/')))
             return APR_FNM_NOMATCH;
         result = 0;
         goto fnmatch_ch_success;

fail if end of string being tested
fail if we're trying to match a '/' but this is a pathname (wildcard
'?' can't match '/' in that case)

Re: apr_fnmatch deltas

Posted by Jeff Trawick <tr...@gmail.com>.
this fails unexpectedly:

    res = apr_fnmatch("a?z", "a/z", 0);
    ABTS_INT_EQUAL(tc, 0, res);

this related testcase works:

    res = apr_fnmatch("a?z", "a/z", APR_FNM_PATHNAME);
    ABTS_INT_EQUAL(tc, APR_FNM_NOMATCH, res);

Re: apr_fnmatch deltas

Posted by Jeff Trawick <tr...@gmail.com>.
this fails unexpectedly:

    res = apr_fnmatch("a?z", "azz", 0);
    ABTS_INT_EQUAL(tc, 0, res);