From 1e95768041d85c9bbb73f82bedffa3e7786ba0a7 Mon Sep 17 00:00:00 2001 From: Simon Nattress Date: Mon, 12 Oct 2020 12:05:47 -0700 Subject: [PATCH] [Crossgen2] Improve compilation throughput for types with many fields (#43195) `IsLayoutFixedInCurrentVersionBubble` is a fairly expensive algorithm and can be called very frequently for the same type during JIT compilation. Store the computed result so future lookups are fast. Fixes https://github.com/dotnet/runtime/issues/38259 --- .../Compiler/ReadyToRunCodegenCompilation.cs | 11 ++++++++++- src/tests/issues.targets | 3 --- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/coreclr/src/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilation.cs b/src/coreclr/src/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilation.cs index 628b356..8fceca0 100644 --- a/src/coreclr/src/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilation.cs +++ b/src/coreclr/src/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCodegenCompilation.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -236,6 +237,11 @@ namespace ILCompiler public ReadyToRunSymbolNodeFactory SymbolNodeFactory { get; } public ReadyToRunCompilationModuleGroupBase CompilationModuleGroup { get; } private readonly int? _customPESectionAlignment; + /// + /// Determining whether a type's layout is fixed is a little expensive and the question can be asked many times + /// for the same type during compilation so preserve the computed value. + /// + private ConcurrentDictionary _computedFixedLayoutTypes = new ConcurrentDictionary(); internal ReadyToRunCodegenCompilation( DependencyAnalyzerBase dependencyGraph, @@ -375,7 +381,7 @@ namespace ILCompiler } } - public bool IsLayoutFixedInCurrentVersionBubble(TypeDesc type) + private bool IsLayoutFixedInCurrentVersionBubbleInternal(TypeDesc type) { // Primitive types and enums have fixed layout if (type.IsPrimitive || type.IsEnum) @@ -423,6 +429,9 @@ namespace ILCompiler return true; } + public bool IsLayoutFixedInCurrentVersionBubble(TypeDesc type) => + _computedFixedLayoutTypes.GetOrAdd(type, (t) => IsLayoutFixedInCurrentVersionBubbleInternal(t)); + public bool IsInheritanceChainLayoutFixedInCurrentVersionBubble(TypeDesc type) { // This method is not expected to be called for value types diff --git a/src/tests/issues.targets b/src/tests/issues.targets index befba6a..92403ca 100644 --- a/src/tests/issues.targets +++ b/src/tests/issues.targets @@ -914,9 +914,6 @@ https://github.com/dotnet/runtime/issues/32732 - - https://github.com/dotnet/runtime/issues/38259 - https://github.com/dotnet/runtime/issues/615 -- 2.7.4