Skip to content

Commit

Permalink
Eliminate DefaultLogMessageFormatter allocations (#6166)
Browse files Browse the repository at this point in the history
* save on `DefaultLogMessageFormatter` allocations

* API approval
  • Loading branch information
Aaronontheweb committed Oct 11, 2022
1 parent c508e3e commit d82cdc2
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/benchmark/Akka.Benchmarks/Logging/LoggingBenchmarks.cs
Expand Up @@ -25,7 +25,7 @@ private sealed class BenchmarkLogAdapter : LoggingAdapterBase
private readonly string _logSource;
private readonly Type _logClass;

public BenchmarkLogAdapter(int capacity) : base(new DefaultLogMessageFormatter())
public BenchmarkLogAdapter(int capacity) : base(DefaultLogMessageFormatter.Instance)
{
AllLogs = new LogEvent[capacity];
_logSource = LogSource.Create(this).Source;
Expand Down
Expand Up @@ -2986,7 +2986,7 @@ namespace Akka.Event
}
public class DefaultLogMessageFormatter : Akka.Event.ILogMessageFormatter
{
public DefaultLogMessageFormatter() { }
public static readonly Akka.Event.DefaultLogMessageFormatter Instance;
public string Format(string format, params object[] args) { }
}
public class DefaultLogger : Akka.Actor.ActorBase, Akka.Dispatch.IRequiresMessageQueue<Akka.Event.ILoggerMessageQueueSemantics>
Expand Down
Expand Up @@ -2991,7 +2991,7 @@ namespace Akka.Event
}
public class DefaultLogMessageFormatter : Akka.Event.ILogMessageFormatter
{
public DefaultLogMessageFormatter() { }
public static readonly Akka.Event.DefaultLogMessageFormatter Instance;
public string Format(string format, params object[] args) { }
}
public class DefaultLogger : Akka.Actor.ActorBase, Akka.Dispatch.IRequiresMessageQueue<Akka.Event.ILoggerMessageQueueSemantics>
Expand Down
Expand Up @@ -2986,7 +2986,7 @@ namespace Akka.Event
}
public class DefaultLogMessageFormatter : Akka.Event.ILogMessageFormatter
{
public DefaultLogMessageFormatter() { }
public static readonly Akka.Event.DefaultLogMessageFormatter Instance;
public string Format(string format, params object[] args) { }
}
public class DefaultLogger : Akka.Actor.ActorBase, Akka.Dispatch.IRequiresMessageQueue<Akka.Event.ILoggerMessageQueueSemantics>
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Streams.Tests/Dsl/FlowLogSpec.cs
Expand Up @@ -107,7 +107,7 @@ public void A_Log_on_source_must_log_upstream_failure()
[Fact]
public void A_Log_on_source_must_allow_passing_in_custom_LoggingAdapter()
{
var log = new BusLogging(Sys.EventStream, "com.example.ImportantLogger", LogType, new DefaultLogMessageFormatter());
var log = new BusLogging(Sys.EventStream, "com.example.ImportantLogger", LogType, DefaultLogMessageFormatter.Instance);

Source.Single(42)
.Log("flow-5", log: log)
Expand Down
Expand Up @@ -422,7 +422,7 @@ private GraphInterpreter GetInterpreter()

private BusLogging GetLogger()
{
return new BusLogging(Materializer.System.EventStream, Self.ToString(), typeof(GraphInterpreterShell), new DefaultLogMessageFormatter());
return new BusLogging(Materializer.System.EventStream, Self.ToString(), typeof(GraphInterpreterShell), DefaultLogMessageFormatter.Instance);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Streams/Implementation/Fusing/Ops.cs
Expand Up @@ -2948,7 +2948,7 @@ public override void PreStart()
try
{
var materializer = ActorMaterializerHelper.Downcast(Materializer);
_log = new BusLogging(materializer.System.EventStream, _stage._name, GetType(), new DefaultLogMessageFormatter());
_log = new BusLogging(materializer.System.EventStream, _stage._name, GetType(), DefaultLogMessageFormatter.Instance);
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Tests/Loggers/LoggerSpec.cs
Expand Up @@ -136,7 +136,7 @@ public static IEnumerable<object[]> LogEventFactory()
var logSource = LogSource.Create(nameof(LoggerSpec));
var ls = logSource.Source;
var lc = logSource.Type;
var formatter = new DefaultLogMessageFormatter();
var formatter = DefaultLogMessageFormatter.Instance;

yield return new object[] { new Error(ex, ls, lc, new LogMessage(formatter, Case.t, Case.p)) };

Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka/Actor/Internal/ActorSystemImpl.cs
Expand Up @@ -464,7 +464,7 @@ private void ConfigureProvider()

private void ConfigureLoggers()
{
_log = new BusLogging(_eventStream, "ActorSystem(" + _name + ")", GetType(), new DefaultLogMessageFormatter());
_log = new BusLogging(_eventStream, "ActorSystem(" + _name + ")", GetType(), DefaultLogMessageFormatter.Instance);
}

private void ConfigureDispatchers()
Expand Down
3 changes: 3 additions & 0 deletions src/core/Akka/Event/DefaultLogMessageFormatter.cs
Expand Up @@ -12,6 +12,9 @@ namespace Akka.Event
/// </summary>
public class DefaultLogMessageFormatter : ILogMessageFormatter
{
public static readonly DefaultLogMessageFormatter Instance = new DefaultLogMessageFormatter();
private DefaultLogMessageFormatter(){}

/// <summary>
/// Formats a specified composite string using an optional list of item substitutions.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions src/core/Akka/Event/Logging.cs
Expand Up @@ -213,7 +213,7 @@ internal static ILoggingAdapter GetLoggerStartup(this IActorContext context, ILo
catch // had a failure, don't want to propagate it. Just start the logger without remote context
{
var logSource = LogSource.Create(context);
return new BusLogging(context.System.EventStream, logSource.Source, logSource.Type, logMessageFormatter ?? new DefaultLogMessageFormatter());
return new BusLogging(context.System.EventStream, logSource.Source, logSource.Type, logMessageFormatter ?? DefaultLogMessageFormatter.Instance);
}
}

Expand All @@ -226,7 +226,7 @@ internal static ILoggingAdapter GetLoggerStartup(this IActorContext context, ILo
public static ILoggingAdapter GetLogger(this IActorContext context, ILogMessageFormatter logMessageFormatter = null)
{
var logSource = LogSource.Create(context, context.System);
return new BusLogging(context.System.EventStream, logSource.Source, logSource.Type, logMessageFormatter ?? new DefaultLogMessageFormatter());
return new BusLogging(context.System.EventStream, logSource.Source, logSource.Type, logMessageFormatter ?? DefaultLogMessageFormatter.Instance);
}

/// <summary>
Expand All @@ -239,7 +239,7 @@ public static ILoggingAdapter GetLogger(this IActorContext context, ILogMessageF
public static ILoggingAdapter GetLogger(ActorSystem system, object logSourceObj, ILogMessageFormatter logMessageFormatter = null)
{
var logSource = LogSource.Create(logSourceObj, system);
return new BusLogging(system.EventStream, logSource.Source, logSource.Type, logMessageFormatter ?? new DefaultLogMessageFormatter());
return new BusLogging(system.EventStream, logSource.Source, logSource.Type, logMessageFormatter ?? DefaultLogMessageFormatter.Instance);
}

/// <summary>
Expand All @@ -252,7 +252,7 @@ public static ILoggingAdapter GetLogger(ActorSystem system, object logSourceObj,
public static ILoggingAdapter GetLogger(LoggingBus loggingBus, object logSourceObj, ILogMessageFormatter logMessageFormatter = null)
{
var logSource = LogSource.Create(logSourceObj);
return new BusLogging(loggingBus, logSource.Source, logSource.Type, logMessageFormatter ?? new DefaultLogMessageFormatter());
return new BusLogging(loggingBus, logSource.Source, logSource.Type, logMessageFormatter ?? DefaultLogMessageFormatter.Instance);
}

/// <summary>
Expand Down

0 comments on commit d82cdc2

Please sign in to comment.