Implement constants inlining

This commit is contained in:
Iaroslav 2020-06-13 17:07:22 +07:00
parent f9835979ea
commit e91f6470d3
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
2 changed files with 23 additions and 11 deletions

View File

@ -17,7 +17,7 @@ import scientifik.kmath.operations.Algebra
* @param className the unique class name of new loaded class. * @param className the unique class name of new loaded class.
*/ */
class AsmGenerationContext<T>( class AsmGenerationContext<T>(
classOfT: Class<*>, private val classOfT: Class<*>,
private val algebra: Algebra<T>, private val algebra: Algebra<T>,
private val className: String private val className: String
) { ) {
@ -186,7 +186,15 @@ class AsmGenerationContext<T>(
return new return new
} }
internal fun visitLoadFromConstants(value: T) = visitLoadAnyFromConstants(value as Any, T_CLASS) internal fun visitLoadFromConstants(value: T) {
if (classOfT in INLINABLE_NUMBERS) {
visitNumberConstant(value as Number)
visitCastToT()
return
}
visitLoadAnyFromConstants(value as Any, T_CLASS)
}
private fun visitLoadAnyFromConstants(value: Any, type: String) { private fun visitLoadAnyFromConstants(value: Any, type: String) {
val idx = if (value in constants) constants.indexOf(value) else constants.apply { add(value) }.lastIndex val idx = if (value in constants) constants.indexOf(value) else constants.apply { add(value) }.lastIndex
@ -207,7 +215,6 @@ class AsmGenerationContext<T>(
maxStack++ maxStack++
val clazz = value.javaClass val clazz = value.javaClass
val c = clazz.name.replace('.', '/') val c = clazz.name.replace('.', '/')
val sigLetter = SIGNATURE_LETTERS[clazz] val sigLetter = SIGNATURE_LETTERS[clazz]
if (sigLetter != null) { if (sigLetter != null) {
@ -284,7 +291,8 @@ class AsmGenerationContext<T>(
} }
internal companion object { internal companion object {
private val SIGNATURE_LETTERS = mapOf( private val SIGNATURE_LETTERS by lazy {
mapOf(
java.lang.Byte::class.java to "B", java.lang.Byte::class.java to "B",
java.lang.Short::class.java to "S", java.lang.Short::class.java to "S",
java.lang.Integer::class.java to "I", java.lang.Integer::class.java to "I",
@ -292,6 +300,9 @@ class AsmGenerationContext<T>(
java.lang.Float::class.java to "F", java.lang.Float::class.java to "F",
java.lang.Double::class.java to "D" java.lang.Double::class.java to "D"
) )
}
private val INLINABLE_NUMBERS by lazy { SIGNATURE_LETTERS.keys }
internal const val FUNCTIONAL_COMPILED_EXPRESSION_CLASS = internal const val FUNCTIONAL_COMPILED_EXPRESSION_CLASS =
"scientifik/kmath/asm/FunctionalCompiledExpression" "scientifik/kmath/asm/FunctionalCompiledExpression"

View File

@ -2,6 +2,7 @@ package scientifik.kmath.asm
import org.objectweb.asm.Opcodes import org.objectweb.asm.Opcodes
import scientifik.kmath.operations.Algebra import scientifik.kmath.operations.Algebra
import scientifik.kmath.operations.ByteRing
private val methodNameAdapters: Map<String, String> = mapOf("+" to "add", "*" to "multiply", "/" to "divide") private val methodNameAdapters: Map<String, String> = mapOf("+" to "add", "*" to "multiply", "/" to "divide")