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

Optimization - Use faster null checking #96

Merged
merged 2 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Serilog
/// </summary>
public static class ConsoleLoggerConfigurationExtensions
{
static object DefaultSyncRoot = new object();
static readonly object DefaultSyncRoot = new object();
const string DefaultConsoleOutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}";

/// <summary>
Expand Down Expand Up @@ -61,8 +61,8 @@ public static LoggerConfiguration Console(
bool applyThemeToRedirectedOutput = false,
object syncRoot = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
if (sinkConfiguration is null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (outputTemplate is null) throw new ArgumentNullException(nameof(outputTemplate));

var appliedTheme = !applyThemeToRedirectedOutput && (System.Console.IsOutputRedirected || System.Console.IsErrorRedirected) ?
ConsoleTheme.None :
Expand Down Expand Up @@ -97,8 +97,8 @@ public static LoggerConfiguration Console(
LogEventLevel? standardErrorFromLevel = null,
object syncRoot = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (formatter == null) throw new ArgumentNullException(nameof(formatter));
if (sinkConfiguration is null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (formatter is null) throw new ArgumentNullException(nameof(formatter));

syncRoot = syncRoot ?? DefaultSyncRoot;
return sinkConfiguration.Sink(new ConsoleSink(ConsoleTheme.None, formatter, standardErrorFromLevel, syncRoot), restrictedToMinimumLevel, levelSwitch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void Emit(LogEvent logEvent)

TextWriter SelectOutputStream(LogEventLevel logEventLevel)
{
if (_standardErrorFromLevel == null)
if (_standardErrorFromLevel is null)
return Console.Out;

return logEventLevel < _standardErrorFromLevel ? Console.Out : Console.Error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public override ThemedValueFormatter SwitchTheme(ConsoleTheme theme)

protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarValue scalar)
{
if (scalar == null)
if (scalar is null)
throw new ArgumentNullException(nameof(scalar));
return FormatLiteralValue(scalar, state.Output, state.Format);
}

protected override int VisitSequenceValue(ThemedValueFormatterState state, SequenceValue sequence)
{
if (sequence == null)
if (sequence is null)
throw new ArgumentNullException(nameof(sequence));

var count = 0;
Expand Down Expand Up @@ -155,7 +155,7 @@ public int FormatLiteralValue(ScalarValue scalar, TextWriter output, string form
var value = scalar.Value;
var count = 0;

if (value == null)
if (value is null)
{
using (ApplyStyle(output, ConsoleThemeStyle.Null, ref count))
output.Write("null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override ThemedValueFormatter SwitchTheme(ConsoleTheme theme)

protected override int VisitScalarValue(ThemedValueFormatterState state, ScalarValue scalar)
{
if (scalar == null)
if (scalar is null)
throw new ArgumentNullException(nameof(scalar));

// At the top level, for scalar values, use "display" rendering.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override void Render(LogEvent logEvent, TextWriter output)
{
// Padding is never applied by this renderer.

if (logEvent.Exception == null)
if (logEvent.Exception is null)
return;

var lines = new StringReader(logEvent.Exception.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static class LevelOutputFormat

public static string GetLevelMoniker(LogEventLevel value, string format = null)
{
if (format == null || format.Length != 2 && format.Length != 3)
if (format is null || format.Length != 2 && format.Length != 3)
return Casing.Format(value.ToString(), format);

// Using int.Parse() here requires allocating a string to exclude the first character prefix.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public MessageTemplateOutputTokenRenderer(ConsoleTheme theme, PropertyToken toke

public override void Render(LogEvent logEvent, TextWriter output)
{
if (_token.Alignment == null || !_theme.CanBuffer)
if (_token.Alignment is null || !_theme.CanBuffer)
{
_renderer.Render(logEvent.MessageTemplate, logEvent.Properties, output);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class OutputTemplateRenderer : ITextFormatter

public OutputTemplateRenderer(ConsoleTheme theme, string outputTemplate, IFormatProvider formatProvider)
{
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
if (outputTemplate is null) throw new ArgumentNullException(nameof(outputTemplate));
var template = new MessageTemplateParser().Parse(outputTemplate);

var renderers = new List<OutputTemplateTokenRenderer>();
Expand Down Expand Up @@ -77,8 +77,8 @@ public OutputTemplateRenderer(ConsoleTheme theme, string outputTemplate, IFormat

public void Format(LogEvent logEvent, TextWriter output)
{
if (logEvent == null) throw new ArgumentNullException(nameof(logEvent));
if (output == null) throw new ArgumentNullException(nameof(output));
if (logEvent is null) throw new ArgumentNullException(nameof(logEvent));
if (output is null) throw new ArgumentNullException(nameof(output));

foreach (var renderer in _renderers)
renderer.Render(logEvent, output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void Render(LogEvent logEvent, TextWriter output)

var value = new StructureValue(included);

if (_token.Alignment == null || !_theme.CanBuffer)
if (_token.Alignment is null || !_theme.CanBuffer)
{
_valueFormatter.Format(value, output, null);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override void Render(LogEvent logEvent, TextWriter output)
var _ = 0;
using (_theme.Apply(output, ConsoleThemeStyle.SecondaryText, ref _))
{
if (_token.Alignment == null)
if (_token.Alignment is null)
{
sv.Render(output, _token.Format, _formatProvider);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static class Padding
/// <param name="alignment">The alignment settings to apply when rendering <paramref name="value"/>.</param>
public static void Apply(TextWriter output, string value, Alignment? alignment)
{
if (alignment == null || value.Length >= alignment.Value.Width)
if (alignment is null || value.Length >= alignment.Value.Width)
{
output.Write(value);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class AnsiConsoleTheme : ConsoleTheme
/// <param name="styles">Styles to apply within the theme.</param>
public AnsiConsoleTheme(IReadOnlyDictionary<ConsoleThemeStyle, string> styles)
{
if (styles == null) throw new ArgumentNullException(nameof(styles));
if (styles is null) throw new ArgumentNullException(nameof(styles));
_styles = styles.ToDictionary(kv => kv.Key, kv => kv.Value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class SystemConsoleTheme : ConsoleTheme
/// <param name="styles">Styles to apply within the theme.</param>
public SystemConsoleTheme(IReadOnlyDictionary<ConsoleThemeStyle, SystemConsoleThemeStyle> styles)
{
if (styles == null) throw new ArgumentNullException(nameof(styles));
if (styles is null) throw new ArgumentNullException(nameof(styles));
Styles = styles.ToDictionary(kv => kv.Key, kv => kv.Value);
}

Expand Down