Introduce PerformancePitfall annotation

This commit is contained in:
Alexander Nozik 2021-05-13 11:09:49 +03:00
parent 97c4b81717
commit f5289abdc3
9 changed files with 18 additions and 10 deletions

View File

@ -674,6 +674,9 @@ public final class space/kscience/kmath/misc/CumulativeKt {
public static final fun cumulativeSumOfLong (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence; public static final fun cumulativeSumOfLong (Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence;
} }
public abstract interface annotation class space/kscience/kmath/misc/PerformancePitfall : java/lang/annotation/Annotation {
}
public final class space/kscience/kmath/misc/StringSymbol : space/kscience/kmath/misc/Symbol { public final class space/kscience/kmath/misc/StringSymbol : space/kscience/kmath/misc/Symbol {
public static final synthetic fun box-impl (Ljava/lang/String;)Lspace/kscience/kmath/misc/StringSymbol; public static final synthetic fun box-impl (Ljava/lang/String;)Lspace/kscience/kmath/misc/StringSymbol;
public static fun constructor-impl (Ljava/lang/String;)Ljava/lang/String; public static fun constructor-impl (Ljava/lang/String;)Ljava/lang/String;

View File

@ -59,6 +59,7 @@ public interface Structure2D<T> : StructureND<T> {
return get(index[0], index[1]) return get(index[0], index[1])
} }
@PerformancePitfall
override fun elements(): Sequence<Pair<IntArray, T>> = sequence { override fun elements(): Sequence<Pair<IntArray, T>> = sequence {
for (i in 0 until rowNum) for (i in 0 until rowNum)
for (j in 0 until colNum) yield(intArrayOf(i, j) to get(i, j)) for (j in 0 until colNum) yield(intArrayOf(i, j) to get(i, j))
@ -134,6 +135,7 @@ private class MutableStructure2DWrapper<T>(val structure: MutableStructureND<T>)
structure[intArrayOf(i, j)] = value structure[intArrayOf(i, j)] = value
} }
@PerformancePitfall
override fun elements(): Sequence<Pair<IntArray, T>> = structure.elements() override fun elements(): Sequence<Pair<IntArray, T>> = structure.elements()
override fun equals(other: Any?): Boolean = false override fun equals(other: Any?): Boolean = false

View File

@ -7,16 +7,14 @@ package space.kscience.kmath.geometry
import space.kscience.kmath.linear.Point import space.kscience.kmath.linear.Point
import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.GroupElement
import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.operations.ScaleOperations
import space.kscience.kmath.operations.invoke import space.kscience.kmath.operations.invoke
import kotlin.math.sqrt import kotlin.math.sqrt
@OptIn(UnstableKMathAPI::class) @OptIn(UnstableKMathAPI::class)
public interface Vector2D : Point<Double>, Vector, GroupElement<Vector2D, Euclidean2DSpace> { public interface Vector2D : Point<Double>, Vector{
public val x: Double public val x: Double
public val y: Double public val y: Double
public override val context: Euclidean2DSpace get() = Euclidean2DSpace
public override val size: Int get() = 2 public override val size: Int get() = 2
public override operator fun get(index: Int): Double = when (index) { public override operator fun get(index: Int): Double = when (index) {

View File

@ -7,17 +7,15 @@ package space.kscience.kmath.geometry
import space.kscience.kmath.linear.Point import space.kscience.kmath.linear.Point
import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.GroupElement
import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.operations.ScaleOperations
import space.kscience.kmath.operations.invoke import space.kscience.kmath.operations.invoke
import kotlin.math.sqrt import kotlin.math.sqrt
@OptIn(UnstableKMathAPI::class) @OptIn(UnstableKMathAPI::class)
public interface Vector3D : Point<Double>, Vector, GroupElement<Vector3D, Euclidean3DSpace> { public interface Vector3D : Point<Double>, Vector {
public val x: Double public val x: Double
public val y: Double public val y: Double
public val z: Double public val z: Double
public override val context: Euclidean3DSpace get() = Euclidean3DSpace
public override val size: Int get() = 3 public override val size: Int get() = 3
public override operator fun get(index: Int): Double = when (index) { public override operator fun get(index: Int): Double = when (index) {

View File

@ -12,7 +12,6 @@ import space.kscience.kmath.nd.FieldND
import space.kscience.kmath.nd.Strides import space.kscience.kmath.nd.Strides
import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.StructureND
import space.kscience.kmath.operations.Group import space.kscience.kmath.operations.Group
import space.kscience.kmath.operations.GroupElement
import space.kscience.kmath.operations.ScaleOperations import space.kscience.kmath.operations.ScaleOperations
import space.kscience.kmath.operations.invoke import space.kscience.kmath.operations.invoke
@ -26,9 +25,9 @@ public data class DomainBin<T : Comparable<T>>(
@OptIn(UnstableKMathAPI::class) @OptIn(UnstableKMathAPI::class)
public class IndexedHistogram<T : Comparable<T>, V : Any>( public class IndexedHistogram<T : Comparable<T>, V : Any>(
override val context: IndexedHistogramSpace<T, V>, public val context: IndexedHistogramSpace<T, V>,
public val values: StructureND<V>, public val values: StructureND<V>,
) : Histogram<T, Bin<T>>, GroupElement<IndexedHistogram<T, V>, IndexedHistogramSpace<T, V>> { ) : Histogram<T, Bin<T>> {
override fun get(point: Point<T>): Bin<T>? { override fun get(point: Point<T>): Bin<T>? {
val index = context.getIndex(point) ?: return null val index = context.getIndex(point) ?: return null

View File

@ -10,6 +10,7 @@ import org.nd4j.linalg.api.ops.impl.scalar.Pow
import org.nd4j.linalg.api.ops.impl.transforms.strict.* import org.nd4j.linalg.api.ops.impl.transforms.strict.*
import org.nd4j.linalg.factory.Nd4j import org.nd4j.linalg.factory.Nd4j
import org.nd4j.linalg.ops.transforms.Transforms import org.nd4j.linalg.ops.transforms.Transforms
import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.nd.* import space.kscience.kmath.nd.*
import space.kscience.kmath.operations.* import space.kscience.kmath.operations.*
@ -52,6 +53,7 @@ public interface Nd4jArrayAlgebra<T, C : Algebra<T>> : AlgebraND<T, C> {
return struct return struct
} }
@PerformancePitfall
public override fun StructureND<T>.map(transform: C.(T) -> T): Nd4jArrayStructure<T> { public override fun StructureND<T>.map(transform: C.(T) -> T): Nd4jArrayStructure<T> {
val newStruct = ndArray.dup().wrap() val newStruct = ndArray.dup().wrap()
newStruct.elements().forEach { (idx, value) -> newStruct[idx] = elementContext.transform(value) } newStruct.elements().forEach { (idx, value) -> newStruct[idx] = elementContext.transform(value) }

View File

@ -6,6 +6,7 @@
package space.kscience.kmath.nd4j package space.kscience.kmath.nd4j
import org.nd4j.linalg.factory.Nd4j import org.nd4j.linalg.factory.Nd4j
import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.StructureND
import space.kscience.kmath.operations.invoke import space.kscience.kmath.operations.invoke
import kotlin.math.PI import kotlin.math.PI
@ -14,6 +15,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertTrue import kotlin.test.assertTrue
import kotlin.test.fail import kotlin.test.fail
@OptIn(PerformancePitfall::class)
internal class Nd4jArrayAlgebraTest { internal class Nd4jArrayAlgebraTest {
@Test @Test
fun testProduce() { fun testProduce() {
@ -28,7 +30,7 @@ internal class Nd4jArrayAlgebraTest {
@Test @Test
fun testMap() { fun testMap() {
val res = with(IntNd4jArrayRing(intArrayOf(2, 2))) { one.map() { it + it * 2 } } val res = with(IntNd4jArrayRing(intArrayOf(2, 2))) { one.map { it + it * 2 } }
val expected = (Nd4j.create(2, 2) ?: fail()).asIntStructure() val expected = (Nd4j.create(2, 2) ?: fail()).asIntStructure()
expected[intArrayOf(0, 0)] = 3 expected[intArrayOf(0, 0)] = 3
expected[intArrayOf(0, 1)] = 3 expected[intArrayOf(0, 1)] = 3

View File

@ -6,6 +6,7 @@
package space.kscience.kmath.nd4j package space.kscience.kmath.nd4j
import org.nd4j.linalg.factory.Nd4j import org.nd4j.linalg.factory.Nd4j
import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.nd.get import space.kscience.kmath.nd.get
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -13,6 +14,7 @@ import kotlin.test.assertNotEquals
import kotlin.test.fail import kotlin.test.fail
internal class Nd4jArrayStructureTest { internal class Nd4jArrayStructureTest {
@OptIn(PerformancePitfall::class)
@Test @Test
fun testElements() { fun testElements() {
val nd = Nd4j.create(doubleArrayOf(1.0, 2.0, 3.0))!! val nd = Nd4j.create(doubleArrayOf(1.0, 2.0, 3.0))!!

View File

@ -1,5 +1,6 @@
package space.kscience.kmath.tensors.core package space.kscience.kmath.tensors.core
import space.kscience.kmath.misc.PerformancePitfall
import space.kscience.kmath.nd.DefaultStrides import space.kscience.kmath.nd.DefaultStrides
import space.kscience.kmath.nd.MutableBufferND import space.kscience.kmath.nd.MutableBufferND
import space.kscience.kmath.nd.as1D import space.kscience.kmath.nd.as1D
@ -24,6 +25,7 @@ internal class TestDoubleTensor {
assertEquals(tensor.value(), value) assertEquals(tensor.value(), value)
} }
@OptIn(PerformancePitfall::class)
@Test @Test
fun testStrides() = DoubleTensorAlgebra { fun testStrides() = DoubleTensorAlgebra {
val tensor = fromArray(intArrayOf(2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4)) val tensor = fromArray(intArrayOf(2, 2), doubleArrayOf(3.5, 5.8, 58.4, 2.4))