Skip to content

Commit

Permalink
Optimize range operations for 'until' extension from stdlib (KT-9900)
Browse files Browse the repository at this point in the history
NB: for-in-until loop is generated as precondition loop, because the
corresponding range is right-exclusive (and thus we have no problems
with integer overflows).
  • Loading branch information
dnpetrov committed May 4, 2017
1 parent b83620f commit 506941e
Show file tree
Hide file tree
Showing 27 changed files with 533 additions and 8 deletions.
Expand Up @@ -134,6 +134,17 @@ public static boolean isPrimitiveNumberDownTo(@NotNull CallableDescriptor descri
return true;
}

public static boolean isPrimitiveNumberUntil(@NotNull CallableDescriptor descriptor) {
if (!isTopLevelInPackage(descriptor, "until", "kotlin.ranges")) return false;

ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter();
if (extensionReceiver == null) return false;
ClassifierDescriptor extensionReceiverClassifier = extensionReceiver.getType().getConstructor().getDeclarationDescriptor();
if (!isPrimitiveNumberClassDescriptor(extensionReceiverClassifier)) return false;

return true;
}

public static boolean isArrayOrPrimitiveArrayIndices(@NotNull CallableDescriptor descriptor) {
if (!isTopLevelInPackage(descriptor, "indices", "kotlin.collections")) return false;

Expand Down
Expand Up @@ -66,9 +66,13 @@ abstract class AbstractForInRangeLoopGenerator : AbstractForInProgressionOrRange

override fun assignToLoopParameter() {}

override fun increment(loopExit: Label) {
override fun checkPostConditionAndIncrement(loopExit: Label) {
checkPostCondition(loopExit)

incrementLoopVariable()
}

protected fun incrementLoopVariable() {
if (loopParameterType === Type.INT_TYPE) {
v.iinc(loopParameterVar, step)
}
Expand Down
Expand Up @@ -116,7 +116,7 @@ abstract class AbstractForLoopGenerator(

protected abstract fun assignToLoopParameter()

protected abstract fun increment(loopExit: Label)
protected abstract fun checkPostConditionAndIncrement(loopExit: Label)

fun body() {
codegen.generateLoopBody(forExpression.body)
Expand All @@ -135,7 +135,7 @@ abstract class AbstractForLoopGenerator(
fun afterBody(loopExit: Label) {
codegen.markStartLineNumber(forExpression)

increment(loopExit)
checkPostConditionAndIncrement(loopExit)

v.mark(bodyEnd)
}
Expand Down
Expand Up @@ -74,7 +74,7 @@ class ForInArrayLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForEx
v.store(loopParameterVar, asmElementType)
}

override fun increment(loopExit: Label) {
override fun checkPostConditionAndIncrement(loopExit: Label) {
v.iinc(indexVar, 1)
}
}
Expand Up @@ -67,7 +67,7 @@ class ForInCharSequenceLoopGenerator(codegen: ExpressionCodegen, forExpression:
v.store(loopParameterVar, asmElementType)
}

override fun increment(loopExit: Label) {
override fun checkPostConditionAndIncrement(loopExit: Label) {
v.iinc(indexVar, 1)
}

Expand Down
Expand Up @@ -92,7 +92,7 @@ class ForInProgressionExpressionLoopGenerator(codegen: ExpressionCodegen, forExp

override fun assignToLoopParameter() {}

override fun increment(loopExit: Label) {
override fun checkPostConditionAndIncrement(loopExit: Label) {
checkPostCondition(loopExit)

val loopParameter = loopParameter()
Expand Down
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jetbrains.kotlin.codegen.forLoop

import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.org.objectweb.asm.Label
import org.jetbrains.org.objectweb.asm.Type

class ForInUntilRangeLoopGenerator(
codegen: ExpressionCodegen,
forExpression: KtForExpression,
loopRangeCall: ResolvedCall<*>
) : AbstractForInRangeLoopGenerator(codegen, forExpression) {
private val from: ReceiverValue = loopRangeCall.extensionReceiver!!
private val to: KtExpression = ExpressionCodegen.getSingleArgumentExpression(loopRangeCall)!!

override fun storeRangeStartAndEnd() {
loopParameter().store(codegen.generateReceiverValue(from, false), v)
StackValue.local(endVar, asmElementType).store(codegen.gen(to), v)
}

override fun checkEmptyLoop(loopExit: Label) {}

override fun checkPreCondition(loopExit: Label) {
loopParameter().put(asmElementType, v)
v.load(endVar, asmElementType)
if (asmElementType.sort == Type.LONG) {
v.lcmp()
v.ifge(loopExit)
}
else {
v.ificmpge(loopExit)
}
}

override fun checkPostConditionAndIncrement(loopExit: Label) {
incrementLoopVariable()
}
}
Expand Up @@ -68,6 +68,8 @@ private fun ExpressionCodegen.createOptimizedForLoopGeneratorOrNull(
ForInRangeLiteralLoopGenerator(this, forExpression, loopRangeCall)
RangeCodegenUtil.isPrimitiveNumberDownTo(loopRangeCallee) ->
ForInDownToProgressionLoopGenerator(this, forExpression, loopRangeCall)
RangeCodegenUtil.isPrimitiveNumberUntil(loopRangeCallee) ->
ForInUntilRangeLoopGenerator(this, forExpression, loopRangeCall)
RangeCodegenUtil.isArrayOrPrimitiveArrayIndices(loopRangeCallee) ->
ForInArrayIndicesRangeLoopGenerator(this, forExpression, loopRangeCall)
RangeCodegenUtil.isCollectionIndices(loopRangeCallee) ->
Expand Down
Expand Up @@ -83,5 +83,5 @@ class IteratorForLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForE
StackValue.local(loopParameterVar, loopParameterType).store(value, v)
}

override fun increment(loopExit: Label) {}
override fun checkPostConditionAndIncrement(loopExit: Label) {}
}
28 changes: 28 additions & 0 deletions compiler/testData/codegen/box/ranges/forInUntil/forInUntilChar.kt
@@ -0,0 +1,28 @@
// IGNORE_BACKEND: JS
// see KT-17700

// WITH_RUNTIME

import kotlin.test.assertEquals

fun box(): String {
testChar()
testNullableChar()
return "OK"
}

private fun testChar() {
var sum = ""
for (ch in '1' until '5') {
sum = sum + ch
}
assertEquals("1234", sum)
}

private fun testNullableChar() {
var sum = ""
for (ch: Char? in '1' until '5') {
sum = sum + (ch ?: break)
}
assertEquals("1234", sum)
}
10 changes: 10 additions & 0 deletions compiler/testData/codegen/box/ranges/forInUntil/forInUntilChar0.kt
@@ -0,0 +1,10 @@
// WITH_RUNTIME

import kotlin.test.assertEquals

fun box(): String {
for (ch in (-10).toChar() until '\u0000') {
throw AssertionError("This loop shoud not be executed")
}
return "OK"
}
25 changes: 25 additions & 0 deletions compiler/testData/codegen/box/ranges/forInUntil/forInUntilInt.kt
@@ -0,0 +1,25 @@
// WITH_RUNTIME

import kotlin.test.assertEquals

fun box(): String {
testIntInIntUntilInt()
testNullableIntInIntUntilInt()
return "OK"
}

private fun testIntInIntUntilInt() {
var sum = 0
for (i in 1 until 5) {
sum = sum * 10 + i
}
assertEquals(1234, sum)
}

private fun testNullableIntInIntUntilInt() {
var sum = 0
for (i: Int? in 1 until 5) {
sum = sum * 10 + (i?.toInt() ?: break)
}
assertEquals(1234, sum)
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME

import kotlin.test.assertEquals

fun box(): String {
for (i in 10 until 0) {
throw AssertionError("This loop should not be executed")
}
return "OK"
}
43 changes: 43 additions & 0 deletions compiler/testData/codegen/box/ranges/forInUntil/forInUntilLong.kt
@@ -0,0 +1,43 @@
// WITH_RUNTIME

import kotlin.test.assertEquals

fun testLongInLongUntilLong() {
var sum = 0
for (i in 1L until 5L) {
sum = sum * 10 + i.toInt()
}
assertEquals(1234, sum)
}

fun testLongInLongUntilInt() {
var sum = 0
for (i in 1L until 5.toInt()) {
sum = sum * 10 + i.toInt()
}
assertEquals(1234, sum)
}

fun testLongInIntUntilLong() {
var sum = 0
for (i in 1.toInt() until 5L) {
sum = sum * 10 + i.toInt()
}
assertEquals(1234, sum)
}

fun testNullableLongInIntUntilLong() {
var sum = 0
for (i: Long? in 1.toInt() until 5L) {
sum = sum * 10 + (i?.toInt() ?: break)
}
assertEquals(1234, sum)
}

fun box(): String {
testLongInLongUntilLong()
testLongInIntUntilLong()
testLongInLongUntilInt()
testNullableLongInIntUntilLong()
return "OK"
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME

import kotlin.test.assertEquals

fun box(): String {
for (i in Int.MAX_VALUE until Int.MAX_VALUE) {
throw AssertionError("This loop shoud not be executed")
}
return "OK"
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME

import kotlin.test.assertEquals

fun box(): String {
for (i in 0 until Int.MIN_VALUE) {
throw AssertionError("This loop shoud not be executed")
}
return "OK"
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME

import kotlin.test.assertEquals

fun box(): String {
for (i in 0 until Long.MIN_VALUE) {
throw AssertionError("This loop shoud not be executed")
}
return "OK"
}
@@ -0,0 +1,21 @@
// WITH_RUNTIME

import kotlin.test.assertEquals

fun box(): String {
testIntInIntUntilSmartcastInt()
return "OK"
}

private fun testIntInIntUntilSmartcastInt() {
var sum = 0

val a: Any = 5
if (a is Int) {
for (i: Int in 1 until a) {
sum = sum * 10 + i
}
}

assertEquals(1234, sum)
}
16 changes: 16 additions & 0 deletions compiler/testData/codegen/bytecodeText/forLoop/forInUntil.kt
@@ -0,0 +1,16 @@
// WITH_RUNTIME

fun test(): Int {
var sum = 0
for (i in 1 until 6) {
sum = sum * 10 + i
}
return sum
}

// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 1 IF
3 changes: 2 additions & 1 deletion compiler/testData/codegen/bytecodeText/intRangeNoBoxing.kt
@@ -1,6 +1,7 @@
fun Int.until(other: Int) = this..other - 1
fun foo() {
for (i in 1 until 2) {
val range = 1 until 2
for (i in range) {
}

for (i in 1..2 step 4) {}
Expand Down

0 comments on commit 506941e

Please sign in to comment.