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:
@@ -148,12 +148,8 @@ public class AsmUtil {
|
|||||||
return type == Type.INT_TYPE || type == Type.SHORT_TYPE || type == Type.BYTE_TYPE || type == Type.CHAR_TYPE;
|
return type == Type.INT_TYPE || type == Type.SHORT_TYPE || type == Type.BYTE_TYPE || type == Type.CHAR_TYPE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isNonFloatingPointPrimitive(Type type) {
|
public static boolean isIntOrLongPrimitive(Type type) {
|
||||||
return isIntPrimitive(type) || type == Type.LONG_TYPE || type == Type.BOOLEAN_TYPE;
|
return isIntPrimitive(type) || type == Type.LONG_TYPE;
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isIntPrimitiveOrBoolean(Type type) {
|
|
||||||
return isIntPrimitive(type) || type == Type.BOOLEAN_TYPE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isNumberPrimitiveOrBoolean(Type type) {
|
public static boolean isNumberPrimitiveOrBoolean(Type type) {
|
||||||
|
|||||||
@@ -249,7 +249,7 @@ class BoxedToPrimitiveEquality private constructor(
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun isApplicable(opToken: IElementType, leftType: Type, rightType: Type) =
|
fun isApplicable(opToken: IElementType, leftType: Type, rightType: Type) =
|
||||||
(opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ) &&
|
(opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ) &&
|
||||||
AsmUtil.isNonFloatingPointPrimitive(rightType) &&
|
AsmUtil.isIntOrLongPrimitive(rightType) &&
|
||||||
AsmUtil.isBoxedTypeOf(leftType, rightType)
|
AsmUtil.isBoxedTypeOf(leftType, rightType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -323,7 +323,7 @@ class PrimitiveToBoxedEquality private constructor(
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun isApplicable(opToken: IElementType, leftType: Type, rightType: Type) =
|
fun isApplicable(opToken: IElementType, leftType: Type, rightType: Type) =
|
||||||
(opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ) &&
|
(opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ) &&
|
||||||
AsmUtil.isNonFloatingPointPrimitive(leftType) &&
|
AsmUtil.isIntOrLongPrimitive(leftType) &&
|
||||||
AsmUtil.isBoxedTypeOf(rightType, leftType)
|
AsmUtil.isBoxedTypeOf(rightType, leftType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -402,7 +402,7 @@ class PrimitiveToObjectEquality private constructor(
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun isApplicable(opToken: IElementType, leftType: Type, rightType: Type) =
|
fun isApplicable(opToken: IElementType, leftType: Type, rightType: Type) =
|
||||||
(opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ) &&
|
(opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ) &&
|
||||||
AsmUtil.isNonFloatingPointPrimitive(leftType) &&
|
AsmUtil.isIntOrLongPrimitive(leftType) &&
|
||||||
rightType.sort == Type.OBJECT
|
rightType.sort == Type.OBJECT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+3
-3
@@ -16,11 +16,11 @@ fun testInt2(a: Int?, b: Int) = a != b
|
|||||||
fun testLong1(a: Long?, b: Long) = a == b
|
fun testLong1(a: Long?, b: Long) = a == b
|
||||||
fun testLong2(a: Long?, b: Long) = a != b
|
fun testLong2(a: Long?, b: Long) = a != b
|
||||||
|
|
||||||
// 2 booleanValue
|
|
||||||
// 2 charValue
|
// 2 charValue
|
||||||
// 2 byteValue
|
// 2 byteValue
|
||||||
// 2 shortValue
|
// 2 shortValue
|
||||||
// 2 intValue
|
// 2 intValue
|
||||||
// 2 longValue
|
// 2 longValue
|
||||||
// 0 valueOf
|
// 2 valueOf
|
||||||
// 0 areEqual
|
// 2 Boolean.valueOf
|
||||||
|
// 2 areEqual
|
||||||
+3
-3
@@ -16,11 +16,11 @@ fun testInt2(a: Int, b: Int?) = a != b
|
|||||||
fun testLong1(a: Long, b: Long?) = a == b
|
fun testLong1(a: Long, b: Long?) = a == b
|
||||||
fun testLong2(a: Long, b: Long?) = a != b
|
fun testLong2(a: Long, b: Long?) = a != b
|
||||||
|
|
||||||
// 2 booleanValue
|
|
||||||
// 2 charValue
|
// 2 charValue
|
||||||
// 2 byteValue
|
// 2 byteValue
|
||||||
// 2 shortValue
|
// 2 shortValue
|
||||||
// 2 intValue
|
// 2 intValue
|
||||||
// 2 longValue
|
// 2 longValue
|
||||||
// 0 valueOf
|
// 2 valueOf
|
||||||
// 0 areEqual
|
// 2 Boolean.valueOf
|
||||||
|
// 2 areEqual
|
||||||
+3
-3
@@ -16,11 +16,11 @@ fun testInt2(a: Int, b: Any?) = a != b
|
|||||||
fun testLong1(a: Long, b: Any?) = a == b
|
fun testLong1(a: Long, b: Any?) = a == b
|
||||||
fun testLong2(a: Long, b: Any?) = a != b
|
fun testLong2(a: Long, b: Any?) = a != b
|
||||||
|
|
||||||
// 2 booleanValue
|
|
||||||
// 2 charValue
|
// 2 charValue
|
||||||
// 2 byteValue
|
// 2 byteValue
|
||||||
// 2 shortValue
|
// 2 shortValue
|
||||||
// 2 intValue
|
// 2 intValue
|
||||||
// 2 longValue
|
// 2 longValue
|
||||||
// 0 valueOf
|
// 2 valueOf
|
||||||
// 0 areEqual
|
// 2 Boolean.valueOf
|
||||||
|
// 2 areEqual
|
||||||
Reference in New Issue
Block a user