From a495f757739a925b510f071840f9495abcf07743 Mon Sep 17 00:00:00 2001 From: Nadeshiko Kagamihara Date: Thu, 31 Aug 2023 20:38:50 -0700 Subject: [PATCH] Destroy reactors when they finished their job --- src/benchmark/quicksort.ts | 62 ++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/src/benchmark/quicksort.ts b/src/benchmark/quicksort.ts index 279a634f5..b1aaf025a 100644 --- a/src/benchmark/quicksort.ts +++ b/src/benchmark/quicksort.ts @@ -43,8 +43,8 @@ class QuickSorter extends Reactor { resultArr: State; numFragments: State; - leftReactor: Reactor | undefined; - rightReactor: Reactor | undefined; + leftReactor: State; + rightReactor: State; constructor(parent: Reactor, name = "root") { super(parent, name); @@ -57,6 +57,9 @@ class QuickSorter extends Reactor { this.resultArr = new State([]); this.numFragments = new State(0); + this.leftReactor = new State(void 114514); + this.rightReactor = new State(void 1919810); + // When the parent sends a message, we send it to children. this.addMutation( [this.parentReadPort], @@ -68,7 +71,9 @@ class QuickSorter extends Reactor { this.leftReadPort, this.rightReadPort, this.resultArr, - this.numFragments + this.numFragments, + this.leftReactor, + this.rightReactor ], function ( this, @@ -79,7 +84,9 @@ class QuickSorter extends Reactor { leftread, rightread, resultArr, - numFragments + numFragments, + s_leftreactor, // This is really cursed, but s_ is to indicate that this is a state + s_rightreactor ) { const hierarchyImplementation = ( useHierarchy @@ -118,6 +125,9 @@ class QuickSorter extends Reactor { `${this.getReactor()._name}/r` ); + s_leftreactor.set(leftReactor); + s_rightreactor.set(rightReactor); + // Connect ports accoringly this.connect(leftWritePort, leftReactor.parentReadPort); this.connect(rightWritePort, rightReactor.parentReadPort); @@ -130,17 +140,26 @@ class QuickSorter extends Reactor { } ); - this.addReaction( + this.addMutation( [this.leftReadPort], [ this.leftReadPort, this.resultArr, this.numFragments, - this.writable(this.parentWritePort) + this.writable(this.parentWritePort), + this.leftReactor ], - function (this, leftreadport, resultArr, numFragments, parentWrite) { + function ( + this, + leftreadport, + resultArr, + numFragments, + parentWrite, + s_leftreactor + ) { const leftResult = leftreadport.get(); const myResult = resultArr.get(); + const leftReactor = s_leftreactor.get(); if (leftResult == null) { throw Error("Left return null"); } @@ -149,10 +168,16 @@ class QuickSorter extends Reactor { "Result length is 0, but should contain at least the pivots." ); } + if (leftReactor == null) { + throw Error("Right reactor is null."); + } console.log(`I received a result from my left! ${leftResult}!`); resultArr.set([...leftResult, ...myResult]); + this.delete(leftReactor); + s_leftreactor.set(void "kksk"); + numFragments.set(numFragments.get() + 1); if (numFragments.get() === 3) { parentWrite.set(resultArr.get()); @@ -160,17 +185,26 @@ class QuickSorter extends Reactor { } ); - this.addReaction( + this.addMutation( [this.rightReadPort], [ this.rightReadPort, this.resultArr, this.numFragments, - this.writable(this.parentWritePort) + this.writable(this.parentWritePort), + this.rightReactor ], - function (this, rightreadport, resultArr, numFragments, parentWrite) { + function ( + this, + rightreadport, + resultArr, + numFragments, + parentWrite, + s_rightreactor + ) { const rightResult = rightreadport.get(); const myResult = resultArr.get(); + const rightReactor = s_rightreactor.get(); if (rightResult == null) { throw Error("Right return null"); } @@ -179,10 +213,18 @@ class QuickSorter extends Reactor { "Result length is 0, but should contain at least the pivots." ); } + if (rightReactor == null) { + throw Error("Right reactor is null."); + } console.log(`I received a result from my right! ${rightResult}!`); resultArr.set([...myResult, ...rightResult]); + // Destroy right reactor and the connection + + this.delete(rightReactor); + s_rightreactor.set(void "tnok"); + numFragments.set(numFragments.get() + 1); if (numFragments.get() === 3) { parentWrite.set(resultArr.get());