user1819676
user1819676

Reputation: 369

How good is Scala By-name parameters performance?

I know how scala by-name parameters work: https://tpolecat.github.io/2014/06/26/call-by-name.html

I'm using it in a very sensitive piece of code that gets run a lot. My questions is: Is there any performance or memory drawback? For example, I know using lazy val has its drawbacks: What's the (hidden) cost of Scala's lazy val?

Is there something similar about by-name parameters? How are they implemented underneath?

Please note I will NOT be using it with lazy for caching. So I wouldn't have that above mentioned problem. I just need to know under the hood it's not using lazy itself.

Thanks

Upvotes: 2

Views: 209

Answers (1)

Jasper-M
Jasper-M

Reputation: 15086

By name parameters are implemented as instances of Function1. So they also have the same performance characteristics.

Calling a method with a by name parameter has the overhead of creating an instance of Function1 and using the by name parameter has the overhead of calling the method apply on a Function1 object.

Upvotes: 7

Related Questions