Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract ActorCell state holders into separate object #771

Closed
rogeralsing opened this issue Mar 30, 2015 · 3 comments
Closed

Extract ActorCell state holders into separate object #771

rogeralsing opened this issue Mar 30, 2015 · 3 comments

Comments

@rogeralsing
Copy link
Contributor

Related to #766 #629 #249

Currently the ActorCell contains these state containers:

HashSet<ActorRef> _watching = new HashSet<ActorRef>();
HashSet<ActorRef> _watchedBy = new HashSet<ActorRef>();
HashSet<ActorRef> _terminatedQueue = new HashSet<ActorRef>();
Stack<Receive> behaviorStack = new Stack<Receive>();

That's quite a bit of memory that are pre allocated for each actor, even if they never use behavior stacking or watches any other actors.

If we could actract this to a dedicated type.
e.g.

interface IActorState
{
        IActorState AddWatchedBy(ActorRef watcher);
        IActorState BecomeStacked(...);
        IActorState UnbecomeStacked();

        IEnumerable<ActorRef> GetWatchedBy();
        Receive CurrentBehavior {get;}
       ...
}

Don't read too much into the actual methods there, it's just to get an idea of the concept.

If we have this, we could have one implementation that is used for the default case, which don't hold a hashset for watchers, it could just be a simple ref to the parent.
And if AddWatchedBy is called and we now need to hold two watchers, then we create an instance of another implementation that do use HashSet and Stacks.

Something like:

class DefaultActorState : IActorState
{
       private ActorRef _watchedBy;
       private Receive _behavior;

      ..implementations of the interface
}

class FullActorState : IActorState
{
   private HashSet<ActorRef> _watching = new HashSet<ActorRef>();
   private HashSet<ActorRef> _watchedBy = new HashSet<ActorRef>();
   private HashSet<ActorRef> _terminatedQueue = new HashSet<ActorRef>();
   private Stack<Receive> behaviorStack = new Stack<Receive>();

      ..implementations of the interface
}

in the ActorCell we could then implement this along the lines of :

private IActorState _state = new DefaultActorState();

public void BecomeStacked(Receive behavior)
{
         _state = _state.BecomeStacked(behavior);
}

This would cut down our memory footprint quite a bit.

Does this idea hold? any obvious drawbacks with it?

@HCanber
Copy link
Contributor

HCanber commented Mar 31, 2015

I think it could work. :)

@rogeralsing
Copy link
Contributor Author

PoC implemented here #783

@rogeralsing
Copy link
Contributor Author

Closing as this has been implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants