Skip to content

Commit

Permalink
chore: auto format by pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
slisson committed May 23, 2024
1 parent b265994 commit 5319ee4
Show file tree
Hide file tree
Showing 47 changed files with 188 additions and 170 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
- name: Build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./gradlew build -PciBuild=true
run: ./gradlew build -PciBuild=true
2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ repositories {

kotlin {
/* Targets configuration omitted.
* To find out how to configure the targets, please follow the link:
* https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#setting-up-targets */
* To find out how to configure the targets, please follow the link:
* https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#setting-up-targets */

jvm {
jvmToolchain(11)
Expand Down Expand Up @@ -68,7 +68,6 @@ kotlin {
}
}


allOpen {
annotation("org.openjdk.jmh.annotations.State")
}
Expand All @@ -85,7 +84,7 @@ benchmark {
iterationTime = 1
iterationTimeUnit = "s"
outputTimeUnit = "s"
//mode = "avgt"
// mode = "avgt"
reportFormat = "text"
include(".*")
}
Expand Down Expand Up @@ -121,10 +120,11 @@ publishing {
if (project.hasProperty("artifacts.itemis.cloud.user")) {
maven {
name = "itemisNexus3"
url = if (version.toString().contains("SNAPSHOT"))
uri("https://artifacts.itemis.cloud/repository/maven-mps-snapshots/")
else
uri("https://artifacts.itemis.cloud/repository/maven-mps-releases/")
url = if (version.toString().contains("SNAPSHOT")) {
uri("https://artifacts.itemis.cloud/repository/maven-mps-snapshots/")
} else {
uri("https://artifacts.itemis.cloud/repository/maven-mps-releases/")
}
credentials {
username = project.findProperty("artifacts.itemis.cloud.user").toString()
password = project.findProperty("artifacts.itemis.cloud.pw").toString()
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
kotlin.code.style=official
kotlin.code.style=official
1 change: 0 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
rootProject.name = "modelix.incremental"

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ expect class AtomicLong() {
fun incrementAndGet(): Long
fun decrementAndGet(): Long
fun get(): Long
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package org.modelix.incremental
class DependencyCycleException(val cycle: List<IStateVariableDeclaration<*, *>>) : Exception() {
override val message: String?
get() = "Cycle detected:\n" + cycle.joinToString("\n") { " $it" }
}
}
22 changes: 10 additions & 12 deletions src/commonMain/kotlin/org/modelix/incremental/DependencyGraph.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.modelix.incremental

import kotlinx.coroutines.Deferred
import kotlinx.coroutines.channels.Channel
import kotlin.math.max

/**
* Not thread-safe.
Expand All @@ -28,14 +26,14 @@ class DependencyGraph(val engine: IncrementalEngine) {
if (n1.isAutoValidate()) return
// replace n2->n1->n0 with n2->n0
val dependencies = n1.getDependencies().toList()
//if (dependencies.any { it.state == ECacheEntryState.VALIDATING }) return
// if (dependencies.any { it.state == ECacheEntryState.VALIDATING }) return
if (n1.getReverseDependencies().any { it.state == ECacheEntryState.VALIDATING }) return
dependencies.forEach { n0 -> n1.removeDependency(n0) }
val reverseDependencies = n1.getReverseDependencies().toList()
for (n2 in reverseDependencies) {
n2.removeDependency(n1)
dependencies.forEach { n0 -> n2.addDependency(n0) }
//println("Merged $n1 into $n2")
// println("Merged $n1 into $n2")
}
reverseDependencies.filterIsInstance<InternalStateNode<*, *>>().forEach { it.shrinkDependencies() }
require(n1.getDependencies().isEmpty()) { "$n1 still has dependencies" }
Expand All @@ -51,12 +49,13 @@ class DependencyGraph(val engine: IncrementalEngine) {
var node = nodes[key]
if (node == null) {
var parentGroup: IStateVariableGroup? = null
node = if (key is InternalStateVariableReference<*, *> && key.engine == engine)
if (key.decl is IComputationDeclaration<*>)
node = if (key is InternalStateVariableReference<*, *> && key.engine == engine) {
if (key.decl is IComputationDeclaration<*>) {
ComputationNode(key as InternalStateVariableReference<Any?, Any?>)
else
} else {
InternalStateNode(key)
else {
}
} else {
val external = if (key is IStateVariableReference<*>) ExternalStateNode(key) else ExternalStateGroupNode(key)
parentGroup = key.getGroup()
external
Expand Down Expand Up @@ -126,11 +125,11 @@ class DependencyGraph(val engine: IncrementalEngine) {
if (newState == ECacheEntryState.VALID) {
lastValidation = clock.getTime()
if (this is InternalStateNode<*, *>) {
//lru[key] // get access to move then entry to the MRU end of the queue
// lru[key] // get access to move then entry to the MRU end of the queue
}
}
if (previousState == ECacheEntryState.VALID) {
//println("Invalidated: $key")
// println("Invalidated: $key")
}
}
private var lastValidation: Long = 0L
Expand Down Expand Up @@ -232,6 +231,7 @@ class DependencyGraph(val engine: IncrementalEngine) {
open inner class InternalStateNode<In, Out>(override val key: InternalStateVariableReference<In, Out>) : Node(key) {
private val inputValues: MutableMap<ComputationNode<*>, In> = HashMap()
private var outputValue: Optional<Out> = Optional.empty()

/**
* if true, the engine will validate it directly after it got invalidated, without any external trigger
*/
Expand Down Expand Up @@ -339,7 +339,5 @@ class DependencyGraph(val engine: IncrementalEngine) {
setDependencies(this, newDependencies)
state = ECacheEntryState.FAILED
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ object DependencyTracking {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ enum class ECacheEntryState {
FAILED,
VALIDATING,
INVALID,
DEPENDENCY_INVALID
}
DEPENDENCY_INVALID,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package org.modelix.incremental

interface IActiveOutput<E> {
fun deactivate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package org.modelix.incremental

interface IComputationDeclaration<E> : IStateVariableDeclaration<E, E> {
fun invoke(context: IIncrementalFunctionContext<E>): E
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ interface IDependencyListener {
fun accessed(key: IStateVariableReference<*>)
fun modified(key: IStateVariableReference<*>)
fun parentGroupChanged(childGroup: IStateVariableGroup)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface IIncrementalEngine {
*/
fun <T> readStateVariables(calls: List<IStateVariableDeclaration<*, T>>): List<T>


/**
* Automatically re-executes the function whenever the inputs change.
* The function usually produces some side effect and is not expected to have any return value.
Expand All @@ -22,5 +21,5 @@ interface IIncrementalEngine {
* Blocks until all pending functions are recomputed.
*/
fun flush()
fun flush(callback: ()->Unit)
}
fun flush(callback: () -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ interface IIncrementalFunctionContext<RetT> {
fun <T> writeStateVariable(ref: IStateVariableDeclaration<T, *>, value: T)
}

fun <T> IIncrementalFunctionContext<T>.readOwnStateVariable(defaultValue: ()->T): T {
fun <T> IIncrementalFunctionContext<T>.readOwnStateVariable(defaultValue: () -> T): T {
return readOwnStateVariable().getOrElse(defaultValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface IStateVariableType<in In, out Out> {
fun getDefault(): Out
fun reduce(inputValues: Iterable<In>): Out
}
class StateVariableType<in In, out Out>(val defaultValue: Out, val reduceFunction: (Iterable<In>)->Out) : IStateVariableType<In, Out> {
class StateVariableType<in In, out Out>(val defaultValue: Out, val reduceFunction: (Iterable<In>) -> Out) : IStateVariableType<In, Out> {
override fun getDefault(): Out {
return defaultValue
}
Expand Down Expand Up @@ -44,4 +44,4 @@ data class InternalStateVariableReference<in In, out Out>(val engine: IIncrement
override fun read(): Out {
return engine.readStateVariable(decl)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ fun IStateVariableGroup.iterateGroups(): Sequence<IStateVariableGroup> {
current = current.getGroup()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package org.modelix.incremental

interface IStateVariableReference<out E> : IStateVariableGroup {
fun read(): E
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package org.modelix.incremental

interface IValueAccessor<E> {
fun getValue(): E
}
}
16 changes: 10 additions & 6 deletions src/commonMain/kotlin/org/modelix/incremental/IncrementalEngine.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.modelix.incremental

import kotlinx.coroutines.*
import kotlin.collections.HashSet
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.Synchronized
Expand Down Expand Up @@ -70,7 +74,7 @@ class IncrementalEngine(val maxSize: Int = 100_000) : IIncrementalEngine, IState
val value = (decl as IComputationDeclaration<T>).invoke(IncrementalFunctionContext(evaluation, node) as IIncrementalFunctionContext<T>)
(node as DependencyGraph.ComputationNode<T>).validationSuccessful(value, evaluation.dependencies)
return value
} catch (e : Throwable) {
} catch (e: Throwable) {
node.validationFailed(e, evaluation.dependencies)
throw e
}
Expand All @@ -83,10 +87,10 @@ class IncrementalEngine(val maxSize: Int = 100_000) : IIncrementalEngine, IState
node.removeDependency(earlierWriter)
update(earlierWriter.key)
}
//TODO("run triggers")
// TODO("run triggers")
node.state = ECacheEntryState.VALID
return node.readValue()
} catch (e : Throwable) {
} catch (e: Throwable) {
node.state = ECacheEntryState.FAILED
throw e
}
Expand Down Expand Up @@ -236,4 +240,4 @@ class IncrementalEngine(val maxSize: Int = 100_000) : IIncrementalEngine, IState
engineScope.launch { deactivate() }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.modelix.incremental

abstract class IncrementalFunctionImplementation<RetT>(
val name: String
val name: String,
) {
open fun getDefaultValue(): Optional<RetT> = Optional.empty()
}
Expand Down Expand Up @@ -79,7 +79,6 @@ data class IncrementalFunctionCall0<RetT>(
override fun toString(): String {
return "${function.name}()"
}

}
data class IncrementalFunctionCall1<RetT, P1>(
val function: IncrementalFunctionImplementation1<RetT, P1>,
Expand Down Expand Up @@ -151,63 +150,63 @@ data class IncrementalFunctionCall6<RetT, P1, P2, P3, P4, P5, P6>(
}
}

fun <RetT> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>)->RetT): () -> RetT {
fun <RetT> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>) -> RetT): () -> RetT {
val impl = IncrementalFunctionImplementation0(name, body)
val call = IncrementalFunctionCall0(impl)
return { this.readStateVariable(call) }
}

fun <RetT, P1> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1)->RetT): (P1) -> RetT {
fun <RetT, P1> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1) -> RetT): (P1) -> RetT {
val impl = IncrementalFunctionImplementation1(name, body)
return { p1 -> this.readStateVariable(IncrementalFunctionCall1(impl, p1)) }
}

fun <RetT, P1, P2> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2)->RetT): (P1, P2) -> RetT {
fun <RetT, P1, P2> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2) -> RetT): (P1, P2) -> RetT {
val impl = IncrementalFunctionImplementation2(name, body)
return { p1, p2 -> this.readStateVariable(IncrementalFunctionCall2(impl, p1, p2)) }
}
fun <RetT, P1, P2, P3> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3)->RetT): (P1, P2, P3) -> RetT {
fun <RetT, P1, P2, P3> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3) -> RetT): (P1, P2, P3) -> RetT {
val impl = IncrementalFunctionImplementation3(name, body)
return { p1, p2, p3 -> this.readStateVariable(IncrementalFunctionCall3(impl, p1, p2, p3)) }
}
fun <RetT, P1, P2, P3, P4> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4)->RetT): (P1, P2, P3, P4) -> RetT {
fun <RetT, P1, P2, P3, P4> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4) -> RetT): (P1, P2, P3, P4) -> RetT {
val impl = IncrementalFunctionImplementation4(name, body)
return { p1, p2, p3, p4 -> this.readStateVariable(IncrementalFunctionCall4(impl, p1, p2, p3, p4)) }
}
fun <RetT, P1, P2, P3, P4, P5> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4, P5)->RetT): (P1, P2, P3, P4, P5) -> RetT {
fun <RetT, P1, P2, P3, P4, P5> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4, P5) -> RetT): (P1, P2, P3, P4, P5) -> RetT {
val impl = IncrementalFunctionImplementation5(name, body)
return { p1, p2, p3, p4, p5 -> this.readStateVariable(IncrementalFunctionCall5(impl, p1, p2, p3, p4, p5)) }
}
fun <RetT, P1, P2, P3, P4, P5, P6> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4, P5, P6)->RetT): (P1, P2, P3, P4, P5, P6) -> RetT {
fun <RetT, P1, P2, P3, P4, P5, P6> IIncrementalEngine.incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4, P5, P6) -> RetT): (P1, P2, P3, P4, P5, P6) -> RetT {
val impl = IncrementalFunctionImplementation6(name, body)
return { p1, p2, p3, p4, p5, p6 -> this.readStateVariable(IncrementalFunctionCall6(impl, p1, p2, p3, p4, p5, p6)) }
}

fun <RetT> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>)->RetT): IncrementalFunctionCall0<RetT> {
fun <RetT> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>) -> RetT): IncrementalFunctionCall0<RetT> {
val impl = IncrementalFunctionImplementation0(name, body)
return IncrementalFunctionCall0(impl)
}
fun <RetT, P1> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1)->RetT): (P1) -> IncrementalFunctionCall1<RetT, P1> {
fun <RetT, P1> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1) -> RetT): (P1) -> IncrementalFunctionCall1<RetT, P1> {
val impl = IncrementalFunctionImplementation1(name, body)
return { p1 -> IncrementalFunctionCall1(impl, p1) }
}
fun <RetT, P1, P2> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2)->RetT): (P1, P2) -> IncrementalFunctionCall2<RetT, P1, P2> {
fun <RetT, P1, P2> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2) -> RetT): (P1, P2) -> IncrementalFunctionCall2<RetT, P1, P2> {
val impl = IncrementalFunctionImplementation2(name, body)
return { p1, p2 -> IncrementalFunctionCall2(impl, p1, p2) }
}
fun <RetT, P1, P2, P3> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3)->RetT): (P1, P2, P3) -> IncrementalFunctionCall3<RetT, P1, P2, P3> {
fun <RetT, P1, P2, P3> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3) -> RetT): (P1, P2, P3) -> IncrementalFunctionCall3<RetT, P1, P2, P3> {
val impl = IncrementalFunctionImplementation3(name, body)
return { p1, p2, p3 -> IncrementalFunctionCall3(impl, p1, p2, p3) }
}
fun <RetT, P1, P2, P3, P4> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4)->RetT): (P1, P2, P3, P4) -> IncrementalFunctionCall4<RetT, P1, P2, P3, P4> {
fun <RetT, P1, P2, P3, P4> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4) -> RetT): (P1, P2, P3, P4) -> IncrementalFunctionCall4<RetT, P1, P2, P3, P4> {
val impl = IncrementalFunctionImplementation4(name, body)
return { p1, p2, p3, p4 -> IncrementalFunctionCall4(impl, p1, p2, p3, p4) }
}
fun <RetT, P1, P2, P3, P4, P5> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4, P5)->RetT): (P1, P2, P3, P4, P5) -> IncrementalFunctionCall5<RetT, P1, P2, P3, P4, P5> {
fun <RetT, P1, P2, P3, P4, P5> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4, P5) -> RetT): (P1, P2, P3, P4, P5) -> IncrementalFunctionCall5<RetT, P1, P2, P3, P4, P5> {
val impl = IncrementalFunctionImplementation5(name, body)
return { p1, p2, p3, p4, p5 -> IncrementalFunctionCall5(impl, p1, p2, p3, p4, p5) }
}
fun <RetT, P1, P2, P3, P4, P5, P6> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4, P5, P6)->RetT): (P1, P2, P3, P4, P5, P6) -> IncrementalFunctionCall6<RetT, P1, P2, P3, P4, P5, P6> {
fun <RetT, P1, P2, P3, P4, P5, P6> incrementalFunction(name: String, body: (IIncrementalFunctionContext<RetT>, P1, P2, P3, P4, P5, P6) -> RetT): (P1, P2, P3, P4, P5, P6) -> IncrementalFunctionCall6<RetT, P1, P2, P3, P4, P5, P6> {
val impl = IncrementalFunctionImplementation6(name, body)
return { p1, p2, p3, p4, p5, p6 -> IncrementalFunctionCall6(impl, p1, p2, p3, p4, p5, p6) }
}
Loading

0 comments on commit 5319ee4

Please sign in to comment.