Skip to content

Commit fd0c39a

Browse files
committed
The probability of methods inlining increased.
1 parent 9b1897a commit fd0c39a

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

Scope.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Platform.Collections.Lists;
99
using Platform.Reflection;
1010
using Platform.Singletons;
11+
using System.Runtime.CompilerServices;
1112

1213
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
1314

@@ -25,38 +26,50 @@ public class Scope : DisposableBase
2526
private readonly HashSet<object> _blocked = new HashSet<object>();
2627
private readonly Dictionary<Type, object> _resolutions = new Dictionary<Type, object>();
2728

29+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2830
public Scope(bool autoInclude, bool autoExplore)
2931
{
3032
_autoInclude = autoInclude;
3133
_autoExplore = autoExplore;
3234
}
3335

36+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3437
public Scope(bool autoInclude) : this(autoInclude, false) { }
3538

39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3640
public Scope() { }
3741

3842
#region Exclude
3943

44+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4045
public void ExcludeAssemblyOf<T>() => ExcludeAssemblyOfType(typeof(T));
4146

47+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4248
public void ExcludeAssemblyOfType(Type type) => ExcludeAssembly(type.GetAssembly());
4349

50+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4451
public void ExcludeAssembly(Assembly assembly) => assembly.GetCachedLoadableTypes().ForEach(Exclude);
4552

53+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4654
public void Exclude<T>() => Exclude(typeof(T));
4755

56+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4857
public void Exclude(object @object) => _excludes.Add(@object);
4958

5059
#endregion
5160

5261
#region Include
5362

63+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5464
public void IncludeAssemblyOf<T>() => IncludeAssemblyOfType(typeof(T));
5565

66+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5667
public void IncludeAssemblyOfType(Type type) => IncludeAssembly(type.GetAssembly());
5768

69+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5870
public void IncludeAssembly(Assembly assembly) => assembly.GetExportedTypes().ForEach(Include);
5971

72+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6073
public void Include<T>()
6174
{
6275
var types = Types<T>.Array;
@@ -70,6 +83,7 @@ public void Include<T>()
7083
}
7184
}
7285

86+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7387
public void Include(object @object)
7488
{
7589
if (@object == null)
@@ -96,6 +110,7 @@ public void Include(object @object)
96110
/// TODO: Think of interface chaining IDoubletLinks[T] (default) -> IDoubletLinks[T] (checker) -> IDoubletLinks[T] (synchronizer) (may be UseChain[IDoubletLinks[T], Types[DefaultLinks, DefaultLinksDependencyChecker, DefaultSynchronizedLinks]]
97111
/// TODO: Add support for factories
98112
/// </remarks>
113+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
99114
public T Use<T>()
100115
{
101116
if (_excludes.Contains(typeof(T)))
@@ -118,16 +133,20 @@ public T Use<T>()
118133
return resolved;
119134
}
120135

136+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
121137
public T UseSingleton<T>(IFactory<T> factory) => UseAndReturn(Singleton.Get(factory));
122138

139+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
123140
public T UseSingleton<T>(Func<T> creator) => UseAndReturn(Singleton.Get(creator));
124141

142+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
125143
public T UseAndReturn<T>(T @object)
126144
{
127145
Use(@object);
128146
return @object;
129147
}
130148

149+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
131150
public void Use(object @object)
132151
{
133152
Include(@object);
@@ -138,6 +157,7 @@ public void Use(object @object)
138157

139158
#region Resolve
140159

160+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
141161
public bool TryResolve<T>(out T resolved)
142162
{
143163
resolved = default;
@@ -149,6 +169,7 @@ public bool TryResolve<T>(out T resolved)
149169
return result;
150170
}
151171

172+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
152173
public bool TryResolve(Type requiredType, out object resolved)
153174
{
154175
resolved = null;
@@ -223,8 +244,10 @@ public bool TryResolve(Type requiredType, out object resolved)
223244
}
224245
}
225246

247+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
226248
protected virtual void SortConstructors(List<ConstructorInfo> resultConstructors) => resultConstructors.Sort((x, y) => -x.GetParameters().Length.CompareTo(y.GetParameters().Length));
227249

250+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
228251
protected virtual bool TryResolveInstance(List<ConstructorInfo> constructors, out object resolved)
229252
{
230253
for (var i = 0; i < constructors.Count; i++)
@@ -247,6 +270,7 @@ protected virtual bool TryResolveInstance(List<ConstructorInfo> constructors, ou
247270
return false;
248271
}
249272

273+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
250274
private ConstructorInfo[] GetValidConstructors(Type type)
251275
{
252276
var constructors = type.GetConstructors();
@@ -268,6 +292,7 @@ private ConstructorInfo[] GetValidConstructors(Type type)
268292
return constructors;
269293
}
270294

295+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
271296
private bool TryResolveConstructorArguments(ConstructorInfo constructor, out object[] arguments)
272297
{
273298
var parameters = constructor.GetParameters();
@@ -286,6 +311,7 @@ private bool TryResolveConstructorArguments(ConstructorInfo constructor, out obj
286311

287312
#endregion
288313

314+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
289315
protected override void Dispose(bool manual, bool wasDisposed)
290316
{
291317
if (!wasDisposed)

Scope[TInclude].cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
1+
using System.Runtime.CompilerServices;
2+
3+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
24

35
namespace Platform.Scopes
46
{
57
public class Scope<TInclude> : Scope
68
{
9+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
710
public Scope() : this(false, false) { }
11+
12+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
813
public Scope(bool autoInclude) : this(autoInclude, false) { }
14+
15+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
916
public Scope(bool autoInclude, bool autoExplore) : base(autoInclude, autoExplore) => Include<TInclude>();
1017
}
1118
}

Use.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
using Platform.Disposables;
1+
using System.Runtime.CompilerServices;
2+
using Platform.Disposables;
23

34
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
45

56
namespace Platform.Scopes
67
{
78
public static class Use<T>
89
{
9-
public static T Single => Scope.Global.Use<T>();
10+
public static T Single
11+
{
12+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13+
get => Scope.Global.Use<T>();
14+
}
1015

1116
public static Disposable<T> New
1217
{
18+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1319
get
1420
{
1521
var scope = new Scope(autoInclude: true, autoExplore: true);

0 commit comments

Comments
 (0)