Scala compiler optimization for immutability

Does the scala compiler optimize for memory usage by deleting references to valused only once in a block?

Imagine that an object contains some huge data in total - reaching the size at which cloning data or its derivatives can well scratch the maximum amount of memory for the JVM / machine.

A minimal code example, but imagine a longer chain of data transformations:

val huge: HugeObjectType
val derivative1 = huge.map(_.x)
val derivative2 = derivative1.groupBy(....)

Will there be a compiler, for example. leave hugemarked for garbage collection after it has derivative1been calculated? or will he support it until the packaging unit is completed?

Consistency is good in theory; I personally find it fascinating. But to be suitable for large data objects that cannot be processed by elements in current operating systems - I would say that in essence impedance is incompatible with reasonable use of memory, since a large data application on the JVM doesn’t do this if compilers do not optimize for things like this case.

+4
source share
2 answers

First of all: the actual release of unused memory occurs whenever the GC JVM considers it necessary. Thus, scalac cannot do this.

, scalac, , , .

val huge: HugeObjectType
val derivative1 = huge.map(_.x)
huge = null // inserted by scalac
val derivative2 = derivative1.groupBy(....)
derivative1 = null // inserted by scalac

scala -Internals, , Hotspot JVM . . scalac hacker Grzegorz Kossakowski .

, JVM JIT-, JIT- . , , JVM .

.

, , apache, , , . , .

. - defs.

def huge: HugeObjectType
def derivative1 = huge.map(_.x)
def derivative2 = derivative1.groupBy(....)
val result = derivative2.<some other transform>

, - ! , map filter, , . ! , groupBy, . .

+7

derivative1 , ( ). , , :

val huge: HugeObjectType
val derivative2 = {
    val derivative1 = huge.map(_.x)
    derivative1.groupBy(....)
}

, , derivative1 derivative2 .

+2

Source: https://habr.com/ru/post/1616928/


All Articles