Avoid proxying for a significant speed boost

This commit is contained in:
Tony Garnock-Jones 2020-12-09 20:27:12 +01:00
parent 76c1ad3268
commit a9c8ded1db
3 changed files with 22 additions and 19 deletions

View File

@ -1,26 +1,26 @@
package org.syndicate_lang.actors.example.example2;
import java.util.List;
import org.syndicate_lang.actors.Remote;
// import java.util.Random;
public class Forwarder implements IForwarder {
private final IForwarder _main;
private final Remote<IForwarder> _main;
private final int _nRounds;
private IForwarder _peer = null;
private Remote<IForwarder> _peer = null;
public Forwarder(IForwarder main, int nRounds) {
public Forwarder(Remote<IForwarder> main, int nRounds) {
this._main = main;
this._nRounds = nRounds;
}
@Override
public void setPeer(IForwarder peer) {
public void setPeer(Remote<IForwarder> peer) {
this._peer = peer;
}
@Override
public void handleMessage(int hopCount) {
IForwarder target = hopCount >= this._nRounds - 1 ? _main : _peer;
target.handleMessage(hopCount + 1);
public void handleMessage(final int hopCount) {
Remote<IForwarder> target = hopCount >= this._nRounds - 1 ? _main : _peer;
target.async(f -> f.handleMessage(hopCount + 1));
}
}

View File

@ -1,8 +1,9 @@
package org.syndicate_lang.actors.example.example2;
import org.syndicate_lang.actors.Actor;
import org.syndicate_lang.actors.Remote;
public interface IForwarder {
void setPeer(IForwarder peer);
void setPeer(Remote<IForwarder> peer);
void handleMessage(int hopCount);
}

View File

@ -29,25 +29,27 @@ public class Main implements IForwarder {
public void boot() {
Actor.log().info("Available processors: " + Runtime.getRuntime().availableProcessors());
List<IForwarder> _actors = new ArrayList<>();
final IForwarder me = Actor.ref(this).asyncProxy(IForwarder.class);
IForwarder previous = null;
final List<Remote<IForwarder>> _actors = new ArrayList<>();
final Remote<IForwarder> me = Actor.ref(this);
Remote<IForwarder> previous = null;
for (int i = 0; i < _nActors; i++) {
Remote<IForwarder> a = Actor.forObject(new Forwarder(me, this._nRounds));
a.getActor().link();
IForwarder current = a.asyncProxy(IForwarder.class);
Remote<IForwarder> current = Actor.forObject(new Forwarder(me, this._nRounds));
current.getActor().link();
_actors.add(current);
if (previous != null) current.setPeer(previous);
if (previous != null) {
final var p = previous;
current.async(f -> f.setPeer(p));
}
previous = current;
}
_actors.get(0).setPeer(_actors.get(_nActors - 1));
_actors.get(0).async(f -> f.setPeer(_actors.get(_nActors - 1)));
Actor.log().info("Start");
this._startTime = System.currentTimeMillis();
_actors.forEach(a -> a.handleMessage(0));
_actors.forEach(a -> a.async(f-> f.handleMessage(0)));
}
@Override
public void setPeer(IForwarder peer) {
public void setPeer(Remote<IForwarder> peer) {
// Do nothing.
}