diff --git a/src/benchmark/quicksort-sibling.ts b/src/benchmark/quicksort-sibling.ts index 02a81f491..bcfff37e4 100644 --- a/src/benchmark/quicksort-sibling.ts +++ b/src/benchmark/quicksort-sibling.ts @@ -9,7 +9,8 @@ import { App, TimeValue, Origin, - Log + Log, + PrecedenceGraph } from "../core/internal"; // This is the thrshold for the quicksort algorithm, feeding sorters below this number will use Array.prototype.sort() @@ -32,7 +33,7 @@ class QuickSorter extends Reactor { leftReactor: Reactor | undefined; rightReactor: Reactor | undefined; - constructor(parent: Reactor, name = "Innocent QuickSorter") { + constructor(parent: Reactor, name = "root") { super(parent, name); this.parentReadPort = new InPort(this); this.parentWritePort = new OutPort(this); @@ -47,33 +48,46 @@ class QuickSorter extends Reactor { this.addMutation( [this.parentReadPort], [this.parentReadPort, - this.parentWritePort, + this.writable(this.parentWritePort), this.leftWritePort, - this.rightWritePort + this.rightWritePort, + this.leftReadPort, + this.rightReadPort ], - function (this, parentReadPort, parentWritePort, leftWritePort, rightWritePort) { + function (this, parentReadPort, parentWritePort, leftWritePort, rightWritePort, leftread, rightread) { const fullarr = parentReadPort.get(); if (fullarr == null) { throw Error("Received null from port") } if (fullarr.length <= T) { const sorted = [...fullarr].sort((a, b) => (a - b)); - this.getReactor().writable(parentWritePort).set(sorted); + parentWritePort.set(sorted); return; } const pivot = fullarr[0]; const leftToSort = fullarr.filter((val) => (val <= pivot)); const righttoSort = fullarr.filter((val) => (val > pivot)); + // Hack: if either of them is empty, this is not a good pivot. + // Instead of choosing another pivot, we simply sort it. + if (leftToSort.length === 0 || righttoSort.length === 0) { + const sorted = [...fullarr].sort((a, b) => (a - b)); + parentWritePort.set(sorted); + return; + } console.log(`I received a request! ${fullarr}! Pivot is ${pivot}, so I divided it into ${leftToSort} and ${righttoSort}`); // First, create 2 new reactors - const leftReactor = this.getReactor()._uncheckedAddSibling(QuickSorter); - const rightReactor = this.getReactor()._uncheckedAddSibling(QuickSorter); + const leftReactor = this.getReactor()._uncheckedAddSibling(QuickSorter, `${this.getReactor()._name}/l`); + const rightReactor = this.getReactor()._uncheckedAddSibling(QuickSorter, `${this.getReactor()._name}/r`); // Connect ports accoringly this.connect(leftWritePort, leftReactor.parentReadPort); this.connect(rightWritePort, rightReactor.parentReadPort); + console.log("000", arb["_getPrecedenceGraph"]().toMermaidString([[leftReactor.parentWritePort, leftread]])); + + this.connect(leftReactor.parentWritePort, leftread); + this.connect(rightReactor.parentWritePort, rightread); this.getReactor().writable(leftWritePort).set(leftToSort); this.getReactor().writable(rightWritePort).set(righttoSort); @@ -179,8 +193,8 @@ class Arbiter extends App { fail?: () => void ) { super(timeout, keepAlive, fast, success, fail, name); - this.rootSorter = new QuickSorter(this, "rootroot"); - this.supplier = new Supplier(this, [5, 1, 4, 1, 1, 4, 8, 1, 0, 1, 9, 1, 9]); + this.rootSorter = new QuickSorter(this, "root"); + this.supplier = new Supplier(this, [578, 530, 482, 105, 400, 787, 563, 613, 483, 888, 439, 928, 857, 404, 949, 736, 68, 761, 951, 432, 799, 212, 108, 937, 562, 616, 436, 358, 221, 315, 423, 539, 215, 795, 409, 227, 715, 847, 66, 242, 168, 637, 572, 468, 116, 668, 213, 859, 880, 291, 609, 502, 486, 710, 662, 172, 991, 631, 120, 905, 751, 293, 411, 503, 901, 53, 774, 145, 831, 140, 592, 184, 228, 111, 907, 640, 553, 519, 579, 389, 735, 545, 975, 255, 83, 449, 673, 427, 369, 854, 86, 33, 885, 940, 904, 764, 834, 250, 183, 191]); this._connect(this.supplier.rootWritePort, this.rootSorter.parentReadPort); this._connect(this.rootSorter.parentWritePort, this.supplier.rootReadPort); }