Skip to content

Commit 4a27665

Browse files
committed
Inline hot path methods
1 parent 7ba8f9a commit 4a27665

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,90 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23
using System.Threading;
34

45
namespace UnityAsync
56
{
67
public static partial class Await
78
{
9+
static readonly Continuation<WaitForFrames> nextUpdate = new Continuation<WaitForFrames>(new WaitForFrames(1));
10+
static readonly Continuation<WaitForFrames> nextLateUpdate = new Continuation<WaitForFrames>(new WaitForFrames(1), FrameScheduler.LateUpdate);
11+
static readonly Continuation<WaitForFrames> nextFixedUpdate = new Continuation<WaitForFrames>(new WaitForFrames(1), FrameScheduler.FixedUpdate);
12+
813
/// <summary>
914
/// Quick access to Unity's <see cref="System.Threading.SynchronizationContext"/>.
1015
/// </summary>
16+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1117
public static SynchronizationContext UnitySyncContext() => AsyncManager.UnitySyncContext;
1218

1319
/// <summary>
1420
/// Quick access to the background <see cref="System.Threading.SynchronizationContext"/>.
1521
/// </summary>
22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1623
public static SynchronizationContext BackgroundSyncContext() => AsyncManager.BackgroundSyncContext;
1724

1825
/// <summary>
1926
/// Convenience function to skip a single frame, equivalent to Unity's <c>yield return null</c>.
2027
/// </summary>
21-
public static WaitForFrames NextUpdate() => new WaitForFrames(1);
28+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29+
public static Continuation<WaitForFrames> NextUpdate() => nextUpdate;
2230

2331
/// <summary>
2432
/// Convenience function to skip a number of frames, equivalent to multiple <c>yield return null</c>s.
2533
/// </summary>
26-
public static WaitForFrames Updates(int count) => new WaitForFrames(count);
34+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35+
public static Continuation<WaitForFrames> Updates(int count) => new Continuation<WaitForFrames>(new WaitForFrames(count));
2736

2837
/// <summary>
2938
/// Convenience function to skip a single frame and continue in the LateUpdate loop.
3039
/// </summary>
31-
public static Continuation<WaitForFrames> NextLateUpdate() => new WaitForFrames(1).ConfigureAwait(FrameScheduler.LateUpdate);
40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41+
public static Continuation<WaitForFrames> NextLateUpdate() => nextLateUpdate;
3242

3343
/// <summary>
3444
/// Convenience function to skip multiple frames and continue in the LateUpdate loop.
3545
/// </summary>
36-
public static Continuation<WaitForFrames> LateUpdates(int count) => new WaitForFrames(count).ConfigureAwait(FrameScheduler.LateUpdate);
46+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47+
public static Continuation<WaitForFrames> LateUpdates(int count) => new Continuation<WaitForFrames>(new WaitForFrames(count), FrameScheduler.LateUpdate);
3748

3849
/// <summary>
3950
/// Convenience function to skip a single FixedUpdate frame and continue in the FixedUpdate loop. Equivalent to
4051
/// Unity's <see cref="UnityEngine.WaitForFixedUpdate"/>.
4152
/// </summary>
42-
public static Continuation<WaitForFrames> NextFixedUpdate() => new WaitForFrames(1).ConfigureAwait(FrameScheduler.FixedUpdate);
53+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
54+
public static Continuation<WaitForFrames> NextFixedUpdate() => nextFixedUpdate;
4355

4456
/// <summary>
4557
/// Convenience function to skip multiple FixedUpdate frames and continue in the FixedUpdate loop.
4658
/// </summary>
47-
public static Continuation<WaitForFrames> FixedUpdates(int count) => new WaitForFrames(count).ConfigureAwait(FrameScheduler.FixedUpdate);
59+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
60+
public static Continuation<WaitForFrames> FixedUpdates(int count) => new Continuation<WaitForFrames>(new WaitForFrames(count), FrameScheduler.FixedUpdate);
4861

4962
/// <summary>
5063
/// Convenience function to wait for a number of in-game seconds before continuing, equivalent to Unity's
5164
/// <see cref="UnityEngine.WaitForSeconds"/>.
5265
/// </summary>
66+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5367
public static WaitForSeconds Seconds(float duration) => new WaitForSeconds(duration);
5468

5569
/// <summary>
5670
/// Convenience function to wait for a number of unscaled seconds before continuing. Equivalent to Unity's
5771
/// <see cref="UnityEngine.WaitForSecondsRealtime"/>.
5872
/// </summary>
73+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5974
public static WaitForSecondsRealtime SecondsRealtime(float duration) => new WaitForSecondsRealtime(duration);
6075

6176
/// <summary>
6277
/// Convenience function to wait for a condition to return true. Equivalent to Unity's
6378
/// <see cref="UnityEngine.WaitUntil"/>.
6479
/// </summary>
80+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6581
public static WaitUntil Until(Func<bool> condition) => new WaitUntil(condition);
6682

6783
/// <summary>
6884
/// Convenience function to wait for a condition to return false. Equivalent to Unity's
6985
/// <see cref="UnityEngine.WaitWhile"/>.
7086
/// </summary>
87+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7188
public static WaitWhile While(Func<bool> condition) => new WaitWhile(condition);
7289
}
7390
}

UnityAsync/Assets/UnityAsync/Continuation.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ public Continuation(T inst)
5151
Scheduler = FrameScheduler.Update;
5252
}
5353

54+
public Continuation(T inst, FrameScheduler scheduler)
55+
{
56+
instruction = inst;
57+
continuation = null;
58+
owner = AsyncManager.Instance;
59+
Scheduler = scheduler;
60+
}
61+
62+
public Continuation(T inst, CancellationToken cancellationToken, FrameScheduler scheduler)
63+
{
64+
instruction = inst;
65+
continuation = null;
66+
owner = AsyncManager.Instance;
67+
this.cancellationToken = cancellationToken;
68+
Scheduler = scheduler;
69+
}
70+
71+
public Continuation(T inst, Object owner, FrameScheduler scheduler)
72+
{
73+
instruction = inst;
74+
continuation = null;
75+
this.owner = owner;
76+
Scheduler = scheduler;
77+
}
78+
5479
public bool IsCompleted => false;
5580

5681
public void OnCompleted(Action continuation)
@@ -64,6 +89,7 @@ public void OnCompleted(Action continuation)
6489
/// cycle it should be evaluated on.
6590
/// </summary>
6691
/// <returns>A new continuation with updated params.</returns>
92+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6793
public Continuation<T> ConfigureAwait(Object owner, FrameScheduler scheduler)
6894
{
6995
this.owner = owner;
@@ -75,6 +101,7 @@ public Continuation<T> ConfigureAwait(Object owner, FrameScheduler scheduler)
75101
/// Link the continuation's lifespan to a <see cref="UnityEngine.Object"/>.
76102
/// </summary>
77103
/// <returns>A new continuation with updated params.</returns>
104+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
78105
public Continuation<T> ConfigureAwait(Object owner)
79106
{
80107
this.owner = owner;
@@ -85,6 +112,7 @@ public Continuation<T> ConfigureAwait(Object owner)
85112
/// Configure the type of update cycle it should be evaluated on.
86113
/// </summary>
87114
/// <returns>A new continuation with updated params.</returns>
115+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
88116
public Continuation<T> ConfigureAwait(FrameScheduler scheduler)
89117
{
90118
Scheduler = scheduler;
@@ -96,6 +124,7 @@ public Continuation<T> ConfigureAwait(FrameScheduler scheduler)
96124
/// type of update cycle it should be evaluated on.
97125
/// </summary>
98126
/// <returns>A new continuation with updated params.</returns>
127+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
99128
public Continuation<T> ConfigureAwait(CancellationToken cancellationToken, FrameScheduler scheduler)
100129
{
101130
this.cancellationToken = cancellationToken;
@@ -107,13 +136,16 @@ public Continuation<T> ConfigureAwait(CancellationToken cancellationToken, Frame
107136
/// Link the continuation's lifespan to a <see cref="System.Threading.CancellationToken"/>.
108137
/// </summary>
109138
/// <returns>A new continuation with updated params.</returns>
139+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
110140
public Continuation<T> ConfigureAwait(CancellationToken cancellationToken)
111141
{
112142
this.cancellationToken = cancellationToken;
113143
return this;
114144
}
115145

116146
public void GetResult() { }
147+
148+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
117149
public Continuation<T> GetAwaiter() => this;
118150
}
119151
}

0 commit comments

Comments
 (0)