Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Cap FormattedLogValues cache #561

Merged
merged 1 commit into from
Mar 16, 2017
Merged

Cap FormattedLogValues cache #561

merged 1 commit into from
Mar 16, 2017

Conversation

BrennanConroy
Copy link
Member

@BrennanConroy BrennanConroy commented Feb 21, 2017

#516
@glennc @davidfowl

I tried a ConcurrentLru cache in the initial commit, but it looked like perf halved in that scenario.
Current thought is to just cap the cache to prevent memory leaks, and if perf is a concern then the LoggerMessage.DefineScope pattern can be used.

Will file an issue in docs to document that pattern: dotnet/AspNetCore.Docs#2810


public FormattedLogValues(string format, params object[] values)
{
if (values?.Length != 0 && format != null)
{
_formatter = _formatters.GetOrAdd(format, f => new LogValuesFormatter(f));
if (_formatters.Count >= MaxCachedFormatters)
Copy link
Contributor

@pakrym pakrym Feb 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConcurrentDictionary.Count acquires all locks which makes this method slow we either need to do our own counter or switch to locking around List<T>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also reorder the checks here to reduce the impact

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean currently the Count check would happen even after the cap is reached but if you were to get a successful hit you could avoid this check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the impact right now vs not having the check?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if we are over the limit we just create it? That seems bad, what's the cache eviction policy on this? Seems like frequently occurring formats should never be dropped.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BrennanConroy
Copy link
Member Author

🆙 📅

@davidfowl
Copy link
Member

No tests?

@BrennanConroy
Copy link
Member Author

Ping

{
_formatter = _formatters.GetOrAdd(format, f =>
{
Interlocked.Increment(ref _count);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think 100% correctness is required here, we are preferring performance over it.

@BrennanConroy BrennanConroy merged commit cc503aa into dev Mar 16, 2017
@BrennanConroy BrennanConroy deleted the brecon/leak branch March 16, 2017 15:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants