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

Useless variable closure #14869

Merged
merged 5 commits into from
Dec 21, 2023
Merged

Useless variable closure #14869

merged 5 commits into from
Dec 21, 2023

Conversation

jtkech
Copy link
Member

@jtkech jtkech commented Dec 9, 2023

By using the variable passed to the worker delegate.

@@ -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 () =>
Copy link
Member

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 () =>
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

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
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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

@jtkech
Copy link
Member Author

jtkech commented Dec 13, 2023

@Piedone

I simulated something with the same pattern and many requested paths, sometimes the same paths sometimes different paths, looks like subPathValue is always well captured and never different than the x parameter passed to the delegate.

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.

@Piedone
Copy link
Member

Piedone commented Dec 13, 2023

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.

@jtkech
Copy link
Member Author

jtkech commented Dec 14, 2023

Okay cool, let me know

@sebastienros sebastienros merged commit 3269df6 into main Dec 21, 2023
3 checks passed
@sebastienros sebastienros deleted the jtkech/media-worker branch December 21, 2023 19:39
urbanit pushed a commit to urbanit/OrchardCore that referenced this pull request Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants