Search code examples
javaregex

How to match regular expression without creating objects in Java?


I'm working on a reg expression match function. The problem is, this function will be called by the framework inside a nested loop. If temporary objects are created, GC will cause very big performance problem.

Is it possible to deal with regexp things without create temp objects (Pattern,Matcher)? Rewrite regexp classes is my last choice...


Solution

  • To quote an old saying:

    Make it work, make it right, make it fast. (in that order)

    So before going down any heavy optomization steps, just write the initial straightforward appropriate code (which in this case would involve pre-compiling your patterns if you can). Run some tests and see if the performance is inadequate, and then optimize if the regex portion is a bottleneck.

    If the object creation (and cleanup) is a serious bottleneck (as compared to the actual regex parsing itself), then you may need to implement your own solution that uses an object pool (so objects are not created, just reset and reused from the pool). I doubt that this will result in any serious performance gains though, so you should benchmark first just to see how much gain is even possible (if you improve object creation / cleanup performance by 50%, would it be worth it?).