From a6d0005f18e155f3f90e1ad2ccefd1e085b13472 Mon Sep 17 00:00:00 2001 From: Andreas Ernst Date: Fri, 22 Mar 2024 10:48:02 +0100 Subject: [PATCH] smaller optimizations --- .../serious/portal/mapper/CompositeFactory.kt | 7 --- .../java/com/serious/portal/mapper/Mapper.kt | 63 ++++++++++--------- .../serious/portal/mapper/ObjectFactory.kt | 14 ----- 3 files changed, 32 insertions(+), 52 deletions(-) delete mode 100644 portal/core/src/main/java/com/serious/portal/mapper/CompositeFactory.kt delete mode 100644 portal/core/src/main/java/com/serious/portal/mapper/ObjectFactory.kt diff --git a/portal/core/src/main/java/com/serious/portal/mapper/CompositeFactory.kt b/portal/core/src/main/java/com/serious/portal/mapper/CompositeFactory.kt deleted file mode 100644 index 69d907b3..00000000 --- a/portal/core/src/main/java/com/serious/portal/mapper/CompositeFactory.kt +++ /dev/null @@ -1,7 +0,0 @@ -package com.serious.portal.mapper - -import kotlin.reflect.KClass - -interface CompositeFactory { - fun createComposite(clazz: KClass<*>, vararg arguments: Any?): Any -} \ No newline at end of file diff --git a/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt b/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt index e54d1772..c5977675 100644 --- a/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt +++ b/portal/core/src/main/java/com/serious/portal/mapper/Mapper.kt @@ -6,10 +6,7 @@ package com.serious.portal.mapper */ import java.util.* -import kotlin.reflect.KClass -import kotlin.reflect.KMutableProperty -import kotlin.reflect.KMutableProperty1 -import kotlin.reflect.KProperty1 +import kotlin.reflect.* import kotlin.reflect.full.* import kotlin.reflect.jvm.ExperimentalReflectionOnLambdas import kotlin.reflect.jvm.jvmErasure @@ -868,6 +865,9 @@ class MappingDefinition(val sourceClass: KClass, val target // composite stuff abstract class CompositeDefinition(val clazz: KClass<*>, val index: Int, val nArgs: Int, val valueReceiver: Mapping.ValueReceiver) { + @JvmField + val constructor = clazz.constructors.first() + open fun immutable() : Boolean {return false} abstract fun createBuffer(mapper: Mapper): Mapping.CompositeBuffer } @@ -880,7 +880,7 @@ class MappingDefinition(val sourceClass: KClass, val target override fun immutable() : Boolean {return true} override fun createBuffer(mapper: Mapper): Mapping.CompositeBuffer { - return Mapping.ImmutableCompositeBuffer(this, mapper, clazz, nArgs) + return Mapping.ImmutableCompositeBuffer(this, nArgs) } } @@ -890,7 +890,7 @@ class MappingDefinition(val sourceClass: KClass, val target // override override fun createBuffer(mapper: Mapper): Mapping.CompositeBuffer { - return Mapping.MutableCompositeBuffer(this, mapper, clazz, nArgs) + return Mapping.MutableCompositeBuffer(this, clazz, nArgs) } } @@ -1245,6 +1245,10 @@ class Mapping( fun setup(compositeDefinitions: Array, stackSize: Int): Array { val buffers = compositeDefinitions.map { definition -> definition.createBuffer(mapper) }.toTypedArray() + /*val buffers = arrayOfNulls(compositeDefinitions.size) + for ( i in 0..( } } - abstract class CompositeBuffer(protected val definition: MappingDefinition.CompositeDefinition, protected var mapper: Mapper, val nargs: Int) { + abstract class CompositeBuffer(protected @JvmField val definition: MappingDefinition.CompositeDefinition, @JvmField val nArgs: Int) { + @JvmField protected var nSuppliedArgs = 0 // abstract @@ -1278,10 +1283,10 @@ class Mapping( abstract fun set(instance: Any, value: Any?, accessor: MappingDefinition.Accessor?, index: Int, mappingContext: Context) } - class ImmutableCompositeBuffer(definition: MappingDefinition.CompositeDefinition, mapper: Mapper, val clazz: KClass<*>, nargs: Int) : CompositeBuffer(definition, mapper, nargs) { + class ImmutableCompositeBuffer(definition: MappingDefinition.CompositeDefinition, nargs: Int) : CompositeBuffer(definition, nargs) { // instance data - protected var arguments: Array = arrayOfNulls(nargs) + private var arguments: Array = arrayOfNulls(nargs) // override @@ -1290,20 +1295,20 @@ class Mapping( // are we done? - if (++nSuppliedArgs == arguments.size) { + if (++nSuppliedArgs == nArgs) { // create composite - val composite = mapper.createComposite(clazz, *arguments) + val composite = definition.constructor.call(*arguments) definition.valueReceiver.receive(mappingContext, instance, composite) } // if } } - class MutableCompositeBuffer(definition: MappingDefinition.CompositeDefinition, mapper: Mapper, clazz: KClass<*>, nargs: Int) : CompositeBuffer(definition, mapper, nargs) { + class MutableCompositeBuffer(definition: MappingDefinition.CompositeDefinition, clazz: KClass<*>, nargs: Int) : CompositeBuffer(definition, nargs) { // instance data - private val newInstance = mapper.createInstance(null, clazz) + private val newInstance = clazz.createInstance() // TODO cache // public @@ -1312,14 +1317,13 @@ class Mapping( // are we done? - if (++nSuppliedArgs == nargs) + if (++nSuppliedArgs == nArgs) definition.valueReceiver.receive(mappingContext, instance, newInstance) } } // value receiver - interface ValueReceiver { fun receive(context: Context, instance: Any, value: Any) } @@ -1746,6 +1750,13 @@ class Mapping( val sourceClass: KClass<*> = definition.sourceClass val targetClass: KClass<*> = definition.targetClass + val isData = targetClass.isData + + //val constructor = targetClass.constructors.find { ctr -> ctr.parameters.size == 0}!! + + //fun createInstance() : Any { + // return constructor.callBy(NO_PARAMETERS) + //} // override @@ -1788,6 +1799,8 @@ class Mapping( return definition } + + val NO_PARAMETERS = emptyMap() } } @@ -1797,7 +1810,7 @@ fun mapping(sourceClass: KClass, targetClass: KClass): // one mapper has n mappings -class Mapper(vararg definitions: MappingDefinition<*, *>) : ObjectFactory, CompositeFactory { +class Mapper(vararg definitions: MappingDefinition<*, *>) { // instance data private var mappings = HashMap, Mapping>() @@ -1898,10 +1911,10 @@ class Mapper(vararg definitions: MappingDefinition<*, *>) : ObjectFactory, Compo val mapping = getMapping(source::class) if ( target == null) { - if ( mapping.targetClass.isData ) + if ( mapping.isData ) target = context // hmm.... else - target = mapping.targetClass.createInstance() + target = mapping.targetClass.createInstance() // TODO context.remember(source, target) } @@ -1910,7 +1923,7 @@ class Mapper(vararg definitions: MappingDefinition<*, *>) : ObjectFactory, Compo try { mapping.transform(source, target, context) - if ( mapping.targetClass.isData ) { + if ( mapping.isData ) { target = context.currentState!!.result context.remember(source, target!!) } @@ -1922,18 +1935,6 @@ class Mapper(vararg definitions: MappingDefinition<*, *>) : ObjectFactory, Compo return target as T } - // override ObjectFactory - - override fun createInstance(source: Any?, clazz: KClass<*>): Any { - return clazz.createInstance() - } - - // override CompositeFactory - - override fun createComposite(clazz: KClass<*>, vararg arguments: Any?): Any { - return clazz.constructors.first().call(*arguments) - } - // override Any fun describe() : String { diff --git a/portal/core/src/main/java/com/serious/portal/mapper/ObjectFactory.kt b/portal/core/src/main/java/com/serious/portal/mapper/ObjectFactory.kt deleted file mode 100644 index f93d2bf2..00000000 --- a/portal/core/src/main/java/com/serious/portal/mapper/ObjectFactory.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.serious.portal.mapper - -import kotlin.reflect.KClass - -interface ObjectFactory { - /** - * create a new instance of the specified class. - * - * @param source the source object - * @param clazz the class - * @return the new instance - */ - fun createInstance(source: Any?, clazz: KClass<*>): Any -} \ No newline at end of file