Skip to content

Commit 475e763

Browse files
committed
Optimize System.Type name getting
- System.Type.Name instead of .GetShortName() extension in places where there it is obvious that short name is equal to Type.Name (type is not nested, it is class, and it is not generic), only for types we have created and control. - avoid getting type name when logging is skipped. RuntimeType has names caching mechanisms so we can rely on it instead of having ours.
1 parent 2eb8ee4 commit 475e763

19 files changed

Lines changed: 72 additions & 62 deletions

File tree

Orm/Xtensive.Orm/Comparison/ComparerProvider.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,36 @@ protected override TAssociate CreateAssociate<TKey, TAssociate>(out Type foundFo
6666
// the comparer.
6767
var comparer = base.CreateAssociate<TKey, IAdvancedComparerBase>(out foundFor);
6868
if (foundFor == null) {
69-
CoreLog.Warning(nameof(Strings.LogCantFindAssociateFor),
70-
TypeSuffixes.ToDelimitedString(" \\ "),
71-
typeof(TAssociate).GetShortName(),
72-
typeof(TKey).GetShortName());
69+
if (CoreLog.IsLogged(Orm.Logging.LogLevel.Warning)) {
70+
CoreLog.Warning(nameof(Strings.LogCantFindAssociateFor),
71+
TypeSuffixes.ToDelimitedString(" \\ "),
72+
typeof(TAssociate).GetShortName(),
73+
typeof(TKey).GetShortName());
74+
}
7375
return null;
7476
}
7577
if (foundFor == typeof(TKey)) {
7678
return (TAssociate) comparer;
7779
}
7880
associate = BaseComparerWrapperType.Activate(new[] { typeof(TKey), foundFor }, ConstructorParams) as TAssociate;
7981
if (associate != null) {
80-
CoreLog.Warning(nameof(Strings.LogGenericAssociateIsUsedFor),
82+
if (CoreLog.IsLogged(Orm.Logging.LogLevel.Warning)) {
83+
CoreLog.Warning(nameof(Strings.LogGenericAssociateIsUsedFor),
8184
BaseComparerWrapperType.GetShortName(),
82-
typeof (TKey).GetShortName(),
85+
typeof(TKey).GetShortName(),
8386
foundFor.GetShortName(),
84-
typeof (TKey).GetShortName());
87+
typeof(TKey).GetShortName());
88+
}
8589
return associate;
8690
}
8791
else {
88-
CoreLog.Warning(nameof(Strings.LogGenericAssociateCreationHasFailedFor),
92+
if (CoreLog.IsLogged(Orm.Logging.LogLevel.Warning)) {
93+
CoreLog.Warning(nameof(Strings.LogGenericAssociateCreationHasFailedFor),
8994
BaseComparerWrapperType.GetShortName(),
90-
typeof (TKey).GetShortName(),
95+
typeof(TKey).GetShortName(),
9196
foundFor.GetShortName(),
92-
typeof (TKey).GetShortName());
97+
typeof(TKey).GetShortName());
98+
}
9399
return null;
94100
}
95101
}

Orm/Xtensive.Orm/IoC/ServiceContainer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace Xtensive.IoC
2727
[Serializable]
2828
public class ServiceContainer : ServiceContainerBase
2929
{
30-
private static readonly Type typeofIServiceContainer = typeof(IServiceContainer);
30+
private static readonly Type iServiceContainerType = typeof(IServiceContainer);
3131

3232
private static readonly Func<ServiceRegistration, Pair<ConstructorInfo, ParameterInfo[]>> ConstructorFactory = serviceInfo => {
3333
var mappedType = serviceInfo.MappedType;
@@ -187,9 +187,9 @@ public static IServiceContainer Create(Type containerType, object configuration)
187187
public static IServiceContainer Create(Type containerType, object configuration, IServiceContainer parent)
188188
{
189189
ArgumentValidator.EnsureArgumentNotNull(containerType, "containerType");
190-
if (!typeofIServiceContainer.IsAssignableFrom(containerType))
190+
if (!iServiceContainerType.IsAssignableFrom(containerType))
191191
throw new ArgumentException(string.Format(
192-
Strings.ExContainerTypeMustImplementX, typeofIServiceContainer.GetShortName()), "containerType");
192+
Strings.ExContainerTypeMustImplementX, iServiceContainerType.Name), "containerType");
193193

194194
Type configurationType = configuration?.GetType(),
195195
parentType = parent?.GetType();

Orm/Xtensive.Orm/Modelling/Actions/NodeAction.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ public override string ToString()
105105
/// <returns>The name of the action.</returns>
106106
protected virtual string GetActionName()
107107
{
108-
string sn = GetType().GetShortName();
109-
return sn.TryCutSuffix("Action");
108+
return GetType().Name.TryCutSuffix("Action");
110109
}
111110

112111
/// <summary>

Orm/Xtensive.Orm/Modelling/Comparison/Difference.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ public abstract class Difference : IDifference
4343
/// <inheritdoc/>
4444
public override string ToString()
4545
{
46+
var diffType = GetType();
4647
return string.Format(Strings.DifferenceFormat,
47-
GetType().GetShortName(), Source, Target, ParametersToString());
48+
diffType.IsGenericType ? diffType.GetShortName() : diffType.Name,
49+
Source, Target, ParametersToString());
4850
}
4951

5052
/// <summary>

Orm/Xtensive.Orm/Modelling/Comparison/Upgrader.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,20 @@ public IReadOnlyList<NodeAction> GetUpgradeSequence(Difference difference, HintS
160160
.ForEach(validationHints.Add);
161161
var diff = comparer.Compare(CurrentModel, TargetModel, validationHints);
162162
if (diff != null) {
163-
using (CoreLog.InfoRegion(nameof(Strings.LogAutomaticUpgradeSequenceValidation))) {
164-
CoreLog.Info(nameof(Strings.LogValidationFailed));
165-
CoreLog.Info(nameof(Strings.LogItemFormat), Strings.Difference);
166-
CoreLog.Info(diff.ToString());
167-
CoreLog.Info(Strings.LogItemFormat + "\r\n{1}", Strings.UpgradeSequence,
168-
new ActionSequence() { actions });
169-
CoreLog.Info(nameof(Strings.LogItemFormat), Strings.ExpectedTargetModel);
170-
TargetModel.Dump();
171-
CoreLog.Info(nameof(Strings.LogItemFormat), Strings.ActualTargetModel);
172-
CurrentModel.Dump();
173-
throw new InvalidOperationException(Strings.ExUpgradeSequenceValidationFailure);
163+
if (CoreLog.IsLogged(Orm.Logging.LogLevel.Info)) {
164+
using (CoreLog.InfoRegion(nameof(Strings.LogAutomaticUpgradeSequenceValidation))) {
165+
CoreLog.Info(nameof(Strings.LogValidationFailed));
166+
CoreLog.Info(nameof(Strings.LogItemFormat), Strings.Difference);
167+
CoreLog.Info(diff.ToString());
168+
CoreLog.Info(Strings.LogItemFormat + "\r\n{1}", Strings.UpgradeSequence,
169+
new ActionSequence() { actions });
170+
CoreLog.Info(nameof(Strings.LogItemFormat), Strings.ExpectedTargetModel);
171+
TargetModel.Dump();
172+
CoreLog.Info(nameof(Strings.LogItemFormat), Strings.ActualTargetModel);
173+
CurrentModel.Dump();
174+
}
174175
}
176+
throw new InvalidOperationException(Strings.ExUpgradeSequenceValidationFailure);
175177
}
176178

177179
return new ReadOnlyCollection<NodeAction>(actions.Actions.ToArray());

Orm/Xtensive.Orm/Orm/Building/Builders/DomainBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static Domain Run(DomainBuilderConfiguration builderConfiguration)
3333
ArgumentValidator.EnsureArgumentNotNull(builderConfiguration, nameof(builderConfiguration));
3434

3535
var context = new BuildingContext(builderConfiguration);
36-
using (BuildLog.InfoRegion(nameof(Strings.LogBuildingX), typeof(Domain).GetShortName())) {
36+
using (BuildLog.InfoRegion(nameof(Strings.LogBuildingX), typeof(Domain).Name)) {
3737
new DomainBuilder(context).Run();
3838
}
3939

@@ -51,7 +51,7 @@ private void Run()
5151

5252
private void CreateDomain()
5353
{
54-
using (BuildLog.InfoRegion(nameof(Strings.LogCreatingX), typeof(Domain).GetShortName())) {
54+
using (BuildLog.InfoRegion(nameof(Strings.LogCreatingX), typeof(Domain).Name)) {
5555
var services = context.BuilderConfiguration.Services;
5656
var useSingleConnection =
5757
services.ProviderInfo.Supports(ProviderFeatures.SingleConnection)
@@ -69,7 +69,7 @@ private void CreateHandlers()
6969
var handlers = context.Domain.Handlers;
7070
var services = context.BuilderConfiguration.Services;
7171

72-
using (BuildLog.InfoRegion(nameof(Strings.LogCreatingX), typeof(DomainHandler).GetShortName())) {
72+
using (BuildLog.InfoRegion(nameof(Strings.LogCreatingX), typeof(DomainHandler).Name)) {
7373
// HandlerFactory
7474
handlers.Factory = services.HandlerFactory;
7575

@@ -93,7 +93,7 @@ private void CreateHandlers()
9393

9494
private void CreateServices()
9595
{
96-
using (BuildLog.InfoRegion(nameof(Strings.LogCreatingX), typeof(IServiceContainer).GetShortName())) {
96+
using (BuildLog.InfoRegion(nameof(Strings.LogCreatingX), typeof(IServiceContainer).Name)) {
9797
var domain = context.Domain;
9898
var configuration = domain.Configuration;
9999
var userContainerType = configuration.ServiceContainerType ?? typeof(ServiceContainer);

Orm/Xtensive.Orm/Orm/Building/Builders/StorageMappingBuilder.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ private void ProcessAll()
9393
foreach (var type in typesToProcess) {
9494
var underlyingType = type.UnderlyingType;
9595
if (verbose)
96-
BuildLog.Info(nameof(Strings.LogProcessingX), underlyingType.GetShortName());
96+
if (BuildLog.IsLogged(LogLevel.Info))
97+
BuildLog.Info(nameof(Strings.LogProcessingX), underlyingType.GetShortName());
9798
var request = new MappingRequest(underlyingType.Assembly, underlyingType.Namespace);
9899
MappingResult result;
99100
if (!mappingCache.TryGetValue(request, out result)) {
@@ -102,7 +103,8 @@ private void ProcessAll()
102103
}
103104
else {
104105
if (verbose)
105-
BuildLog.Info(nameof(Strings.LogReusingCachedMappingInformationForX), underlyingType.GetShortName());
106+
if (BuildLog.IsLogged(LogLevel.Info))
107+
BuildLog.Info(nameof(Strings.LogReusingCachedMappingInformationForX), underlyingType.GetShortName());
106108
}
107109
type.MappingDatabase = result.MappingDatabase;
108110
type.MappingSchema = result.MappingSchema;
@@ -117,7 +119,8 @@ private MappingResult Process(Type type)
117119
var resultSchema = !string.IsNullOrEmpty(rule.Schema) ? rule.Schema : defaultSchema;
118120

119121
if (verbose)
120-
BuildLog.Info(nameof(Strings.ApplyingRuleXToY), rule, type.GetShortName());
122+
if (BuildLog.IsLogged(LogLevel.Info))
123+
BuildLog.Info(nameof(Strings.ApplyingRuleXToY), rule, type.GetShortName());
121124

122125
return new MappingResult(resultDatabase, resultSchema);
123126
}

Orm/Xtensive.Orm/Orm/Key.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,16 @@ public static Key Parse([NotNull] Domain domain, string source)
305305
/// <inheritdoc/>
306306
public override string ToString()
307307
{
308+
var underlyingType = TypeInfo?.UnderlyingType ?? TypeReference.Type.UnderlyingType;
308309
if (TypeInfo!=null)
309310
return string.Format(
310311
Strings.KeyFormat,
311-
TypeInfo.UnderlyingType.GetShortName(),
312+
underlyingType.IsGenericType || underlyingType.IsNested ? underlyingType.GetShortName() : underlyingType.Name,
312313
Value.ToRegular());
313314

314315
return string.Format(
315316
Strings.KeyFormatUnknownKeyType,
316-
TypeReference.Type.UnderlyingType.GetShortName(),
317+
underlyingType.IsGenericType || underlyingType.IsNested ? underlyingType.GetShortName() : underlyingType.Name,
317318
Value.ToRegular());
318319
}
319320

Orm/Xtensive.Orm/Orm/Linq/TranslatorContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public ApplyParameter GetApplyParameter(CompilableProvider provider)
8080
{
8181
ApplyParameter parameter;
8282
if (!applyParameters.TryGetValue(provider, out parameter)) {
83-
parameter = new ApplyParameter(provider.GetType().GetShortName());
83+
var providerType = provider.GetType();
84+
parameter = new ApplyParameter(providerType.IsGenericType ? providerType.GetShortName() : providerType.Name);
8485
// parameter = new ApplyParameter(provider.ToString());
8586
// ENABLE ONLY FOR DEBUGGING!
8687
// May lead TO entity.ToString() calls, while ToString can be overriden.

Orm/Xtensive.Orm/Orm/Model/Node.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ public virtual void UpdateState()
9292
/// <inheritdoc/>
9393
public override string ToString()
9494
{
95-
return string.Format(Strings.NodeFormat,
96-
name ?? Strings.UnnamedNodeDisplayName, GetType().GetShortName());
95+
var type = GetType();
96+
97+
return string.Format(Strings.NodeFormat,
98+
name ?? Strings.UnnamedNodeDisplayName,
99+
type.IsGenericType ? type.GetShortName() : type.Name);
97100
}
98101

99102

0 commit comments

Comments
 (0)