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

few minor MemoryCache perf improvements #44797

Merged
merged 6 commits into from
Nov 18, 2020

Conversation

adamsitnik
Copy link
Member

I just took a quick look at the source code to see if we could get some gains in the Caching TechEmpower benchmark. This PR is a free lunch, there is definitely more low hanging fruits there and I hope that it becomes one of our .NET 6 goals.

The performance repo microbenchmarks (dotnet/performance#1598) show 5 to 15% perf improvement

Method Toolchain Mean Ratio
GetHit \after\CoreRun.exe 137.61 ns 0.86
GetHit \before\CoreRun.exe 159.17 ns 1.00
TryGetValueHit \after\CoreRun.exe 131.57 ns 0.84
TryGetValueHit \before\CoreRun.exe 157.07 ns 1.00
GetMiss \after\CoreRun.exe 98.56 ns 0.87
GetMiss \before\CoreRun.exe 113.47 ns 1.00
TryGetValueMiss \after\CoreRun.exe 97.62 ns 0.86
TryGetValueMiss \before\CoreRun.exe 113.05 ns 1.00
SetOverride \after\CoreRun.exe 362.80 ns 0.95
SetOverride \before\CoreRun.exe 381.01 ns 1.00

The TechEmpower benchmark shows +6k RPS (from 234,210 240,778) to which translates to +2.75% RPS

@ghost
Copy link

ghost commented Nov 17, 2020

Tagging subscribers to this area: @eerhardt, @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details
Description:

I just took a quick look at the source code to see if we could get some gains in the Caching TechEmpower benchmark. This PR is a free lunch, there is definitely more low hanging fruits there and I hope that it becomes one of our .NET 6 goals.

The performance repo microbenchmarks (dotnet/performance#1598) show 5 to 15% perf improvement

Method Toolchain Mean Ratio
GetHit \after\CoreRun.exe 137.61 ns 0.86
GetHit \before\CoreRun.exe 159.17 ns 1.00
TryGetValueHit \after\CoreRun.exe 131.57 ns 0.84
TryGetValueHit \before\CoreRun.exe 157.07 ns 1.00
GetMiss \after\CoreRun.exe 98.56 ns 0.87
GetMiss \before\CoreRun.exe 113.47 ns 1.00
TryGetValueMiss \after\CoreRun.exe 97.62 ns 0.86
TryGetValueMiss \before\CoreRun.exe 113.05 ns 1.00
SetOverride \after\CoreRun.exe 362.80 ns 0.95
SetOverride \before\CoreRun.exe 381.01 ns 1.00

The TechEmpower benchmark shows +6k RPS (from 234,210 240,778) to which translates to +2.75% RPS

Author: adamsitnik
Assignees: -
Labels:

area-Extensions-Caching

Milestone: -

@adamsitnik adamsitnik added this to the 6.0.0 milestone Nov 17, 2020
}

// Called by multiple actions to see how long it's been since we last checked for expired items.
// If sufficient time has elapsed then a scan is initiated on a background task.
private void StartScanForExpiredItems(DateTimeOffset? utcNow = null)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Copy link
Member

@stephentoub stephentoub Nov 17, 2020

Choose a reason for hiding this comment

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

This isn't inlined otherwise? Also, ScheduleTask looks pretty small; it's not inlined automatically (which would defeat your goal here)?

Copy link
Member Author

Choose a reason for hiding this comment

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

before my changes, the StartScanForExpiredItems was not getting inlined:

obraz

now StartScanForExpiredItemsIfNeeded does get inlined an ScheduleTask does not:

obraz

obraz

I was simply not sure if _options.ExpirationScanFrequency < utcNow - _lastExpirationScan won't get translated to too much IL and just to be sure I've applied the attribute

Copy link
Member

Choose a reason for hiding this comment

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

I was simply not sure if _options.ExpirationScanFrequency < utcNow - _lastExpirationScan won't get translated to too much IL and just to be sure I've applied the attribute

Ok, but the same argument goes for whether ScheduleTask may get inlined automatically even though you don't want it to.

Copy link
Member Author

Choose a reason for hiding this comment

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

but the same argument goes for whether ScheduleTask may get inlined automatically even though you don't want it to

It's quite big (from the IL perspective) and I would not expect it to get inlined. Do you want me to ensure it by using an attribute?

Co-authored-by: Stephen Toub <stoub@microsoft.com>
@adamsitnik adamsitnik merged commit 65efefd into dotnet:master Nov 18, 2020
@adamsitnik adamsitnik deleted the minorMemoryCachePerfImprovements branch November 18, 2020 12:30
@kunalspathak
Copy link
Member

Related improvement link: DrewScoggins/performance-2#3516

@adamsitnik adamsitnik added the tenet-performance Performance related issue label Dec 4, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants