-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
More MemoryCache perf improvements #45280
More MemoryCache perf improvements #45280
Conversation
…opagated anyway, +20k RPS for TechEmpower benchmark!
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue Details
+7.5% (+18k) RPS for TechEmpower benchmark!
|
src/libraries/Microsoft.Extensions.Caching.Memory/src/CacheEntry.cs
Outdated
Show resolved
Hide resolved
@@ -220,7 +221,8 @@ public void Dispose() | |||
} | |||
} | |||
|
|||
internal bool CheckExpired(DateTimeOffset now) | |||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |||
internal bool CheckExpired(in DateTimeOffset now) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How much does the ‘in’ help here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanted to be 100% sure that this struct is not being copied on Linux.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanted to be 100% sure that this struct is not being copied on Linux.
Did this one change make any measurable impact? We should not be sprinkling lots of in
s everywhere unless they're actually meaningful.
|
||
if (_slidingExpiration.HasValue | ||
&& (now - LastAccessed) >= _slidingExpiration) | ||
return SlowPath(now); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the idea here that the above check can get inlined, and then the “SlowPath” code remains a normal method call? Is there a good guidance on when this pattern should be used?
Side nit - is “SlowPath” a good name for this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the idea here that the above check can get inlined, and then the “SlowPath” code remains a normal method call?
Yes, exactly.
Is there a good guidance on when this pattern should be used?
I would say that it should be used when the condtion typically returns false and rarely executes the code inside it (like throwing exceptions) and is ofc on a hot path (small helper methods executed frequently)
…private helper methods
…ing cache entries
src/libraries/Microsoft.Extensions.Caching.Memory/src/CacheEntry.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Caching.Memory/src/CacheEntry.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM. Nice work here @adamsitnik! Looks like a good perf win.
cc @Tratcher
@@ -215,12 +216,17 @@ public void Dispose() | |||
if (_valueHasBeenSet) | |||
{ | |||
_notifyCacheEntryCommit(this); | |||
PropagateOptions(CacheEntryHelper.Current); | |||
|
|||
if (CanPropagateOptions()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always leave a comment about why you're doing what appears to be a redundant check.
@@ -235,27 +241,44 @@ internal void SetExpired(EvictionReason reason) | |||
DetachTokens(); | |||
} | |||
|
|||
private bool CheckForExpiredTime(DateTimeOffset now) | |||
private bool CheckForExpiredTime(in DateTimeOffset now) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same question.
don't acces expensive CacheEntryHelper.Current if options can't be propagated anyway
CacheEntryHelper.Current
uses expensiveAsyncLocal
. Before accessing it, we can just check if the options can be propagated and don't use it at all if not.+7.5% (+18k) RPS for TechEmpower benchmark!
inline the expiration check hot paths