dotnet / roslyn-analyzers

MIT License
1.6k stars 468 forks source link

CA1041 performance #6143

Open Youssef1313 opened 2 years ago

Youssef1313 commented 2 years ago

Seeing the following in a binlog:

image

Looks like this is a very simple analyzer, but is somewhat slow. The analyzer registers symbol actions for named types, methods, fields, properties, and events.

So I assume this is alone too much work.

The analyzer should use IAttributeOperation when it's added back (but the analyzer will not work for people consuming a compiler without this IOp), or register a syntax action for AttributeSyntax.

Yes we try to avoid syntax analysis in many cases, but I think for this one we might see a good performance gain.

Youssef1313 commented 2 years ago

cc @mavasani @sharwell

NewellClark commented 1 year ago

I would be willing to work on this.

mavasani commented 1 year ago

Looks like this is a very simple analyzer, but is somewhat slow. The analyzer registers symbol actions for named types, methods, fields, properties, and events. So I assume this is alone too much work.

Registering callbacks should not add so much of a performance cost to analyzer execution. @Youssef1313 are you able to share a performance trace or a repro project?

Youssef1313 commented 1 year ago

I have seen it in Uno's binlogs. I'm enabling the analyzer in https://github.com/unoplatform/uno/pull/12641 to see if it will still show up.

But I'm not sure I'll be able to get a meaningful trace. I could record a trace while building Uno locally, but there are parts of the build that are much more costly than CA1041 so I don't think anything related to CA1041 will show up in the trace (unless there is a way to know the time interval of the trace during which the analyzer is run maybe?)

Youssef1313 commented 1 year ago

A quick note:

https://github.com/dotnet/roslyn/blob/c31875bdfde1e64b1879212e4b8f974973704b08/src/Compilers/CSharp/Portable/Symbols/Symbol_Attributes.cs#L300

This line will allocate a BindingDiagnosticBag. Since the analyzer calls GetAttributes on every symbol in the compilation, and that runs concurrently, that might be too much allocations?

mavasani commented 1 year ago

A quick note:

https://github.com/dotnet/roslyn/blob/c31875bdfde1e64b1879212e4b8f974973704b08/src/Compilers/CSharp/Portable/Symbols/Symbol_Attributes.cs#L300

This line will allocate a BindingDiagnosticBag. Since the analyzer calls GetAttributes on every symbol in the compilation, and that runs concurrently, that might be too much allocations?

@Youssef1313 Even if the analyzer execution does not lead to invoking that method, the compiler will do so to finish binding. I don't believe that should justify moving away from the analyzer causing force binding of attributes. We would just be moving the performance cost from the analyzer to the rest of the compilation process. Unless we can definitely reduce the overall build time cost, and show that analyzer is causing some unnecessary compute overhead, we may have to just live with this.

Youssef1313 commented 1 year ago

@mavasani CPU Stacks view for the analyzer

image

Are you able to see the trace in https://developercommunity.visualstudio.com/t/Performance-trace-for-CA1041/10395065 ?

Youssef1313 commented 1 year ago

I created a "fake" codebase using:

for (int i = 0; i < 10000; i++)
{
    var x = $$"""
        class C{{i}}
        {
            void M1 () { }
            void M2 () { }
            void M3 () { }
            void M4 () { }
            void M5 () { }

            string P1 { get; set; }
            string P2 { get; set; }
            string P3 { get; set; }
            string P4 { get; set; }
            string P5 { get; set; }

            event EventHandler E1;
            event EventHandler E2;
            event EventHandler E3;
            event EventHandler E4;
            event EventHandler E5;
        }
        """;
    File.WriteAllText($"C{i}.cs", x);
}

The following would be needed in csproj to avoid too much build warnings:

<Nullable>disable</Nullable>
<NoWarn>$(NoWarn);CS0067</NoWarn>

Then I started PerfView with:

PerfView.exe collect C:\temp\ReproTrace.etl -CircularMB:2048 -Merge:true -Zip:true -Providers:641d7f6c-481c-42e8-ab7e-d18dc5e5cb9e,*Microsoft-VisualStudio-Common -ThreadTime -NoV2Rundown -BufferSizeMB:1024

Then performed a build in VS.

Youssef1313 commented 1 year ago

For the above codebase, CA1041 is the most expensive:

image

Analyzer Summary
    3.209 s   Microsoft.CodeAnalysis.NetAnalyzers, Version=7.0.8.6101, Culture=neutral, PublicKeyToken=31bf3856ad364e35
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.ProvideObsoleteAttributeMessageAnalyzer (CA1041) = 1.056 s
        Microsoft.NetCore.Analyzers.InteropServices.PlatformCompatibilityAnalyzer (CA1416, CA1422) = 609 ms
        Microsoft.NetCore.Analyzers.InteropServices.UseValidPlatformString (CA1418) = 486 ms
        Microsoft.NetCore.Analyzers.Runtime.UseThreadStaticCorrectly (CA2019, CA2259) = 418 ms
        Microsoft.NetCore.Analyzers.InteropServices.DisableRuntimeMarshallingAnalyzer (CA1420, CA1421) = 372 ms
        Microsoft.NetCore.Analyzers.InteropServices.DoNotUseOutAttributeStringPInvokeParametersAnalyzer (CA1417) = 68 ms
        Microsoft.NetCore.Analyzers.Runtime.DoNotDefineFinalizersForTypesDerivedFromMemoryManager (CA2015) = 51 ms
        Microsoft.NetCore.Analyzers.InteropServices.DynamicInterfaceCastableImplementationAnalyzer (CA2256, CA2257, CA2258) = 39 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.RemoveEmptyFinalizersAnalyzer (CA1821) = 29 ms
        Microsoft.NetCore.Analyzers.Runtime.BufferBlockCopyLengthAnalyzer (CA2018) = 15 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.EnumsShouldHaveZeroValueAnalyzer (CA1008) = 6 ms
        Microsoft.NetCore.Analyzers.Runtime.DoNotUseReferenceEqualsWithValueTypesAnalyzer (CA2013) = 5 ms
        Microsoft.NetCore.Analyzers.Performance.UseAsSpanInsteadOfRangeIndexerAnalyzer (CA1831, CA1832, CA1833) = 4 ms
        Microsoft.NetCore.Analyzers.Tasks.DoNotCreateTaskCompletionSourceWithWrongArguments (CA2247) = 3 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.RethrowToPreserveStackDetailsAnalyzer (CA2200) = 2 ms
        Microsoft.NetCore.Analyzers.Runtime.LoggerMessageDefineAnalyzer (CA1727, CA1848, CA2017, CA2253, CA2254) = 2 ms
        Microsoft.NetCore.Analyzers.Runtime.ModuleInitializerAttributeShouldNotBeUsedInLibraries (CA2255) = 2 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.AbstractTypesShouldNotHaveConstructorsAnalyzer (CA1012) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.AvoidEmptyInterfacesAnalyzer (CA1040) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.AvoidExcessiveParametersOnGenericTypes (CA1005) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.AvoidOutParameters (CA1021) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.CancellationTokenParametersMustComeLastAnalyzer (CA1068) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.CollectionPropertiesShouldBeReadOnlyAnalyzer (CA2227) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.CollectionsShouldImplementGenericInterfaceAnalyzer (CA1010) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DeclareTypesInNamespacesAnalyzer (CA1050) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DefineAccessorsForAttributeArgumentsAnalyzer (CA1019) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotCatchGeneralExceptionTypesAnalyzer (CA1031) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotDeclareStaticMembersOnGenericTypesAnalyzer (CA1000) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotDeclareVisibleInstanceFieldsAnalyzer (CA1051) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotDirectlyAwaitATaskAnalyzer (CA2007) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotExposeGenericLists (CA1002) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotHideBaseClassMethodsAnalyzer (CA1061) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotNameEnumValuesReserved (CA1700) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotOverloadOperatorEqualsOnReferenceTypes (CA1046) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotPassTypesByReference (CA1045) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotPrefixEnumValuesWithTypeNameAnalyzer (CA1712) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotRaiseExceptionsInUnexpectedLocationsAnalyzer (CA1065) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.EnumShouldNotHaveDuplicatedValues (CA1069) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.EnumStorageShouldBeInt32Analyzer (CA1028) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.EnumWithFlagsAttributeAnalyzer (CA1027, CA2217) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.EquatableAnalyzer (CA1066, CA1067) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.EventsShouldNotHaveBeforeOrAfterPrefix (CA1713) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.ExceptionsShouldBePublicAnalyzer (CA1064) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.IdentifiersShouldDifferByMoreThanCaseAnalyzer (CA1708) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.IdentifiersShouldHaveCorrectPrefixAnalyzer (CA1715) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.IdentifiersShouldHaveCorrectSuffixAnalyzer (CA1710) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.IdentifiersShouldNotContainTypeNames (CA1720) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.IdentifiersShouldNotContainUnderscoresAnalyzer (CA1707) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.IdentifiersShouldNotHaveIncorrectSuffixAnalyzer (CA1711) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.IdentifiersShouldNotMatchKeywordsAnalyzer (CA1716) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.ImplementIDisposableCorrectlyAnalyzer (CA1063) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.InterfaceMethodsShouldBeCallableByChildTypesAnalyzer (CA1033) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.MarkAssembliesWithAttributesDiagnosticAnalyzer (CA1014, CA1016) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.MarkAssembliesWithComVisibleAnalyzer (CA1017) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.MarkAttributesWithAttributeUsageAnalyzer (CA1018) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.MovePInvokesToNativeMethodsClassAnalyzer (CA1060) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.NestedTypesShouldNotBeVisibleAnalyzer (CA1034) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.NonConstantFieldsShouldNotBeVisibleAnalyzer (CA2211) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.OperatorOverloadsHaveNamedAlternatesAnalyzer (CA2225) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.OperatorsShouldHaveSymmetricalOverloadsAnalyzer (CA2226) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.OverloadOperatorEqualsOnOverridingValueTypeEqualsAnalyzer (CA2231) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.OverrideEqualsAndOperatorEqualsOnValueTypesAnalyzer (CA1815) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.OverrideMethodsOnComparableTypesAnalyzer (CA1036) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.ParameterNamesShouldMatchBaseDeclarationAnalyzer (CA1725) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.PropertiesShouldNotBeWriteOnlyAnalyzer (CA1044) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.PropertiesShouldNotReturnArraysAnalyzer (CA1819) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.PropertyNamesShouldNotMatchGetMethodsAnalyzer (CA1721) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.StaticHolderTypesAnalyzer (CA1052) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.TypeNamesShouldNotMatchNamespacesAnalyzer (CA1724) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UriParametersShouldNotBeStringsAnalyzer (CA1054) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UriPropertiesShouldNotBeStringsAnalyzer (CA1056) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UriReturnValuesShouldNotBeStringsAnalyzer (CA1055) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UseEventsWhereAppropriateAnalyzer (CA1030) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UseGenericEventHandlerInstancesAnalyzer (CA1003) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UseIntegralOrStringArgumentForIndexersAnalyzer (CA1043) = 0 ms
        Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UsePropertiesWhereAppropriateAnalyzer (CA1024) = 0 ms
        Microsoft.CodeQuality.Analyzers.Maintainability.AvoidDeadConditionalCode (CA1508) = 0 ms
        Microsoft.CodeQuality.Analyzers.Maintainability.AvoidUnusedPrivateFieldsAnalyzer (CA1823) = 0 ms
        Microsoft.CodeQuality.Analyzers.Maintainability.CodeMetrics.CodeMetricsAnalyzer (CA1501, CA1502, CA1505, CA1506, CA1509) = 0 ms
        Microsoft.CodeQuality.Analyzers.Maintainability.DoNotIgnoreMethodResultsAnalyzer (CA1806) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.AssigningSymbolAndItsMemberInSameStatement (CA2246) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.AvoidDuplicateElementInitialization (CA2244) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.AvoidInfiniteRecursion (CA2011) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.AvoidPropertySelfAssignment (CA2245) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.DoNotCallOverridableMethodsInConstructorsAnalyzer (CA2214) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.DoNotDeclareEventFieldsAsVirtual (CA1070) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.DoNotRaiseExceptionsInExceptionClausesAnalyzer (CA2219) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.MarkMembersAsStaticAnalyzer (CA1822) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.PreferJaggedArraysOverMultidimensionalAnalyzer (CA1814) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.ReviewVisibleEventHandlersAnalyzer (CA2109) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.SealMethodsThatSatisfyPrivateInterfacesAnalyzer (CA2119) = 0 ms
        Microsoft.CodeQuality.Analyzers.QualityGuidelines.ValidateArgumentsOfPublicMethods (CA1062) = 0 ms
        Microsoft.NetCore.Analyzers.Data.ReviewSqlQueriesForSecurityVulnerabilities (CA2100) = 0 ms
        Microsoft.NetCore.Analyzers.ImmutableCollections.DoNotCallToImmutableCollectionOnAnImmutableCollectionValueAnalyzer (CA2009) = 0 ms
        Microsoft.NetCore.Analyzers.InteropServices.AvoidStringBuilderPInvokeParametersAnalyzer (CA1838) = 0 ms
        Microsoft.NetCore.Analyzers.InteropServices.PInvokeDiagnosticAnalyzer (CA1401, CA2101) = 0 ms
        Microsoft.NetCore.Analyzers.InteropServices.ProvidePublicParameterlessSafeHandleConstructorAnalyzer (CA1419) = 0 ms
        Microsoft.NetCore.Analyzers.Performance.DoNotGuardDictionaryRemoveByContainsKey (CA1853) = 0 ms
        Microsoft.NetCore.Analyzers.Performance.PreferHashDataOverComputeHashAnalyzer (CA1850) = 0 ms
        Microsoft.NetCore.Analyzers.Performance.UseCountProperlyAnalyzer (CA1827, CA1828, CA1829, CA1836) = 0 ms
        Microsoft.NetCore.Analyzers.Performance.UseStringContainsCharOverloadWithSingleCharactersAnalyzer (CA1847) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.AttributeStringLiteralsShouldParseCorrectlyAnalyzer (CA2243) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.AvoidUnsealedAttributesAnalyzer (CA1813) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.CallGCSuppressFinalizeCorrectlyAnalyzer (CA1816) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.DisposableFieldsShouldBeDisposed (CA2213) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.DisposableTypesShouldDeclareFinalizerAnalyzer (CA2216) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.DisposeMethodsShouldCallBaseClassDispose (CA2215) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.DisposeObjectsBeforeLosingScope (CA2000) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.DoNotLockOnObjectsWithWeakIdentityAnalyzer (CA2002) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.DoNotPassLiteralsAsLocalizedParameters (CA1303) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.DoNotRaiseReservedExceptionTypesAnalyzer (CA2201) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.DoNotUseEnumerableMethodsOnIndexableCollectionsInsteadUseTheCollectionDirectlyAnalyzer (CA1826) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.InitializeStaticFieldsInlineAnalyzer (CA1810, CA2207) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.InstantiateArgumentExceptionsCorrectlyAnalyzer (CA2208) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.NormalizeStringsToUppercaseAnalyzer (CA1308) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.PreferAsSpanOverSubstring (CA1846) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.PreferConstCharOverConstUnitStringAnalyzer (CA1834) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.PreferDictionaryTryGetValueAnalyzer (CA1854) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.PreferStreamAsyncMemoryOverloads (CA1835) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.PreferStringContainsOverIndexOfAnalyzer (CA2249) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.PreferTypedStringBuilderAppendOverloads (CA1830) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.ProvideCorrectArgumentsToFormattingMethodsAnalyzer (CA2241) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.ProvideStreamMemoryBasedAsyncOverrides (CA1844) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.SealInternalTypes (CA1852) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.SerializationRulesDiagnosticAnalyzer (CA2229, CA2235, CA2237) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.SpecifyCultureInfoAnalyzer (CA1304) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.SpecifyIFormatProviderAnalyzer (CA1305) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.SpecifyStringComparisonAnalyzer (CA1307, CA1310) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.TestForEmptyStringsUsingStringLengthAnalyzer (CA1820) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.TestForNaNCorrectlyAnalyzer (CA2242) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.UseAsyncMethodInAsyncContext (CA1849) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.UseCancellationTokenThrowIfCancellationRequested (CA2250) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.UseEnvironmentMembers (CA1837, CA1839, CA1840) = 0 ms
        Microsoft.NetCore.Analyzers.Runtime.UseStringEqualsOverStringCompare (CA2251) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ApprovedCipherModeAnalyzer (CA5358) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotAddArchiveItemPathToTheTargetFileSystemPath (CA5389) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotAddSchemaByURL (CA3061) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotAlwaysSkipTokenValidationInDelegates (CA5405) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotCallDangerousMethodsInDeserialization (CA5360) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotDisableCertificateValidation (CA5359) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotDisableHttpClientCRLCheck (CA5399, CA5400) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotDisableHTTPHeaderChecking (CA5365) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotDisableRequestValidation (CA5363) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotDisableTokenValidationChecks (CA5404) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotHardCodeCertificate (CA5403) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotHardCodeEncryptionKey (CA5390) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotInstallRootCert (CA5380, CA5381) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotSerializeTypeWithPointerFields (CA5367) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotSetSwitch (CA5361, CA5378) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseAccountSAS (CA5375) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseCreateEncryptorWithNonDefaultIV (CA5401, CA5402) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseDataSetReadXml (CA2351, CA2361) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseDataTableReadXml (CA2350) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseDeprecatedSecurityProtocols (CA5364, CA5386) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseDSA (CA5384) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureCryptographicAlgorithmsAnalyzer (CA5350, CA5351) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureDeserializerBinaryFormatterMethods (CA2300) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureDeserializerBinaryFormatterWithoutBinder (CA2301, CA2302) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureDeserializerJavaScriptSerializerWithSimpleTypeResolver (CA2321, CA2322) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureDeserializerJsonNetWithoutBinder (CA2329, CA2330) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureDeserializerLosFormatter (CA2305) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureDeserializerNetDataContractSerializerMethods (CA2310) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureDeserializerNetDataContractSerializerWithoutBinder (CA2311, CA2312) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureDeserializerObjectStateFormatter (CA2315) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureRandomness (CA5394) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseInsecureSettingsForJsonNet (CA2327, CA2328) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseObsoleteKDFAlgorithm (CA5373) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseWeakKDFAlgorithm (CA5379) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseWeakKDFInsufficientIterationCount (CA5387, CA5388) = 0 ms
        Microsoft.NetCore.Analyzers.Security.DoNotUseXslTransform (CA5374) = 0 ms
        Microsoft.NetCore.Analyzers.Security.JsonNetTypeNameHandling (CA2326) = 0 ms
        Microsoft.NetCore.Analyzers.Security.PotentialReferenceCycleInDeserializedObjectGraph (CA5362) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForCommandExecutionVulnerabilities (CA3006) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForDllInjectionVulnerabilities (CA3011) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForFilePathInjectionVulnerabilities (CA3003) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForInformationDisclosureVulnerabilities (CA3004) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForLdapInjectionVulnerabilities (CA3005) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForOpenRedirectVulnerabilities (CA3007) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForRegexInjectionVulnerabilities (CA3012) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForSqlInjectionVulnerabilities (CA3001) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForXamlInjectionVulnerabilities (CA3010) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForXmlInjectionVulnerabilities (CA3009) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForXPathInjectionVulnerabilities (CA3008) = 0 ms
        Microsoft.NetCore.Analyzers.Security.ReviewCodeForXssVulnerabilities (CA3002) = 0 ms
        Microsoft.NetCore.Analyzers.Security.SetHttpOnlyForHttpCookie (CA5396) = 0 ms
        Microsoft.NetCore.Analyzers.Security.SetViewStateUserKey (CA5368) = 0 ms
        Microsoft.NetCore.Analyzers.Security.SslProtocolsAnalyzer (CA5397, CA5398) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseAutoValidateAntiforgeryToken (CA5391, CA5395) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseContainerLevelAccessPolicy (CA5377) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseDefaultDllImportSearchPathsAttribute (CA5392, CA5393) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseRSAWithSufficientKeySize (CA5385) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseSecureCookiesASPNetCore (CA5382, CA5383) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseSharedAccessProtocolHttpsOnly (CA5376) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseXmlReaderForDataSetReadXml (CA5366) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseXmlReaderForDeserialize (CA5369) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseXmlReaderForSchemaRead (CA5371) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseXmlReaderForValidatingReader (CA5370) = 0 ms
        Microsoft.NetCore.Analyzers.Security.UseXmlReaderForXPathDocument (CA5372) = 0 ms
        Microsoft.NetCore.Analyzers.Tasks.DoNotCreateTasksWithoutPassingATaskSchedulerAnalyzer (CA2008) = 0 ms
        Microsoft.NetCore.Analyzers.Tasks.DoNotUseWhenAllOrWaitAllWithSingleArgument (CA1842, CA1843) = 0 ms
        Microsoft.NetCore.Analyzers.Tasks.UseValueTasksCorrectlyAnalyzer (CA2012) = 0 ms
        Microsoft.NetCore.Analyzers.Usage.ProvideCorrectArgumentToEnumHasFlag (CA2248) = 0 ms
        Microsoft.NetFramework.Analyzers.DoNotCatchCorruptedStateExceptionsAnalyzer (CA2153) = 0 ms
        Microsoft.NetFramework.Analyzers.DoNotUseInsecureDtdProcessingAnalyzer (CA3075) = 0 ms
        Microsoft.NetFramework.Analyzers.DoNotUseInsecureDtdProcessingInApiDesignAnalyzer (CA3077) = 0 ms
        Microsoft.NetFramework.Analyzers.DoNotUseInsecureXSLTScriptExecutionAnalyzer (CA3076) = 0 ms
        Microsoft.NetFramework.Analyzers.MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer (CA3147) = 0 ms
        Microsoft.NetFramework.Analyzers.TypesShouldNotExtendCertainBaseTypesAnalyzer (CA1058) = 0 ms
    2.620 s   Microsoft.CodeAnalysis.CSharp.NetAnalyzers, Version=7.0.8.6101, Culture=neutral, PublicKeyToken=31bf3856ad364e35
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpDetectPreviewFeatureAnalyzer (CA2252) = 2.580 s
        Microsoft.NetCore.CSharp.Analyzers.Usage.CSharpImplementGenericMathInterfacesCorrectly (CA2260) = 34 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpDoNotUseStackallocInLoopsAnalyzer (CA2014) = 2 ms
        Microsoft.CodeAnalysis.CSharp.NetAnalyzers.Microsoft.CodeQuality.Analyzers.QualityGuidelines.CSharpAvoidMultipleEnumerationsAnalyzer (CA1851) = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.ApiDesignGuidelines.CSharpImplementStandardExceptionConstructorsAnalyzer (CA1032) = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.ApiDesignGuidelines.CSharpPassSystemUriObjectsInsteadOfStringsAnalyzer (CA2234) = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.ApiDesignGuidelines.CSharpTypesThatOwnDisposableFieldsShouldBeDisposableAnalyzer (CA1001) = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.ApiDesignGuidelines.CSharpUsePreferredTermsAnalyzer () = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.ApiReview.CSharpAvoidCallingProblematicMethodsAnalyzer () = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.Documentation.CSharpAvoidUsingCrefTagsWithAPrefixAnalyzer (CA1200) = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.Maintainability.CSharpAvoidUninstantiatedInternalClasses (CA1812) = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.Maintainability.CSharpUseNameofInPlaceOfStringAnalyzer (CA1507) = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.Maintainability.CSharpVariableNamesShouldNotMatchFieldNamesAnalyzer () = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.QualityGuidelines.CSharpDoNotInitializeUnnecessarilyAnalyzer (CA1805) = 0 ms
        Microsoft.CodeQuality.CSharp.Analyzers.QualityGuidelines.CSharpUseLiteralsWhereAppropriate (CA1802) = 0 ms
        Microsoft.NetCore.Analyzers.Performance.CSharpUseSpanClearInsteadOfFillAnalyzer (CA1855) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.InteropServices.CSharpMarkBooleanPInvokeArgumentsWithMarshalAsAnalyzer () = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.InteropServices.CSharpUseManagedEquivalentsOfWin32ApiAnalyzer () = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Resources.CSharpMarkAssembliesWithNeutralResourcesLanguageAnalyzer (CA1824) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpAvoidZeroLengthArrayAllocationsAnalyzer (CA1825) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpDoNotUseTimersThatPreventPowerStateChangesAnalyzer () = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpForwardCancellationTokenToInvocationsAnalyzer (CA2016) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpImplementISerializableCorrectlyAnalyzer () = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpImplementSerializationMethodsCorrectlyAnalyzer () = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpPreferDictionaryContainsMethods (CA1841) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpPreventNumericIntPtrUIntPtrBehavioralChanges (CA2020) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpProvideDeserializationMethodsForOptionalFieldsAnalyzer () = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpSpecifyCultureForToLowerAndToUpperAnalyzer (CA1311) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpUseOrdinalStringComparisonAnalyzer (CA1309) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpUseSpanBasedStringConcat (CA1845) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Security.CSharpDataSetDataTableInIFormatterSerializableObjectGraphAnalyzer (CA2354) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Security.CSharpDataSetDataTableInSerializableObjectGraphAnalyzer (CA2355) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Security.CSharpDataSetDataTableInSerializableTypeAnalyzer (CA2352, CA2353, CA2362) = 0 ms
        Microsoft.NetCore.CSharp.Analyzers.Security.CSharpDataSetDataTableInWebSerializableObjectGraphAnalyzer (CA2356) = 0 ms
        Microsoft.NetFramework.CSharp.Analyzers.CSharpAvoidDuplicateAcceleratorsAnalyzer () = 0 ms
        Microsoft.NetFramework.CSharp.Analyzers.CSharpCallBaseClassMethodsOnISerializableTypesAnalyzer () = 0 ms
        Microsoft.NetFramework.CSharp.Analyzers.CSharpDoNotMarkServicedComponentsWithWebMethodAnalyzer () = 0 ms
        Microsoft.NetFramework.CSharp.Analyzers.CSharpMarkWindowsFormsEntryPointsWithStaThreadAnalyzer () = 0 ms
        Microsoft.NetFramework.CSharp.Analyzers.CSharpSetLocaleForDataTypesAnalyzer () = 0 ms
        Microsoft.NetFramework.CSharp.Analyzers.CSharpSpecifyMessageBoxOptionsAnalyzer () = 0 ms
    7 ms   Microsoft.Interop.LibraryImportGenerator, Version=7.0.8.17405, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
        Microsoft.Interop.Analyzers.CustomMarshallerAttributeAnalyzer (SYSLIB1055, SYSLIB1056, SYSLIB1057, SYSLIB1060, SYSLIB1061) = 5 ms
        Microsoft.Interop.Analyzers.ConvertToLibraryImportAnalyzer (SYSLIB1054) = 0 ms
        Microsoft.Interop.Analyzers.NativeMarshallingAttributeAnalyzer (SYSLIB1058) = 0 ms
        Microsoft.Interop.Analyzers.ShapeBreakingDiagnosticSuppressor () = 0 ms
    0 ms   System.Text.RegularExpressions.Generator, Version=7.0.8.17405, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
        System.Text.RegularExpressions.Generator.UpgradeToGeneratedRegexAnalyzer (SYSLIB1045) = 0 ms
mavasani commented 1 year ago

@Youssef1313 - are you able to see any noticeable build time difference with this analyzer enabled vs disabled?

Youssef1313 commented 1 year ago

Yes. Csc task goes up from ~9 seconds to ~14 seconds on my machine. For a very simple analyzer like this, I think this increase is quite a lot.

binlogs.zip

sharwell commented 1 year ago

For the performance trace attached to AB#1838338, this analyzer was only responsible for 700ms of thread time. Almost all of that time was spent in this check:

https://github.com/dotnet/roslyn-analyzers/blob/ebd3126894fbc52f9b926c596d8c6d3949d9de4c/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/ProvideObsoleteAttributeMessage.cs#L58-L62

sharwell commented 1 year ago

Moving a "has any attributes" check to before the visibility check will improve things, provided ISymbol.GetAttributes() is consistently cached by Roslyn.

Youssef1313 commented 1 year ago

I took a look at the Uno's binlog again from https://github.com/unoplatform/uno/pull/12641 and the binlog time is much less than what I originally reported. Actually all analyzers now take much less time. In original issue, the binlog shows 3 minutes. The newest binlog I have is only 2 seconds.

This is very much surprising, but this improvement could be related to the many performance improvements done in Roslyn recently (I'm guessing - but anyway, the situation appears to be much better now).

I'm keeping this open though to move the slow check after GetAttributes though. But the scenario already magically improved very very significantly!