twitter/twitter-text-java — pull request #5 by spullara (2011-10-09 21:56:06+00:00)
Title: Optimizations

Added a micro benchmark and then made some optimizations:

(Executed on a Macbook Pro 13", 2.7 ghz i7, -Xmx1G -Xms1G)
Before Average: 3.677476679731188
After Average: 4.712168415220144
~28% speed boost

The optimizations include (in effect order):
- Removed String.format and replaced it with StringBuilder
- Removed a + from the hash tag regex that wasn't necessary
- Changed from String.split to a custom implementation

The current split between the various stages are:
- autoLinkHashtags: 60% (the unicode regex ranges are more than 50% of the cost of this method)
- autoLinkURLs: 30%
- autoLinkUsernamesAndLists: 5%
- other: 5%

Attempted optimizations that were not optimizations:
- Change references to String to CharSequences and avoid the StringBuilder.toString() at the end of each link phase -- there must be internal conversions to String as this is *slower*

The next step would be to replace regexes with a custom parser. Finally, if you could combine all the stages in a reasonable way that would probably have a pretty big benefit.