Given a trait containing an object with a large-ish number of instances (100 files, each 100 lines) stored in non-lazy val
trait Repository {
object dao {
val a = new A
val b = new B
val c = new C
...
}
}
what is the initialization cost? Assume that there are 100 Repository consumers, one per DAO instance (i.e. “object X extends Repository” * 100). Will object X, Y, Z consumers each incur the overhead of 100 DAO instances?
Given that Repository consumers will only ever need to reference a subset of DAO instances, I am thinking of taking the lazy initialization approach instead, but trying to see what the trade off is here between cost of lazy vs. non-lazy initialization.
If, on JVM container startup, all 100 DAO instances are initialized, then a non-lazy approach would avoid the unnecessary overhead of lazy initialization. However, if 100 DAO instances are created for each Repository consumer, better to go lazy and reduce memory usage.
As far as I know,
objectsare lazily initialised. For instance withObserve:
Only now: