From 41094e63da244314b07bb7043d519600b018d9dc Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Sat, 6 Jun 2020 21:48:52 +0700 Subject: [PATCH] Optimize number constants placing to economize contant pool places --- .../kmath/expressions/MethodVisitors.kt | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/kmath-asm/src/main/kotlin/scientifik/kmath/expressions/MethodVisitors.kt b/kmath-asm/src/main/kotlin/scientifik/kmath/expressions/MethodVisitors.kt index 5b9a8afe8..dc5d83ea5 100644 --- a/kmath-asm/src/main/kotlin/scientifik/kmath/expressions/MethodVisitors.kt +++ b/kmath-asm/src/main/kotlin/scientifik/kmath/expressions/MethodVisitors.kt @@ -3,17 +3,28 @@ package scientifik.kmath.expressions import org.objectweb.asm.MethodVisitor import org.objectweb.asm.Opcodes.* -fun MethodVisitor.visitLdcOrIConstInsn(value: Int) { - when (value) { - -1 -> visitInsn(ICONST_M1) - 0 -> visitInsn(ICONST_0) - 1 -> visitInsn(ICONST_1) - 2 -> visitInsn(ICONST_2) - 3 -> visitInsn(ICONST_3) - 4 -> visitInsn(ICONST_4) - 5 -> visitInsn(ICONST_5) - else -> visitLdcInsn(value) - } +fun MethodVisitor.visitLdcOrIConstInsn(value: Int) = when (value) { + -1 -> visitInsn(ICONST_M1) + 0 -> visitInsn(ICONST_0) + 1 -> visitInsn(ICONST_1) + 2 -> visitInsn(ICONST_2) + 3 -> visitInsn(ICONST_3) + 4 -> visitInsn(ICONST_4) + 5 -> visitInsn(ICONST_5) + else -> visitLdcInsn(value) +} + +fun MethodVisitor.visitLdcOrDConstInsn(value: Double) = when (value) { + 0.0 -> visitInsn(DCONST_0) + 1.0 -> visitInsn(DCONST_1) + else -> visitLdcInsn(value) +} + +fun MethodVisitor.visitLdcOrFConstInsn(value: Float) = when (value) { + 0f -> visitInsn(FCONST_0) + 1f -> visitInsn(FCONST_1) + 2f -> visitInsn(FCONST_2) + else -> visitLdcInsn(value) } private val signatureLetters = mapOf( @@ -29,6 +40,13 @@ private val signatureLetters = mapOf( fun MethodVisitor.visitBoxedNumberConstant(number: Number) { val clazz = number.javaClass val c = clazz.name.replace('.', '/') - visitLdcInsn(number) + + when (number) { + is Int -> visitLdcOrIConstInsn(number) + is Double -> visitLdcOrDConstInsn(number) + is Float -> visitLdcOrFConstInsn(number) + else -> visitLdcInsn(number) + } + visitMethodInsn(INVOKESTATIC, c, "valueOf", "(${signatureLetters[clazz]})L${c};", false) }