Matcher performance improvements
Martin Buchholz
martinrb at google.com
Fri Nov 13 01:43:43 UTC 2009
Guess we dropped the ball on this.
I just created a new webrev to optimize Matcher.replaceAll,
by using StringBuilder instead of StringBuffer.
This version of the patch does not add any public APIs,
which has the advantage that this patch can be included in jdk6 as well.
A follow-on patch can add new public methods,
the signatures of which is sure to be controversial.
http://cr.openjdk.java.net/~martin/webrevs/openjdk7/Matcher-perf/
I still see 20-30% win on Jeremy's toy benchmark.
Please review.
Martin
On Thu, Aug 13, 2009 at 16:14, Martin Buchholz <martinrb at google.com> wrote:
> Jeremy is out for a few days - let's pick this up next week.
>
> In the meantime, we probably don't want
> public Appendable appendTail(Appendable a) {
>
> but instead
>
> public <T extends Appendable> T appendTail(T a) {
>
> It would be nice to be able to simply change the signature of the
> existing method
> that takes a StringBuffer, but there's no doubt some obscure
> reason why that would not be 100% compatible.
>
> Martin
>
>
> > I suggested yesterday that you might consider to add the pair for the
> "Appendable" for more general use scenario,
> > instead of the StringBuilder. I just tried your test case with this
> approach
>
> >
> http://cr.openjdk.java.net/~sherman/regex_replace/webrev/src/share/classes/java/util/regex/Matcher.java.sdiff.html<http://cr.openjdk.java.net/%7Esherman/regex_replace/webrev/src/share/classes/java/util/regex/Matcher.java.sdiff.html>
>
> > I still see 20%+ gain.
>
> > The only trick is we will have to deal with the "IOException" from the
> Appendable append methods...
>
>
> On Wed, Aug 12, 2009 at 18:56, Xueming Shen<Xueming.Shen at sun.com> wrote:
> >
> > The RFE#5066679 has been on my "next week's todo list" for a while:-) it
> > looks like
> > the Appendable might be a better choice here? I guess we should gain the
> > same performance
> > when you pass in a StringBuilder. Opinion?
> >
> > Sherman
> >
> > Martin Buchholz wrote:
> >>
> >> Hi regex/String team,
> >>
> >> We would like to contribute a performance improvement for Matcher.
> >>
> >> Please file a bug.
> >>
> >> category: java_util_regex
> >> Synopsis: Faster Matcher by replacing StringBuffer with StringBuilder
> >>
> >> Description:
> >> The old Matcher API and implementation depend on the old StringBuffer
> >> class.
> >> The use of StringBuilder gives measurable performance improvement in
> >> real world applications.
> >> http://cr.openjdk.java.net/~martin/webrevs/openjdk7/Matcher-perf/<http://cr.openjdk.java.net/%7Emartin/webrevs/openjdk7/Matcher-perf/>
> >> Written by Jeremy Manson.
> >>
> >> Here's a microbenchmark, where it gives 25% win:
> >>
> >> public class StringReplace {
> >> static String jabber = "Twas brillig, and the slithy toves " +
> >> "Did gyre and gimble in the wabe; " +
> >> "All mimsy were the borogoves, " +
> >> "And the mome raths outgrabe. " +
> >> " " +
> >> "Beware the Jabberwock, my son! " +
> >> "The jaws that bite, the claws that catch! " +
> >> "Beware the Jubjub bird, and shun " +
> >> "The frumious Bandersnatch!" +
> >> " " +
> >> "He took his vorpal sword in hand: " +
> >> "Long time the manxome foe he sought— " +
> >> "So rested he by the Tumtum tree, " +
> >> "And stood awhile in thought. " +
> >> " " +
> >> "And as in uffish thought he stood, " +
> >> "The Jabberwock, with eyes of flame, " +
> >> "Came whiffling through the tulgey wood, " +
> >> "And burbled as it came! " +
> >> " " +
> >> "One, two! One, two! and through and through " +
> >> "The vorpal blade went snicker-snack! " +
> >> "He left it dead, and with its head " +
> >> "He went galumphing back. " +
> >> " " +
> >> "And hast thou slain the Jabberwock? " +
> >> "Come to my arms, my beamish boy! " +
> >> "O frabjous day! Callooh! Callay!" +" " +
> >> "He chortled in his joy. " +
> >> " " +
> >> "'Twas brillig, and the slithy toves " +
> >> "Did gyre and gimble in the wabe; " +
> >> "All mimsy were the borogoves, " +
> >> "And the mome raths outgrabe?";
> >>
> >> public static void replace() {
> >> long start = System.currentTimeMillis();
> >> for (int i = 0; i < 100000; i++) {
> >> jabber.replaceAll("the", "ze");
> >> }
> >> long stop = System.currentTimeMillis();
> >> System.out.println(stop);
> >> System.out.println(start);
> >>
> >> System.out.println(stop - start);
> >> }
> >>
> >> public static void main(String[] args) {
> >> for (int i = 0; i < 5; i++) {
> >> replace();
> >> }
> >> }
> >> }
> >>
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20091112/88e8f2ea/attachment.html>
More information about the core-libs-dev
mailing list