From e99f7ad360d2c0efb448c7dcdd2825dc1c9d663c Mon Sep 17 00:00:00 2001
From: Iaroslav <postovalovya@gmail.com>
Date: Mon, 22 Jun 2020 04:05:52 +0700
Subject: [PATCH 1/2] Optimize constant pooling

---
 kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt  | 6 +++---
 .../kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt    | 1 +
 .../scientifik/kmath/asm/internal/methodVisitors.kt       | 8 ++++++++
 3 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt
index 43c1a377d..feb3745b0 100644
--- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt
+++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt
@@ -19,11 +19,11 @@ fun <T : Any> MST.compileWith(type: KClass<T>, algebra: Algebra<T>): Expression<
         when (node) {
             is MST.Symbolic -> loadVariable(node.value)
             is MST.Numeric -> {
-                val constant = if (algebra is NumericAlgebra<T>) {
+                val constant = if (algebra is NumericAlgebra<T>)
                     algebra.number(node.value)
-                } else {
+                else
                     error("Number literals are not supported in $algebra")
-                }
+
                 loadTConstant(constant)
             }
             is MST.Unary -> {
diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt
index 649d06277..8b6892235 100644
--- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt
+++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt
@@ -260,6 +260,7 @@ internal class AsmBuilder<T> internal constructor(
                 is Int -> invokeMethodVisitor.visitLdcOrIntConstant(value)
                 is Double -> invokeMethodVisitor.visitLdcOrDoubleConstant(value)
                 is Float -> invokeMethodVisitor.visitLdcOrFloatConstant(value)
+                is Long -> invokeMethodVisitor.visitLdcOrLongConstant(value)
                 else -> invokeMethodVisitor.visitLdcInsn(value)
             }
 
diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/methodVisitors.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/methodVisitors.kt
index 70fb3bd44..6ecce8833 100644
--- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/methodVisitors.kt
+++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/methodVisitors.kt
@@ -11,6 +11,8 @@ internal fun MethodVisitor.visitLdcOrIntConstant(value: Int): Unit = when (value
     3 -> visitInsn(ICONST_3)
     4 -> visitInsn(ICONST_4)
     5 -> visitInsn(ICONST_5)
+    in -128..127 -> visitIntInsn(BIPUSH, value)
+    in -32768..32767 -> visitIntInsn(SIPUSH, value)
     else -> visitLdcInsn(value)
 }
 
@@ -20,6 +22,12 @@ internal fun MethodVisitor.visitLdcOrDoubleConstant(value: Double): Unit = when
     else -> visitLdcInsn(value)
 }
 
+internal fun MethodVisitor.visitLdcOrLongConstant(value: Long): Unit = when (value) {
+    0L -> visitInsn(LCONST_0)
+    1L -> visitInsn(LCONST_1)
+    else -> visitLdcInsn(value)
+}
+
 internal fun MethodVisitor.visitLdcOrFloatConstant(value: Float): Unit = when (value) {
     0f -> visitInsn(FCONST_0)
     1f -> visitInsn(FCONST_1)
-- 
2.34.1


From 29c6d2596733d25c4babc80aa90856b877153f42 Mon Sep 17 00:00:00 2001
From: Iaroslav <postovalovya@gmail.com>
Date: Mon, 22 Jun 2020 15:15:46 +0700
Subject: [PATCH 2/2] Optimize constant pooling for Byte and Short

---
 .../jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt  | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt
index 8b6892235..ff66e69b0 100644
--- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt
+++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt
@@ -257,6 +257,8 @@ internal class AsmBuilder<T> internal constructor(
 
         if (sigLetter != null) {
             when (value) {
+                is Byte -> invokeMethodVisitor.visitLdcOrIntConstant(value.toInt())
+                is Short -> invokeMethodVisitor.visitLdcOrIntConstant(value.toInt())
                 is Int -> invokeMethodVisitor.visitLdcOrIntConstant(value)
                 is Double -> invokeMethodVisitor.visitLdcOrDoubleConstant(value)
                 is Float -> invokeMethodVisitor.visitLdcOrFloatConstant(value)
-- 
2.34.1