Skip to content

Commit

Permalink
Avoid some LINQ allocations (#2819)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Apr 23, 2024
1 parent 00d4483 commit f0c94fd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public static bool IsDictionaryValueNonNullable(this MemberInfo memberInfo)

private static object GetNullableAttribute(this MemberInfo memberInfo)
{
var nullableAttribute = memberInfo.GetCustomAttributes()
.Where(attr => string.Equals(attr.GetType().FullName, NullableAttributeFullTypeName))
.FirstOrDefault();
var nullableAttribute = memberInfo
.GetCustomAttributes()
.FirstOrDefault(attr => string.Equals(attr.GetType().FullName, NullableAttributeFullTypeName));

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

var nullableContext = attributes
.Where(attr => string.Equals(attr.GetType().FullName, NullableContextAttributeFullTypeName))
.FirstOrDefault();
.FirstOrDefault(attr => string.Equals(attr.GetType().FullName, NullableContextAttributeFullTypeName));

if (nullableContext != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ public OpenApiDocument GetSwagger(string documentName, string host = null, strin

var applicableApiDescriptions = _apiDescriptionsProvider.ApiDescriptionGroups.Items
.SelectMany(group => group.Items)
.Where(apiDesc => !(_options.IgnoreObsoleteActions && apiDesc.CustomAttributes().OfType<ObsoleteAttribute>().Any()))
.Where(apiDesc => !apiDesc.CustomAttributes().OfType<SwaggerIgnoreAttribute>().Any())
.Where(apiDesc => _options.DocInclusionPredicate(documentName, apiDesc));
.Where(apiDesc =>
{
var attributes = apiDesc.CustomAttributes().ToList();
return !(_options.IgnoreObsoleteActions && attributes.OfType<ObsoleteAttribute>().Any()) &&
!attributes.OfType<SwaggerIgnoreAttribute>().Any() &&
_options.DocInclusionPredicate(documentName, apiDesc);
});

var schemaRepository = new SchemaRepository(documentName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public static ApiDescription Create(
// If the provided action has a matching parameter - use it to assign ParameterDescriptor & ModelMetadata
parameter.ParameterDescriptor = actionDescriptor.Parameters
.OfType<ParameterDescriptor>()
.Where(parameterDescriptor => parameterDescriptor.Name == parameter.Name)
.FirstOrDefault();
.FirstOrDefault(parameterDescriptor => parameterDescriptor.Name == parameter.Name);

var parameterDescriptorWithParameterInfo = parameter.ParameterDescriptor as
#if NETCOREAPP2_2_OR_GREATER
Expand Down

0 comments on commit f0c94fd

Please sign in to comment.