This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Trade stack space for time
authorKarl Williamson <public@khwilliamson.com>
Sun, 27 May 2012 07:08:46 +0000 (01:08 -0600)
committerKarl Williamson <public@khwilliamson.com>
Thu, 2 Aug 2012 15:24:51 +0000 (09:24 -0600)
commit4924e08c35622f31867cf9e248a2d7aa96e3eace
tree39a5cfa58ab45d2721945d335e364b4aaef9d212
parent3d26503b21699b90df97d95c59c0078178969d7d
regcomp.c: Trade stack space for time

Pass 1 of regular expression compilation merely calculates the size it
will need. (Note that Yves and I both think this is very suboptimal
behavior.)  Nothing is written out during this pass, but sizes are
just incremented.  The code in regcomp.c all knows this, and skips
writing things in pass 1.  However, when folding, code in other files is
called which doesn't have this size-only mode, and always writes its
results out.  Currently, regcomp handles this by passing to that code a
temporary buffer allocated for the purpose.  In pass1, the result is
simply ignored; in pass2, the results are copied to the correct final
destination.

We can avoid that copy by making the temporary buffer large enough to
hold the whole node, and in pass1, use it instead of the node.  The
non-regcomp code writes to the same relative spot in the buffer that it
will use for the real node.  In pass2 the real destination is used, and
the fold gets written directly to the correct spot.

Note that this increases the size pushed onto the stack, but code is
ripped out as well.

However, the main reason I'm doing this is not this speed-up; it is
because it is needed by future commits to fix a bug.
regcomp.c