liquibase/liquibase — pull request #109 by ojacobson (2013-02-21 18:50:46+00:00)
Title: Be more conservative with regular expressions.

Starting with a non-greedy universal match causes Java's regular expression engine to try to match zero characters followed by the rest of the pattern, then 1 character followed by the rest of the pattern, and so on until it finds a match. Since comments must appear at the _end_ of input, this ensures that the regex engine does a maximal amount of work before eliminating a comment. For queries of a "reasonable" size, this isn't an issue, but a 10 or 20
kilocharacter query is large enough to cause comment removal to take over a second.

Since the replacement removes the trailing part of the match outright, I've rewritten the filter regexes to do so directly, rather than by replacing the trailing part with the preceding universal group, and removed the universal group match. This permits the regex engine to skip from whitespace to whitespace without evaluating all the intervening bytes, and avoids
backtracking entirely.

This bit my day job, in real life, on queries that insert entire PNG images via hex-encoded strings. While this is an unreasonable query, it's not an _unrealistic_ query. We worked around the problem by disabling stripComments, but fixing the regex will improve overall performance.