Skip to content

Commit

Permalink
Store children in a state, and destroy finished reactors
Browse files Browse the repository at this point in the history
  • Loading branch information
axmmisaka committed Sep 7, 2023
1 parent c21b29b commit 10761eb
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions src/benchmark/quicksort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class QuickSorter extends Reactor {
resultArr: State<number[]>;
numFragments: State<number>;

leftReactor: Reactor | undefined;
rightReactor: Reactor | undefined;
leftReactor: State<QuickSorter | undefined>;
rightReactor: State<QuickSorter | undefined>;

constructor(parent: Reactor) {
super(parent);
Expand All @@ -52,6 +52,9 @@ class QuickSorter extends Reactor {
this.resultArr = new State([]);
this.numFragments = new State(0);

this.leftReactor = new State(undefined);
this.rightReactor = new State(undefined);

// When the parent sends a message, we send it to children.
this.addMutation(
[this.parentReadPort],
Expand All @@ -63,7 +66,9 @@ class QuickSorter extends Reactor {
this.leftReadPort,
this.rightReadPort,
this.resultArr,
this.numFragments
this.numFragments,
this.leftReactor,
this.rightReactor
],
function (
this,
Expand All @@ -74,7 +79,9 @@ class QuickSorter extends Reactor {
leftread,
rightread,
resultArr,
numFragments
numFragments,
stateLeftReactor, // This is really cursed, but s_ is to indicate that this is a state
stateRightReactor
) {
const hierarchyImplementation = (
useHierarchy ? this.addChild : this.addSibling
Expand Down Expand Up @@ -105,6 +112,9 @@ class QuickSorter extends Reactor {
const leftReactor = hierarchyImplementation(QuickSorter);
const rightReactor = hierarchyImplementation(QuickSorter);

stateLeftReactor.set(leftReactor);
stateRightReactor.set(rightReactor);

// Connect ports accoringly
this.connect(leftWritePort, leftReactor.parentReadPort);
this.connect(rightWritePort, rightReactor.parentReadPort);
Expand All @@ -117,17 +127,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,
stateLeftReactor
) {
const leftResult = leftreadport.get();
const myResult = resultArr.get();
const leftReactor = stateLeftReactor.get();
if (leftResult == null) {
throw Error("Left return null");
}
Expand All @@ -136,28 +155,43 @@ 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);
stateLeftReactor.set(undefined);

numFragments.set(numFragments.get() + 1);
if (numFragments.get() === 3) {
parentWrite.set(resultArr.get());
}
}
);

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,
stateRightReactor
) {
const rightResult = rightreadport.get();
const myResult = resultArr.get();
const rightReactor = stateRightReactor.get();
if (rightResult == null) {
throw Error("Right return null");
}
Expand All @@ -166,10 +200,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);
stateRightReactor.set(undefined);

numFragments.set(numFragments.get() + 1);
if (numFragments.get() === 3) {
parentWrite.set(resultArr.get());
Expand Down

0 comments on commit 10761eb

Please sign in to comment.