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
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.