@darksnake, I know about inlining. My question about loop conversion.
Let’s go step-by-step.
Firstly compiler will split operations to have something like this:
val var1 = ints.filter { it / 5 > 3 }
val var2 = var1.map { it.toString() }
val var3 = var2.filter { it.contains("1") }
val var4 = var3.map { it.length.toString() }
val var5 = var4.joinToString()
Then compiler will apply function inlining, e.g. first loop will be like this:
val var1 = ArrayList<Int>()
for (element in ints){
if(element / 5 > 3){
var1.add(element)
}
}
val var2 = var1.map { it.toString() }
val var3 = var2.filter { it.contains("1") }
val var4 = var3.map { it.length.toString() }
val var5 = var4.joinToString()
Then compiler will expand all loops, so we will have something like this:
val var1 = ArrayList<Int>()
for (element in ints){
...
}
val var2 = ArrayList<String>()
for (element in var1){
...
}
val var3 = ArrayList<String>()
for (element in var2){
...
}
val var4 = ArrayList<String>()
for (element in var3){
...
}
val var5 = ...
So as you can see (please correct me if I wrong) we will have separate foreach loops, each of them will create iterator on head + temporary array (before jit optimizations of course)
However kotlin compiler can convert this set of loops to the single loop, so we will have something like this:
val var5 = StringBuilder()
for (element in ints){
if(element / 5 > 3){
val var2 = element.toString()
if(var2.contains("1")){
if(!var5.isEmpty()){
var5.append(", ")
}
var5.append(var2)
}
}
}
I know, that some JITs can sometimes convert separate loops to the single loop. However, my question: are there any discussions to apply the following optimization on the kotlin-to-bytecode compilation step?
Other things are optimized by jit
Am I right that your point is that all JIT compilers have all optimizations which I listed above?