davidmoten / rxjava-extras

Utilities for use with rxjava
Apache License 2.0
269 stars 27 forks source link

Precompile TransformerStringSplit pattern #8

Closed Crystark closed 8 years ago

Crystark commented 8 years ago

Hi,

I was looking at TransformerStringSplit and noticed the use of String.split. Wouldn't this compile the pattern for each input String ? Or am I missing something ? An idea would be to compile the pattern int the TransformerStringSplit.split method the use Pattern.split ?

Thanks

davidmoten commented 8 years ago

Thanks for the comment, excuse the delay! I remember going down the same train of thought myself till I had a look at the String.split source code. You'll notice that it has special optimizations for handling simple regex patterns and in those cases doesn't call Pattern.compile. Having said that some users might be splitting on non-simple regex patterns and would want to precompile the pattern. What about your use cases? I could modify the code so that it used the optimized simple regex path if possible (extract the code out of String.split) and otherwise compile the Pattern once only.

davidmoten commented 8 years ago

Heck I'll just add an overload for specifying a Pattern and I'll make the existing version only check for the optimisation once.

Crystark commented 8 years ago

I was looking to this because i wanted to split on any line break so it's a more complex regex:

private static final Transformer<String, String>    LINE_SPLITTER   = Transformers.split("\\r?\\n|\\r");

The overload seems like a good option.

davidmoten commented 8 years ago

I've released rxjava 0.7.1 with the Pattern overload to maven central if you'd like to review/use it.