Skip to content

Commit 19c9471

Browse files
authored
perf: Improve performance by introducing caches to hotspots (#182)
Co-authored-by: spkl <spkl>
1 parent 426d817 commit 19c9471

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/dscom/TypeInfoResolver.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ internal sealed class TypeInfoResolver : ITypeLibCache
2525

2626
private readonly Dictionary<Guid, ITypeInfo> _types = new();
2727

28+
private readonly Dictionary<Type, ITypeInfo?> _resolvedTypeInfos = new();
29+
2830
public WriterContext WriterContext { get; }
2931

3032
public TypeInfoResolver(WriterContext writerContext)
@@ -67,6 +69,11 @@ public TypeInfoResolver(WriterContext writerContext)
6769

6870
public ITypeInfo? ResolveTypeInfo(Type type)
6971
{
72+
if (_resolvedTypeInfos.TryGetValue(type, out var typeInfo))
73+
{
74+
return typeInfo;
75+
}
76+
7077
ITypeInfo? retval;
7178
if (type.FullName == "System.Collections.IEnumerator")
7279
{
@@ -126,6 +133,7 @@ public TypeInfoResolver(WriterContext writerContext)
126133
}
127134
}
128135
}
136+
_resolvedTypeInfos[type] = retval;
129137
return retval;
130138
}
131139

src/dscom/writer/MethodWriter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,20 @@ protected virtual short GetParametersCount()
6060
return retVal;
6161
}
6262

63+
private bool? _isComVisible;
64+
6365
protected virtual bool IsComVisible
6466
{
6567
get
6668
{
69+
if (_isComVisible != null)
70+
{
71+
return _isComVisible.Value;
72+
}
73+
6774
var methodAttribute = MethodInfo.GetCustomAttribute<ComVisibleAttribute>();
68-
return methodAttribute == null || methodAttribute.Value;
75+
_isComVisible = methodAttribute == null || methodAttribute.Value;
76+
return _isComVisible.Value;
6977
}
7078
}
7179

src/dscom/writer/PropertyMethodWriter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ public PropertyMethodWriter(InterfaceWriter interfaceWriter, MethodInfo methodIn
2626
MemberInfo = _propertyInfo!;
2727
}
2828

29+
private bool? _isComVisible;
30+
2931
protected override bool IsComVisible
3032
{
3133
get
3234
{
35+
if (_isComVisible != null)
36+
{
37+
return _isComVisible.Value;
38+
}
39+
3340
var propertyAttribute = MemberInfo.GetCustomAttribute<ComVisibleAttribute>();
34-
return (propertyAttribute == null || propertyAttribute.Value) && base.IsComVisible;
41+
_isComVisible = (propertyAttribute == null || propertyAttribute.Value) && base.IsComVisible;
42+
return _isComVisible.Value;
3543
}
3644
}
3745

0 commit comments

Comments
 (0)