1

As I am learning scal, I was wondering about optimisation when using local variables in lambda function.

For instance the following code :

val My_List = l.map{x =>
     val a = coomplexFun(x.attr_1, x.attr_2 ) ;
     (x.attr_1, doSomthing(a))
}

is it equivalent to :

val My_List = l.map{x =>
     (x.attr_1, doSomthing(coomplexFun(x.attr_1, x.attr_2 )))
}

Or is there some extra memory allocation cost?

1 Answer 1

1

It is equivalent during runtime, since the JIT/Hotspot compiler will optimize it if necessary . The compiled class will be slightly larger since it contains information for the debugger, e.g. the name of the local variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Not all implementations of Scala have a JIT. Actually, no implementations of Scala have a JIT. Scala-native is a static AOT compiler that compiles to native machine code. Scala.js is a static AOT compiler that compiles to ECMAScript source code. That code may or may not then be executed on an execution engine with a JIT. Scala-JVM is a static AOT compiler that compiles to JVML bytecode. That bytecode may or may not then be executed on an execution engine with a JIT. E.g. the Excelsior JET JVM doesn't have a JIT. Or, you could further compile the JVM bytecode to Android bytecode, to be …
… executed by the ART, which doesn't have a JIT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.