Skip to content

Commit

Permalink
Optimize intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
Iliya-usov committed Oct 19, 2023
1 parent 7634b15 commit 3cdf2b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ sealed class Lifetime {
* Creates an intersection of some lifetimes: new lifetime that terminate when either one terminates.
* Created lifetime inherits the smallest [terminationTimeoutKind]
*/
fun intersect(lifetime1: Lifetime, lifetime2: Lifetime): Lifetime = defineIntersection(lifetime1, lifetime2).lifetime
fun intersect(lifetime1: Lifetime, lifetime2: Lifetime): Lifetime {
if (lifetime1 === lifetime2 || lifetime2.isEternal)
return lifetime1

if (lifetime1.isEternal)
return lifetime2

return defineIntersection(lifetime1, lifetime2).lifetime
}

/**
* Creates an intersection of some lifetimes: new lifetime that terminate when either one terminates.
Expand Down Expand Up @@ -642,7 +650,8 @@ operator fun Lifetime.plusAssign(action : () -> Unit) = onTermination(action)
* Creates an intersection of some lifetimes: new lifetime that terminate when either one terminates.
* Created lifetime inherits the smallest [terminationTimeoutKind]
*/
fun Lifetime.intersect(lifetime: Lifetime): LifetimeDefinition = Lifetime.defineIntersection(this, lifetime)
fun Lifetime.intersect(lifetime: Lifetime): Lifetime = Lifetime.intersect(this, lifetime)
fun Lifetime.defineIntersection(lifetime: Lifetime): LifetimeDefinition = Lifetime.defineIntersection(this, lifetime)

inline fun <T> Lifetime.view(viewable: IViewable<T>, crossinline handler: Lifetime.(T) -> Unit) {
viewable.view(this) { lt, value -> lt.handler(value) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class WriteOnceProperty<T : Any> : IOptProperty<T> {
if (def.isNotAlive || lifetime.isNotAlive) return

val nestedDef = def.intersect(lifetime)
super.advise(nestedDef.lifetime, handler)
super.advise(nestedDef, handler)
}

override fun fire(value: T) {
Expand Down

0 comments on commit 3cdf2b8

Please sign in to comment.