[JPIERCE - Sun Dec 18 20:23:59 2005]:
Show quoted text> See
http://web/belg4mit/www/18/, basically mutable yields unoptimized
> expressions.
>
> Also, mutability is probably the DWIM behavior (were the above bug not
> the case) and it's clear why one would want to set immutability.
It is actually working as advertised, which leads me to conclude that
the documentation sucks.
There are two contradicting forces at work here.
Firstly: performing the assembly transforms the internal data structure
so radically that it is no longer possible to add new patterns in any
sane manner.
Secondly: in a long-running program, you might want to add a few
patterns, assemble the result, add a few more, assemble the result and
so on until the end of time, or until the program halts, whichever comes
first.
So to reconcile these two opposing forces, I came up with the idea of
raising a flag to say that we want to assemble a pattern, but we also
want to be able to continue to add patterns afterwards. That's what
mutable is for: we want to keep changing it. Since we want to be able to
change it, we cannot reduce the data structure, because then we cannot
add new patterns. And since the data structure isn't reduced, the trie
optimisation isn't run, and the patterns come out with only prefix
reductions performed. Usually that's good enough, because in doing so
you save a few cycles by skipping the reduction code and a pattern, if
not the shortest pattern, is returned faster.
There is a way, though, of having your cake and eating it. You just
clone() the pattern at time t and reduce the copy. This allows you to
continue to add to the orginal, since its internal data structure wasn't
subjected to stresses of assembly. A rough sketch of the code would look
like:
my $master = Regexp::Assemble->new;
while (my $pattern = get_another_pattern()) {
$master->add( $pattern );
my $copy = $master->clone;
print $copy->as_string, "\n";
}
... for some definition of get_another_pattern(). Hope this helps.