So I did some more testing to tweak the regexps to see if I could narrow it down, and I found a new set of regexps that run the same speed from 5.8 all the way to 5.30 AND they are something like 10% faster than the original (on my host, your regexps take 10-12 secs, and mine take 9-11).
All of your regexps match up to (but not including) the newline. Based on the comment that '^' might be causing problems, I just added a '\s*' to the end of each regexp so basically each one of them grabs the newline and any additional whitespace. The following runs quickly for all versions:
sub parse_foo {
my ($text) = @_;
my $name;
{
last if $text =~ /\G \Z/gcmsx;
if ($text =~ /\G begfoo \s+ (\S+?) \s* \( \s* (.*?) \s* \)
+ \s* ; \s*/gcmsx) { $name = $1 }
elsif ($text =~ /\G endfoo \s* /gcmsx) { }
elsif ($text =~ /\G \S+ \s+ .*? \s* ; \s*/gcmsx) { }
else { die "ERROR: unknown syntax\n" }
redo;
}
print "LAST FOO: $name\n";
}