Skip to content

Commit f0c94fd

Browse files
authored
Avoid some LINQ allocations (#2819)
1 parent 00d4483 commit f0c94fd

3 files changed

Lines changed: 12 additions & 10 deletions

File tree

src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/MemberInfoExtensions.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public static bool IsDictionaryValueNonNullable(this MemberInfo memberInfo)
8585

8686
private static object GetNullableAttribute(this MemberInfo memberInfo)
8787
{
88-
var nullableAttribute = memberInfo.GetCustomAttributes()
89-
.Where(attr => string.Equals(attr.GetType().FullName, NullableAttributeFullTypeName))
90-
.FirstOrDefault();
88+
var nullableAttribute = memberInfo
89+
.GetCustomAttributes()
90+
.FirstOrDefault(attr => string.Equals(attr.GetType().FullName, NullableAttributeFullTypeName));
9191

9292
return nullableAttribute;
9393
}
@@ -103,8 +103,7 @@ private static bool GetNullableFallbackValue(this MemberInfo memberInfo)
103103
var attributes = (IEnumerable<object>)declaringType.GetCustomAttributes(false);
104104

105105
var nullableContext = attributes
106-
.Where(attr => string.Equals(attr.GetType().FullName, NullableContextAttributeFullTypeName))
107-
.FirstOrDefault();
106+
.FirstOrDefault(attr => string.Equals(attr.GetType().FullName, NullableContextAttributeFullTypeName));
108107

109108
if (nullableContext != null)
110109
{

src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ public OpenApiDocument GetSwagger(string documentName, string host = null, strin
8686

8787
var applicableApiDescriptions = _apiDescriptionsProvider.ApiDescriptionGroups.Items
8888
.SelectMany(group => group.Items)
89-
.Where(apiDesc => !(_options.IgnoreObsoleteActions && apiDesc.CustomAttributes().OfType<ObsoleteAttribute>().Any()))
90-
.Where(apiDesc => !apiDesc.CustomAttributes().OfType<SwaggerIgnoreAttribute>().Any())
91-
.Where(apiDesc => _options.DocInclusionPredicate(documentName, apiDesc));
89+
.Where(apiDesc =>
90+
{
91+
var attributes = apiDesc.CustomAttributes().ToList();
92+
return !(_options.IgnoreObsoleteActions && attributes.OfType<ObsoleteAttribute>().Any()) &&
93+
!attributes.OfType<SwaggerIgnoreAttribute>().Any() &&
94+
_options.DocInclusionPredicate(documentName, apiDesc);
95+
});
9296

9397
var schemaRepository = new SchemaRepository(documentName);
9498

test/Swashbuckle.AspNetCore.TestSupport/ApiExplorer/ApiDescriptionFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public static ApiDescription Create(
3636
// If the provided action has a matching parameter - use it to assign ParameterDescriptor & ModelMetadata
3737
parameter.ParameterDescriptor = actionDescriptor.Parameters
3838
.OfType<ParameterDescriptor>()
39-
.Where(parameterDescriptor => parameterDescriptor.Name == parameter.Name)
40-
.FirstOrDefault();
39+
.FirstOrDefault(parameterDescriptor => parameterDescriptor.Name == parameter.Name);
4140

4241
var parameterDescriptorWithParameterInfo = parameter.ParameterDescriptor as
4342
#if NETCOREAPP2_2_OR_GREATER

0 commit comments

Comments
 (0)