-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Useless variable closure #14869
Useless variable closure #14869
Conversation
@@ -79,7 +79,7 @@ public async Task Invoke(HttpContext context) | |||
|
|||
// When multiple requests occur for the same file we use a Lazy<Task> | |||
// to initialize the file store request once. | |||
await _workers.GetOrAdd(subPathValue, x => new Lazy<Task>(async () => | |||
await _workers.GetOrAdd(subPathValue, subPathValue => new Lazy<Task>(async () => |
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 this fixes the issue?!!
@@ -79,7 +79,7 @@ public async Task Invoke(HttpContext context) | |||
|
|||
// When multiple requests occur for the same file we use a Lazy<Task> | |||
// to initialize the file store request once. | |||
await _workers.GetOrAdd(subPathValue, x => new Lazy<Task>(async () => | |||
await _workers.GetOrAdd(subPathValue, subPathValue => new Lazy<Task>(async () => |
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.
Wouldn't putting the delegate's body into an actual method be safer?
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.
Yes and no, still the same problem if used like this
await _workers.GetOrAdd(
subPathValue,
x => new Lazy<Task>(SomeMethodAsync(subPathValue))).Value;
In place of being used like this
await _workers.GetOrAdd(
subPathValue,
x => new Lazy<Task>(SomeMethodAsync(x))).Value;
Or like that
await _workers.GetOrAdd(
subPathValue,
subPathValue => new Lazy<Task>(SomeMethodAsync(subPathValue))).Value;
Would need more time to simulate it because we need concurrent requests but with different media paths, so not so obvious without using a testing app to simulate these requests.
In the meantime, maybe worth to first try the simple suggested change before trying another fix if needed, like using a simple async semaphore.
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.
Thanks, I'll try, though I couldn't reproduce the issue locally.
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 will try to simulate something if I have time.
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.
@jtkech we can update this code (and anywhere we do this gating pattern) with this version which is better: https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/OutputCaching/src/DispatcherExtensions.cs#L8
Usage example: https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/OutputCaching/src/OutputCacheMiddleware.cs#L346
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.
@sebastienros Thanks for the info, anyway it looks like that #14859 is due to something else.
So this PR doesn't fix anything but maybe I will merge it because at least it prevents an useless variable closure.
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 due to something else
I know, just mentioning it there. Also I'd rather see the lambda use the x
internally instead of renaming the argument, because now it's ambiguous which one is used if you don't know the precedence rule.
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.
Okay no problem I will update it
I simulated something with the same pattern and many requested paths, sometimes the same paths sometimes different paths, looks like Maybe still worth to try this PR to confirm but I don't think it fixes anything. That said not fully sure and still better to use the passed parameter. |
src/OrchardCore.Modules/OrchardCore.Media/Services/MediaFileStoreResolverMiddleware.cs
Show resolved
Hide resolved
I'm trying it out now. And while I'm at it, since I'm adding just the changed middleware as an override to an app, I'll also add some custom tracing to it to figure out what actually takes the time. |
Okay cool, let me know |
By using the variable passed to the worker delegate.