Skip to content

Commit

Permalink
Merge pull request #1745 from snakefoot/FilePathLayoutCachePrev
Browse files Browse the repository at this point in the history
FilePathLayout - Reduce memory-allocation for cleanup of filename
  • Loading branch information
304NotModified committed Nov 7, 2016
2 parents 3840873 + acc8071 commit 64fb715
Showing 1 changed file with 50 additions and 16 deletions.
66 changes: 50 additions & 16 deletions src/NLog/Internal/FilePathLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ internal class FilePathLayout : IRenderable

private bool _cleanupInvalidChars;

/// <summary>
/// <see cref="_cachedPrevRawFileName"/> is the cache-key, and when newly rendered filename matches the cache-key,
/// then it reuses the cleaned cache-value <see cref="_cachedPrevCleanFileName"/>.
/// </summary>
private string _cachedPrevRawFileName;
/// <summary>
/// <see cref="_cachedPrevCleanFileName"/> is the cache-value that is reused, when the newly rendered filename
/// matches the cache-key <see cref="_cachedPrevRawFileName"/>
/// </summary>
private string _cachedPrevCleanFileName;

//TODO onInit maken
/// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
public FilePathLayout(Layout layout, bool cleanupInvalidChars, FilePathKind filePathKind)
Expand Down Expand Up @@ -138,56 +149,78 @@ public Layout GetLayout()
#region Implementation of IRenderable

/// <summary>
/// Render, as cleaned if requested.
/// Render the raw filename from Layout
/// </summary>
/// <param name="logEvent">The log event.</param>
/// <returns>String representation of a layout.</returns>
private string GetCleanedFileName(LogEventInfo logEvent)
private string GetRenderedFileName(LogEventInfo logEvent)
{
if (cleanedFixedResult != null)
{
return cleanedFixedResult;
}

if (_layout == null)
{
return null;
}

var result = _layout.Render(logEvent);
if (_cleanupInvalidChars)
{
return CleanupInvalidFilePath(result);
}
return result;
return _layout.Render(logEvent);
}

public string Render(LogEventInfo logEvent)
/// <summary>
/// Convert the raw filename to a correct filename
/// </summary>
/// <param name="rawFileName">The filename generated by Layout.</param>
/// <returns>String representation of a correct filename.</returns>
private string GetCleanFileName(string rawFileName)
{
var rendered = GetCleanedFileName(logEvent);
if (String.IsNullOrEmpty(rendered))
var cleanFileName = rawFileName;
if (_cleanupInvalidChars && cleanedFixedResult == null)
{
return rendered;
cleanFileName = CleanupInvalidFilePath(rawFileName);
}

if (_filePathKind == FilePathKind.Absolute)
{
return rendered;
return cleanFileName;
}

#if !SILVERLIGHT
if (_filePathKind == FilePathKind.Relative)
{
return Path.Combine(_baseDir, rendered);
//use basedir, faster than Path.GetFullPath
cleanFileName = Path.Combine(_baseDir, cleanFileName);
return cleanFileName;
}
#endif
//unknown, use slow method
return Path.GetFullPath(rendered);
cleanFileName = Path.GetFullPath(cleanFileName);
return cleanFileName;
}

public string Render(LogEventInfo logEvent)
{
var rawFileName = GetRenderedFileName(logEvent);
if (string.IsNullOrEmpty(rawFileName))
{
return rawFileName;
}

if ((!_cleanupInvalidChars || cleanedFixedResult != null) && _filePathKind == FilePathKind.Absolute)
return rawFileName; // Skip clean filename string-allocation

if (string.CompareOrdinal(_cachedPrevRawFileName, rawFileName) == 0 && _cachedPrevCleanFileName != null)
return _cachedPrevCleanFileName; // Cache Hit, reuse clean filename string-allocation

var cleanFileName = GetCleanFileName(rawFileName);
_cachedPrevCleanFileName = cleanFileName;
_cachedPrevRawFileName = rawFileName;
return cleanFileName;
}

#endregion


/// <summary>
/// Is this (templated/invalid) path an absolute, relative or unknown?
/// </summary>
Expand All @@ -201,6 +234,7 @@ internal static FilePathKind DetectFilePathKind(Layout pathLayout)

return DetectFilePathKind(simpleLayout);
}

/// <summary>
/// Is this (templated/invalid) path an absolute, relative or unknown?
/// </summary>
Expand Down

0 comments on commit 64fb715

Please sign in to comment.