develooper Front page | perl.perl5.porters | Postings from September 2003

Re: optimizing /\s*,\s*/

Thread Previous | Thread Next
From:
Jeff 'japhy' Pinyan
Date:
September 8, 2003 15:46
Subject:
Re: optimizing /\s*,\s*/
Message ID:
Pine.LNX.4.44.0309081842530.2175-100000@perlmonk.org
On Sep 8, Mark Jason Dominus said:

>> Could the engine be optimized to search FIRST for the NON-OPTIONAL comma,
>> and then match all immediately preceding whitespace?
>
>Perhaps I misunderstand what you mean, but doesn't it already do this?
>
>        perl -Mre=debug -e '"   ,    " =~ /\s*,\s*/'
>
>        ...
>        floating `,' at 0..2147483647 (checking floating) stclass `ANYOF[\11\12\14\15 ,{unicode_all}]' minlen 1
>        ...
>        Guessing start of match, REx `\s*,\s*' against `   ,    '...
>        Found floating substr `,' at offset 3...
>        ...
>
>and then it starts the backtracking search.

That's only a preliminary check.

====
perlmonk:~ $ perl -Mre=debug -e '"   b    ,    " =~ /\s*,\s*/'

[...]

floating `,' at 0..2147483647 (checking floating) stclass

[...]

`ANYOF[\11\12\14\15 ,{unicode_all}]' minlen 1
Guessing start of match, REx `\s*,\s*' against `   b    ,    '...
Found floating substr `,' at offset 8...
Does not contradict STCLASS...
Guessed: match at offset 0

[...]

  Setting an EVAL scope, savestack=3
   0 <> <   b    ,   >    |  1:  STAR
                           SPACE can match 3 times out of 2147483647...
  Setting an EVAL scope, savestack=3
                            failed...
  Setting an EVAL scope, savestack=3
   1 < > <  b    ,   >    |  1:  STAR
                           SPACE can match 2 times out of 2147483647...
  Setting an EVAL scope, savestack=3
                            failed...
  Setting an EVAL scope, savestack=3
   2 <  > < b    ,   >    |  1:  STAR
                           SPACE can match 1 times out of 2147483647...
  Setting an EVAL scope, savestack=3
                            failed...
====

See what it just did?  I started at the first chunk of whitespace it found
(and it tried matching at EACH location IN the whitespace).

====
  Setting an EVAL scope, savestack=3
   4 <   b> <    ,   >    |  1:  STAR
                           SPACE can match 4 times out of 2147483647...
  Setting an EVAL scope, savestack=3
   8 <  b    > <,    >    |  3:    EXACT <,>
   9 <  b    ,> <    >    |  5:    STAR
                           SPACE can match 4 times out of 2147483647...
  Setting an EVAL scope, savestack=3
  13 <  b    ,    > <>    |  7:      END
====

What I'm asking is, can an optimization be written that removes the failed
cases in this example?

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About