Q:

In Scala using variables in a map reduces the performance?

maybe it is a stupid question, but I have this doubt and I cannot find a response...
If I have a map operation on a list of complex objects and to make the code more readable I use intermediate variables inside the map the performance can change?
For example this are two versions of the same code:
 profilesGroupedWithIds map {
  c =>
    val blockId = c._2
    val entityIds = c._1._2
    val entropy = c._1._1
    if (separatorID < 0) BlockDirty(blockId, entityIds.swap, entropy)
    else BlockClean(blockId, entityIds, entropy)
}

..
profilesGroupedWithIds map {
  c =>
    if (separatorID < 0) BlockDirty(c._2, c._1._2.swap, c._1._1)
    else BlockClean(c._2, c._1._2, c._1._1)
}

As you can see the first version is more readable than the second one.
But the efficiency is the same? Or the three variables that I have created inside the map have to be removed by the garbage collector and this will reduce the perfomance (suppose that 'profilesGroupedWithIds' is a very big list)?
Thanks
Regards
Luca

A:

The vals are just aliases for the tuple elements. So the generated java bytecode will be identical in both cases, and so will be the performance.
More importantly, the first variant is much better code since it clearly conveys the intent.
Here is a third variant that avoids accessing the tuple elements _1 and _2 entirely:
profilesGroupedWithIds map {
  case ((entropy,entityIds),blockId) =>
    if (separatorID < 0) BlockDirty(blockId, entityIds.swap, entropy)
    else BlockClean(blockId, entityIds, entropy)
}

