Prefer compact bytecode for primitive/object comparisons with Boolean

'java.lang.Boolean#valueOf' doesn't allocate new objects
(it uses pre-allocated singletons for 'true' and 'false').
This commit is contained in:
Dmitry Petrov
2017-07-17 11:42:54 +03:00
parent 8e9c0294fe
commit e1a55e8dec
5 changed files with 14 additions and 18 deletions
@@ -148,12 +148,8 @@ public class AsmUtil {
return type == Type.INT_TYPE || type == Type.SHORT_TYPE || type == Type.BYTE_TYPE || type == Type.CHAR_TYPE;
}
public static boolean isNonFloatingPointPrimitive(Type type) {
return isIntPrimitive(type) || type == Type.LONG_TYPE || type == Type.BOOLEAN_TYPE;
}
public static boolean isIntPrimitiveOrBoolean(Type type) {
return isIntPrimitive(type) || type == Type.BOOLEAN_TYPE;
public static boolean isIntOrLongPrimitive(Type type) {
return isIntPrimitive(type) || type == Type.LONG_TYPE;
}
public static boolean isNumberPrimitiveOrBoolean(Type type) {
@@ -249,7 +249,7 @@ class BoxedToPrimitiveEquality private constructor(
@JvmStatic
fun isApplicable(opToken: IElementType, leftType: Type, rightType: Type) =
(opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ) &&
AsmUtil.isNonFloatingPointPrimitive(rightType) &&
AsmUtil.isIntOrLongPrimitive(rightType) &&
AsmUtil.isBoxedTypeOf(leftType, rightType)
}
}
@@ -323,7 +323,7 @@ class PrimitiveToBoxedEquality private constructor(
@JvmStatic
fun isApplicable(opToken: IElementType, leftType: Type, rightType: Type) =
(opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ) &&
AsmUtil.isNonFloatingPointPrimitive(leftType) &&
AsmUtil.isIntOrLongPrimitive(leftType) &&
AsmUtil.isBoxedTypeOf(rightType, leftType)
}
}
@@ -402,7 +402,7 @@ class PrimitiveToObjectEquality private constructor(
@JvmStatic
fun isApplicable(opToken: IElementType, leftType: Type, rightType: Type) =
(opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ) &&
AsmUtil.isNonFloatingPointPrimitive(leftType) &&
AsmUtil.isIntOrLongPrimitive(leftType) &&
rightType.sort == Type.OBJECT
}
}
@@ -16,11 +16,11 @@ fun testInt2(a: Int?, b: Int) = a != b
fun testLong1(a: Long?, b: Long) = a == b
fun testLong2(a: Long?, b: Long) = a != b
// 2 booleanValue
// 2 charValue
// 2 byteValue
// 2 shortValue
// 2 intValue
// 2 longValue
// 0 valueOf
// 0 areEqual
// 2 valueOf
// 2 Boolean.valueOf
// 2 areEqual
@@ -16,11 +16,11 @@ fun testInt2(a: Int, b: Int?) = a != b
fun testLong1(a: Long, b: Long?) = a == b
fun testLong2(a: Long, b: Long?) = a != b
// 2 booleanValue
// 2 charValue
// 2 byteValue
// 2 shortValue
// 2 intValue
// 2 longValue
// 0 valueOf
// 0 areEqual
// 2 valueOf
// 2 Boolean.valueOf
// 2 areEqual
@@ -16,11 +16,11 @@ fun testInt2(a: Int, b: Any?) = a != b
fun testLong1(a: Long, b: Any?) = a == b
fun testLong2(a: Long, b: Any?) = a != b
// 2 booleanValue
// 2 charValue
// 2 byteValue
// 2 shortValue
// 2 intValue
// 2 longValue
// 0 valueOf
// 0 areEqual
// 2 valueOf
// 2 Boolean.valueOf
// 2 areEqual