-
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
Akka.Streams: memory-optimize ActorMaterializer
HOCON injection
#6440
Merged
Aaronontheweb
merged 5 commits into
akkadotnet:dev
from
Aaronontheweb:materializer-hocon-injection
Feb 24, 2023
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
90b1cfd
optimize `ActorMaterializer` to only inject HOCON configuration once
Aaronontheweb 7f9411e
modernized syntax
Aaronontheweb 73eb93e
improve cache to detect multiple `ActorSystem` instances
Aaronontheweb b0049e7
Merge branch 'dev' into materializer-hocon-injection
Aaronontheweb 85e2ae0
Merge branch 'dev' into materializer-hocon-injection
Aaronontheweb 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,7 +6,9 @@ | |
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Runtime.Serialization; | ||
using System.Threading.Tasks; | ||
using Akka.Actor; | ||
using Akka.Configuration; | ||
using Akka.Dispatch; | ||
|
@@ -43,6 +45,18 @@ public static Config DefaultConfig() | |
|
||
#region static | ||
|
||
/// <summary> | ||
/// Injecting the top-level Materializer HOCON configuration over and over again is expensive, so we want to avoid | ||
/// doing it each time a materializer is instantiated. This flag will be set to true once the configuration has been | ||
/// injected the first time. | ||
/// </summary> | ||
private static readonly ConcurrentDictionary<ActorSystem, bool> InjectedConfig = new(); | ||
|
||
/// <summary> | ||
/// Cache the default materializer settings so we don't constantly parse them | ||
/// </summary> | ||
private static readonly ConcurrentDictionary<ActorSystem, ActorMaterializerSettings> DefaultSettings = new(); | ||
|
||
/// <summary> | ||
/// <para> | ||
/// Creates a ActorMaterializer which will execute every step of a transformation | ||
|
@@ -75,9 +89,28 @@ public static ActorMaterializer Create(IActorRefFactory context, ActorMaterializ | |
var haveShutDown = new AtomicBoolean(); | ||
var system = ActorSystemOf(context); | ||
|
||
system.Settings.InjectTopLevelFallback(DefaultConfig()); | ||
if(!InjectedConfig.TryGetValue(system, out _) && InjectedConfig.TryAdd(system, true)) | ||
{ | ||
// Inject the top-level fallback config for the Materializer once, and only once. | ||
// This is a performance optimization to avoid having to do this on every materialization. | ||
system.Settings.InjectTopLevelFallback(DefaultConfig()); | ||
|
||
settings = settings ?? ActorMaterializerSettings.Create(system); | ||
static async Task CleanUp(ActorSystem sys) | ||
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. Added a static delegate to cleanup |
||
{ | ||
// remove ActorSystem from cache when it terminates so we don't leak memory | ||
await sys.WhenTerminated.ConfigureAwait(false); | ||
InjectedConfig.TryRemove(sys, out _); | ||
DefaultSettings.TryRemove(sys, out _); | ||
} | ||
|
||
#pragma warning disable CS4014 | ||
CleanUp(system); | ||
#pragma warning restore CS4014 | ||
|
||
} | ||
|
||
// use the default settings if none have been passed in | ||
settings ??= DefaultSettings.GetOrAdd(system, ActorMaterializerSettings.Create); | ||
|
||
return new ActorMaterializerImpl( | ||
system: system, | ||
|
@@ -90,14 +123,14 @@ public static ActorMaterializer Create(IActorRefFactory context, ActorMaterializ | |
|
||
private static ActorSystem ActorSystemOf(IActorRefFactory context) | ||
{ | ||
if (context is ExtendedActorSystem) | ||
return (ActorSystem)context; | ||
if (context is IActorContext) | ||
return ((IActorContext)context).System; | ||
if (context == null) | ||
throw new ArgumentNullException(nameof(context), "IActorRefFactory must be defined"); | ||
|
||
throw new ArgumentException($"ActorRefFactory context must be a ActorSystem or ActorContext, got [{context.GetType()}]"); | ||
return context switch | ||
{ | ||
ExtendedActorSystem system => system, | ||
IActorContext actorContext => actorContext.System, | ||
null => throw new ArgumentNullException(nameof(context), "IActorRefFactory must be defined"), | ||
_ => throw new ArgumentException( | ||
$"ActorRefFactory context must be a ActorSystem or ActorContext, got [{context.GetType()}]") | ||
}; | ||
} | ||
|
||
#endregion | ||
|
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.
This would be unnecessary if the materializer were an
ActorSystemExtension
, but since it's not - have to do it this way.