This page is no longer maintained — Please continue to the home page at

Compiler optimisation question

No replies
Alan Burlison
Joined: 2011-08-26,
User offline. Last seen 42 years 45 weeks ago.

I have some code that does URL replacement given a list of (from, to)
tuples. I'm converting the mappings into a list of anonymous functions
for ease of use as follows:

replaceUrls is a List[(String, String)]

val replaceFns: List[String => String] = match {
case (p, r) => s: String => {
new Regex("""\b(?i:href\s*=\s*")""" + p)
.replaceAllIn(s, """href="""" + r) }

My question is this: Is the compiler smart enough to optimise (for
example) the '"""href="""" + r' clause when the function is generated,
or will that string concatenation be done each time the function is
evaluated? I'm assuming it will be reevaluated each time. Would it
therefore be better to perform the concatenation outside the function
body and use the concatenated value inside the function, as a closure?

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland