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

Remove fast-path closures from EventBasedLRUCache #2170

Merged
merged 1 commit into from
Jul 27, 2023

Conversation

stephentoub
Copy link
Collaborator

No description provided.

@kimbell
Copy link

kimbell commented Jul 26, 2023

How does this avoid a closure?
var localCacheItem = cacheItem; // avoid closure on fast path or when !_maintainLRU

The localCacheItem is still outside the delegate, I thought that would have created a closure.

@stephentoub
Copy link
Collaborator Author

stephentoub commented Jul 26, 2023

How does this avoid a closure? var localCacheItem = cacheItem; // avoid closure on fast path or when !_maintainLRU

The localCacheItem is still outside the delegate, I thought that would have created a closure.

It doesn't avoid the closure all-up, but it avoids it on the fast path. If you have the code:

if (d.TryGetValue(key, out var something))
{
    return something;
}

if (whatever)
{
    Capture(() => something);
}

that will result in the C# compiler generating code along the lines of:

<>__DisplayClass_42 display = new();
if (d.TryGetValue(key, out display.something))
{
    return display.something;
}

if (whatever)
{
    Capture(new Action(display.SomeMethod));
}

but if you instead do:

if (d.TryGetValue(key, out var something))
{
    return something;
}

if (whatever)
{
    var localSomething = something;
    Capture(() => localSomething);
}

the closure moves to the scope in which localSomething is declared:

if (d.TryGetValue(key, out var something))
{
    return something;
}

if (whatever)
{
    <>__DisplayClass_42 display = new();
    display.localSomething = something;
    Capture(new Action(display.SomeMethod));
}

@kimbell
Copy link

kimbell commented Jul 26, 2023

@stephentoub Thanks, that was very interesting. Learn something new every day.
Something else I'll try to remember when reviewing my code.

Copy link
Contributor

@keegan-caruso keegan-caruso left a comment

Choose a reason for hiding this comment

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

failing tests

@stephentoub
Copy link
Collaborator Author

failing tests

Thanks, I'll take a look. Which tests? I guess CI isn't running any?

@stephentoub stephentoub force-pushed the eventbasedcacheclosures branch from 92fbb8a to 7b05330 Compare July 27, 2023 02:15
@stephentoub
Copy link
Collaborator Author

Fixed. I'd accidentally written cacheItem in one place when it should have been newCacheItem.

@jennyf19 jennyf19 added this to the 7.0.0-preview2 milestone Jul 27, 2023
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