Will scala compiler pick up regular expressions
I wonder if this:
object Foo {
val regex = "some complex regex".r
def foo() {
// use regex
}
}
and this:
object Foo {
def foo() {
val regex = "some complex regex".r
// use regex
}
}
will make any difference in performance. those. "some complex regex".r
does the scala compiler recognize what is a constant and cache it so it doesn't recompile every time?
source to share
This will make a difference in execution time. The expression from the first example will be evaluated only once. The expression from the second is every time you call Foo.foo()
. Evaluation here means applying the implicitly added "r" function (from scala -library) to a string:
scala> ".*".r
res40: scala.util.matching.Regex = .*
This function actually compiles the regex every time you call it (no caching).
Btw, any naive regex caching at runtime is vulnerable to OutOfMemory
- however I believe it is safe to implement it with WeakHashMap
, but the current Java implementation Pattern
(which underpins scala Regex
) doesn't actually implement it, probably because such an implementation may not have a predictable impact on performance (the GC might have to remove most of the cached values ββevery time it runs), The eviction cache is more predictable, but still not so simple (who will choose the timeout / size for it?). Speaking of scala -way, some smart macro could do compile-time optimizations (do caching only for string based regexes), but by default:
The Scala compiler also lacks regex optimizations, since regexp is not part of the scala language.
Therefore, it is better to move static ".r constructs from a function".
source to share