performance - What's the (hidden) cost of Scala's lazy val? -
An easy way to Scala is lazy valve
, where val
It's getting late (at first access).
Of course, there should be some upper section with a lazy valve
- somewhere Scala should keep in mind that the value has already been evaluated and the evaluation should be synchronized , Because multiple threads can attempt to use the value at the same time for the first time.
A Lazy Val / u> - Whether it has been evaluated or not, there is a hidden boolean flag attached to a
lazy valve
to track, Is it really synchronized and has any other costs in it?
In addition, suppose I do this:
class something {lazy wal (x, y) = {...}}
Is it like having two different lazy vals
s x
and y
or I get an overhead once for the pair (X, y)
?
is taken from it and returns the implementation details of lazy
java code In terms of (instead of bytecode):
square is the lazy {lazy val msg = "lazy"}
to do something similar to the following java code Compiled for:
Class LazyTest {public int bitmap $ 0; Private string msg; Public string msg (if (bitmap $ 0 and amp; 1) == 0) {synchronize (this) {if ((bitmap $ 0 and amp; 1) == 0) {synchronize (this) {msg = "Lazy";}} bitmap $ 0 = bitmap $ 0 | 1;}} return message;}}
Comments
Post a Comment