-
Notifications
You must be signed in to change notification settings - Fork 1k
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
ActorGraphInterpreter Box Caching #7116
Draft
to11mtm
wants to merge
5
commits into
akkadotnet:dev
Choose a base branch
from
to11mtm:actorgraphinterpreter-fix-boxing-add-pooling
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,11 @@ | |
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using Akka.Actor; | ||
using Akka.Annotations; | ||
using Akka.Event; | ||
|
@@ -21,6 +23,39 @@ | |
// ReSharper disable MemberHidesStaticFromOuterClass | ||
namespace Akka.Streams.Implementation.Fusing | ||
{ | ||
[InternalApi] | ||
public sealed class ImperfectPerformantPool<T> | ||
{ | ||
private readonly int _maxItems; | ||
private readonly ConcurrentQueue<T> _itemSet; | ||
private int _curr = 0; | ||
|
||
public ImperfectPerformantPool(int? maxItems = null) | ||
{ | ||
_maxItems = maxItems ?? Environment.ProcessorCount; | ||
_itemSet = new ConcurrentQueue<T>(); | ||
} | ||
|
||
public void TryAdd(T item) | ||
{ | ||
if (_curr + 1 <= _maxItems) | ||
{ | ||
_curr = _curr + 1; | ||
_itemSet.Enqueue(item); | ||
} | ||
} | ||
|
||
public bool TryGetValue(out T item) | ||
{ | ||
if (_itemSet.TryDequeue(out item)) | ||
{ | ||
_curr = _curr - 1; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
/// <summary> | ||
/// INTERNAL API | ||
/// </summary> | ||
|
@@ -98,11 +133,63 @@ public override string ToString() => "GraphModule\n" + | |
[InternalApi] | ||
public sealed class GraphInterpreterShell | ||
{ | ||
internal sealed class BoxedRequestMore : ActorGraphInterpreter.IBoundaryEvent | ||
{ | ||
public ActorGraphInterpreter.RequestMore Boxed { get; private set; } | ||
|
||
public BoxedRequestMore() | ||
{ | ||
|
||
} | ||
public BoxedRequestMore SetBox(ActorGraphInterpreter.RequestMore value) | ||
{ | ||
Boxed = value; | ||
return this; | ||
} | ||
|
||
public GraphInterpreterShell Shell => Boxed.Shell; | ||
} | ||
internal sealed class BoxedOnNext : ActorGraphInterpreter.IBoundaryEvent | ||
{ | ||
public ActorGraphInterpreter.OnNext Boxed { get; private set; } | ||
|
||
public BoxedOnNext() | ||
{ | ||
|
||
} | ||
public BoxedOnNext SetBox(ActorGraphInterpreter.OnNext value) | ||
{ | ||
Boxed = value; | ||
return this; | ||
} | ||
|
||
public GraphInterpreterShell Shell => Boxed.Shell; | ||
} | ||
private sealed class BoxedAsyncInput : ActorGraphInterpreter.IBoundaryEvent | ||
{ | ||
public ActorGraphInterpreter.AsyncInput Boxed { get; private set; } | ||
public BoxedAsyncInput() | ||
{ | ||
|
||
} | ||
public BoxedAsyncInput SetBox(ActorGraphInterpreter.AsyncInput value) | ||
{ | ||
Boxed = value; | ||
return this; | ||
} | ||
|
||
public GraphInterpreterShell Shell => Boxed.Shell; | ||
} | ||
private readonly GraphAssembly _assembly; | ||
private readonly Connection[] _connections; | ||
private readonly GraphStageLogic[] _logics; | ||
private readonly Shape _shape; | ||
private readonly ActorMaterializerSettings _settings; | ||
|
||
private readonly ImperfectPerformantPool<BoxedOnNext> _onNextPool = | ||
new ImperfectPerformantPool<BoxedOnNext>(); | ||
private readonly ImperfectPerformantPool<BoxedRequestMore> _requestMorePool = | ||
new ImperfectPerformantPool<BoxedRequestMore>(); | ||
/// <summary> | ||
/// TBD | ||
/// </summary> | ||
|
@@ -138,6 +225,8 @@ public sealed class GraphInterpreterShell | |
private bool _interpreterCompleted; | ||
private readonly ActorGraphInterpreter.Resume _resume; | ||
|
||
private readonly ImperfectPerformantPool<BoxedAsyncInput> | ||
_asyncInputPool = new ImperfectPerformantPool<BoxedAsyncInput>(); | ||
/// <summary> | ||
/// TBD | ||
/// </summary> | ||
|
@@ -156,8 +245,8 @@ public GraphInterpreterShell(GraphAssembly assembly, Connection[] connections, G | |
_settings = settings; | ||
Materializer = materializer; | ||
|
||
_inputs = new ActorGraphInterpreter.BatchingActorInputBoundary[shape.Inlets.Count()]; | ||
_outputs = new ActorGraphInterpreter.IActorOutputBoundary[shape.Outlets.Count()]; | ||
_inputs = new ActorGraphInterpreter.BatchingActorInputBoundary[shape.Inlets.Length]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM |
||
_outputs = new ActorGraphInterpreter.IActorOutputBoundary[shape.Outlets.Length]; | ||
_subscribersPending = _inputs.Length; | ||
_publishersPending = _outputs.Length; | ||
_shellEventLimit = settings.MaxInputBufferSize * (assembly.Inlets.Length + assembly.Outlets.Length); | ||
|
@@ -264,30 +353,38 @@ public int Receive(ActorGraphInterpreter.IBoundaryEvent e, int eventLimit) | |
// Cases that are most likely on the hot path, in decreasing order of frequency | ||
switch (e) | ||
{ | ||
case ActorGraphInterpreter.OnNext onNext: | ||
if (IsDebug) Console.WriteLine($"{Interpreter.Name} OnNext {onNext.Event} id={onNext.Id}"); | ||
_inputs[onNext.Id].OnNext(onNext.Event); | ||
return RunBatch(eventLimit); | ||
|
||
case ActorGraphInterpreter.RequestMore requestMore: | ||
if (IsDebug) Console.WriteLine($"{Interpreter.Name} Request {requestMore.Demand} id={requestMore.Id}"); | ||
_outputs[requestMore.Id].RequestMore(requestMore.Demand); | ||
return RunBatch(eventLimit); | ||
|
||
case BoxedOnNext bOn: | ||
var asO = bOn.Boxed; | ||
bOn.SetBox(default); | ||
_onNextPool.TryAdd(bOn); | ||
return RunOnNextMeth(asO); | ||
break; | ||
// Case unused: | ||
//case ActorGraphInterpreter.OnNext onNext: | ||
// return RunOnNextMeth(onNext); | ||
|
||
//case ActorGraphInterpreter.RequestMore requestMore: | ||
// return RunRequestMore(requestMore); | ||
case BoxedRequestMore bRm: | ||
var asR = bRm.Boxed; | ||
bRm.SetBox(default); | ||
_requestMorePool.TryAdd(bRm); | ||
return RunRequestMore(asR); | ||
|
||
case ActorGraphInterpreter.Resume _: | ||
if (IsDebug) Console.WriteLine($"{Interpreter.Name} Resume"); | ||
if (Interpreter.IsSuspended) | ||
return RunBatch(eventLimit); | ||
return eventLimit; | ||
|
||
case ActorGraphInterpreter.AsyncInput asyncInput: | ||
Interpreter.RunAsyncInput(asyncInput.Logic, asyncInput.Event, asyncInput.Handler); | ||
if (eventLimit == 1 && _interpreter.IsSuspended) | ||
{ | ||
SendResume(true); | ||
return 0; | ||
} | ||
return RunBatch(eventLimit - 1); | ||
case BoxedAsyncInput bAi: | ||
var asI = bAi.Boxed; | ||
bAi.SetBox(default); | ||
_asyncInputPool.TryAdd(bAi); | ||
return RunAsyncInputMeth(asI); | ||
|
||
//case ActorGraphInterpreter.AsyncInput asyncInput: | ||
// return RunAsyncInputMeth(asyncInput); | ||
|
||
case ActorGraphInterpreter.OnError onError: | ||
if (IsDebug) Console.WriteLine($"{Interpreter.Name} OnError id={onError.Id}"); | ||
|
@@ -320,7 +417,32 @@ public int Receive(ActorGraphInterpreter.IBoundaryEvent e, int eventLimit) | |
return eventLimit; | ||
} | ||
|
||
int RunRequestMore(ActorGraphInterpreter.RequestMore requestMore1) | ||
{ | ||
if (IsDebug) Console.WriteLine($"{Interpreter.Name} Request {requestMore1.Demand} id={requestMore1.Id}"); | ||
_outputs[requestMore1.Id].RequestMore(requestMore1.Demand); | ||
return RunBatch(eventLimit); | ||
} | ||
|
||
int RunOnNextMeth(ActorGraphInterpreter.OnNext onNext1) | ||
{ | ||
if (IsDebug) Console.WriteLine($"{Interpreter.Name} OnNext {onNext1.Event} id={onNext1.Id}"); | ||
_inputs[onNext1.Id].OnNext(onNext1.Event); | ||
return RunBatch(eventLimit); | ||
} | ||
|
||
return eventLimit; | ||
|
||
int RunAsyncInputMeth(ActorGraphInterpreter.AsyncInput asyncInput) | ||
{ | ||
Interpreter.RunAsyncInput(asyncInput.Logic, asyncInput.Event, asyncInput.Handler); | ||
if (eventLimit == 1 && _interpreter.IsSuspended) | ||
{ | ||
SendResume(true); | ||
return 0; | ||
} | ||
return RunBatch(eventLimit - 1); | ||
} | ||
} | ||
#pragma warning restore CS0162 | ||
|
||
|
@@ -411,12 +533,25 @@ private GraphInterpreter GetInterpreter() | |
return new GraphInterpreter(_assembly, Materializer, Log, _logics, _connections, | ||
(logic, @event, handler) => | ||
{ | ||
var asyncInput = new ActorGraphInterpreter.AsyncInput(this, logic, @event, handler); | ||
if (_asyncInputPool.TryGetValue(out var bAi) == false) | ||
{ | ||
bAi = new BoxedAsyncInput(); | ||
} | ||
var currentInterpreter = CurrentInterpreterOrNull; | ||
if (currentInterpreter == null || !Equals(currentInterpreter.Context, Self)) | ||
Self.Tell(new ActorGraphInterpreter.AsyncInput(this, logic, @event, handler)); | ||
if (currentInterpreter == null || | ||
!Equals(currentInterpreter.Context, Self)) | ||
{ | ||
Self.Tell(bAi.SetBox( | ||
new ActorGraphInterpreter.AsyncInput(this, | ||
logic, @event, handler))); | ||
//Self.Tell(new ActorGraphInterpreter.AsyncInput(this, | ||
// logic, @event, handler)); | ||
} | ||
else | ||
_enqueueToShortCircuit(asyncInput); | ||
_enqueueToShortCircuit(bAi.SetBox( | ||
new ActorGraphInterpreter.AsyncInput(this, | ||
logic, @event, handler))); | ||
//_enqueueToShortCircuit( new ActorGraphInterpreter.AsyncInput(this, logic, @event, handler)); | ||
}, _settings.IsFuzzingMode, Self); | ||
} | ||
|
||
|
@@ -430,6 +565,26 @@ private ILoggingAdapter GetLogger() | |
/// </summary> | ||
/// <returns>TBD</returns> | ||
public override string ToString() => $"GraphInterpreterShell\n {_assembly.ToString().Replace("\n", "\n ")}"; | ||
|
||
internal BoxedOnNext GetNextBuffer() | ||
{ | ||
if (_onNextPool.TryGetValue(out var rV) == false) | ||
{ | ||
rV = new BoxedOnNext(); | ||
} | ||
|
||
return rV; | ||
} | ||
|
||
internal BoxedRequestMore GetNextRequestMore() | ||
{ | ||
if (_requestMorePool.TryGetValue(out var rV) == false) | ||
{ | ||
rV = new BoxedRequestMore(); | ||
} | ||
|
||
return rV; | ||
} | ||
} | ||
|
||
/// <summary> | ||
|
@@ -736,7 +891,7 @@ public AsyncInput(GraphInterpreterShell shell, GraphStageLogic logic, object @ev | |
/// <summary> | ||
/// TBD | ||
/// </summary> | ||
public readonly struct Resume : IBoundaryEvent | ||
public class Resume : IBoundaryEvent | ||
{ | ||
/// <summary> | ||
/// TBD | ||
|
@@ -828,7 +983,12 @@ public BoundarySubscription(IActorRef parent, GraphInterpreterShell shell, int i | |
/// TBD | ||
/// </summary> | ||
/// <param name="elements">TBD</param> | ||
public void Request(long elements) => _parent.Tell(new RequestMore(_shell, _id, elements)); | ||
public void Request(long elements) | ||
{ | ||
var bRm = _shell.GetNextRequestMore(); | ||
_parent.Tell(bRm.SetBox(new RequestMore(_shell, _id, elements))); | ||
//_parent.Tell(new RequestMore(_shell, _id, elements)); | ||
} | ||
|
||
/// <summary> | ||
/// TBD | ||
|
@@ -899,7 +1059,8 @@ public void OnError(Exception cause) | |
public void OnNext(T element) | ||
{ | ||
ReactiveStreamsCompliance.RequireNonNullElement(element); | ||
_parent.Tell(new OnNext(_shell, _id, element)); | ||
_parent.Tell(_shell.GetNextBuffer().SetBox(new OnNext(_shell, _id, element))); | ||
//_parent.Tell(new OnNext(_shell, _id, element)); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@to11mtm can you describe the approach / "why do it this way?" for me just so I understand the thinking behind this.