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

Removed OfType usage. Less allocations and much faster #29962

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ public static IMvcCoreBuilder AddMvcCore(this IServiceCollection services)

private static void ConfigureDefaultFeatureProviders(ApplicationPartManager manager)
{
if (!manager.FeatureProviders.OfType<ControllerFeatureProvider>().Any())
var controllerFeaturePresent = false;

foreach(var feature in manager.FeatureProviders)
{
if(feature is ControllerFeatureProvider)
{
controllerFeaturePresent = true;
break;
}
}

if(!controllerFeaturePresent)
Comment on lines +68 to +79
Copy link
Member

Choose a reason for hiding this comment

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

You should be able to get exactly the same performance from this:

Suggested change
var controllerFeaturePresent = false;
foreach(var feature in manager.FeatureProviders)
{
if(feature is ControllerFeatureProvider)
{
controllerFeaturePresent = true;
break;
}
}
if(!controllerFeaturePresent)
if(!manager.FeatureProviders.Any(provider => provider is ControllerFeatureProvider))

Copy link
Member

@stephentoub stephentoub Feb 10, 2021

Choose a reason for hiding this comment

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

exactly the same performance

It won't be "exactly" the same: every element would now involve a delegate invocation.

But I don't think that matters here. I agree with Sam we should keep this simple and that this is a perfectly reasonable place to use Enumerable.Any(func). I would change my tune if this were part of a default Blazor wasm app and was the last thing keeping Enumerable.Any alive in such an app and we could otherwise trim out Enumerable.Any, but it's not.

{
manager.FeatureProviders.Add(new ControllerFeatureProvider());
}
Expand Down