Skip to content

Commit 5c440e3

Browse files
committed
Added aggressive inlining to all methods.
1 parent 5732879 commit 5c440e3

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Runtime/MathsHelper.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Created by: DavidFDev
44

55
using System.Diagnostics.Contracts;
6+
using System.Runtime.CompilerServices;
67
using UnityEngine;
78

89
namespace DavidFDev.Maths
@@ -22,6 +23,7 @@ public static class MathsHelper
2223
/// <param name="shift"></param>
2324
/// <returns></returns>
2425
[Pure]
26+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2527
public static float Approach(float start, float end, float shift)
2628
{
2729
if (start < end)
@@ -40,6 +42,7 @@ public static float Approach(float start, float end, float shift)
4042
/// <param name="shift"></param>
4143
/// <returns></returns>
4244
[Pure]
45+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4346
public static float ApproachAngle(float start, float end, float shift)
4447
{
4548
float deltaAngle = Mathf.DeltaAngle(start, end);
@@ -58,6 +61,7 @@ public static float ApproachAngle(float start, float end, float shift)
5861
/// <param name="shift"></param>
5962
/// <returns></returns>
6063
[Pure]
64+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6165
public static float Reduce(float start, float shift)
6266
{
6367
return Approach(start, 0f, shift);
@@ -70,6 +74,7 @@ public static float Reduce(float start, float shift)
7074
/// <param name="shift"></param>
7175
/// <returns></returns>
7276
[Pure]
77+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7378
public static float ReduceAngle(float start, float shift)
7479
{
7580
return ApproachAngle(start, 0f, shift);
@@ -84,6 +89,7 @@ public static float ReduceAngle(float start, float shift)
8489
/// <param name="max"></param>
8590
/// <returns></returns>
8691
[Pure]
92+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8793
public static float Pulse(float time, float frequency, float min, float max)
8894
{
8995
float half = (max - min) * 0.5f;
@@ -97,6 +103,7 @@ public static float Pulse(float time, float frequency, float min, float max)
97103
/// <param name="frequency">How many false..true..false per second (lower is slower).</param>
98104
/// <returns></returns>
99105
[Pure]
106+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
100107
public static bool FlipFlop(float time, float frequency)
101108
{
102109
return Pulse(time, frequency, 0f, 1f) >= 0.5f;
@@ -109,6 +116,7 @@ public static bool FlipFlop(float time, float frequency)
109116
/// <param name="frequency">Blip speed (lower is slower).</param>
110117
/// <returns></returns>
111118
[Pure]
119+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
112120
public static bool Blip(float time, float frequency)
113121
{
114122
return Pulse(time, frequency, 0f, 1f) == 1f;
@@ -120,6 +128,7 @@ public static bool Blip(float time, float frequency)
120128
/// <param name="value"></param>
121129
/// <returns></returns>
122130
[Pure]
131+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
123132
public static float Sign(float value)
124133
{
125134
return value < 0f ? -1f : (value > 0f ? 1f : 0f);
@@ -132,6 +141,7 @@ public static float Sign(float value)
132141
/// <param name="threshold"></param>
133142
/// <returns></returns>
134143
[Pure]
144+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
135145
public static float SignThreshold(float value, float threshold)
136146
{
137147
return Mathf.Abs(value) >= threshold ? Sign(value) : 0f;
@@ -143,6 +153,7 @@ public static float SignThreshold(float value, float threshold)
143153
/// <param name="value"></param>
144154
/// <returns></returns>
145155
[Pure]
156+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
146157
public static Vector2 Sign(Vector2 value)
147158
{
148159
return new Vector2(Sign(value.x), Sign(value.y));
@@ -154,6 +165,7 @@ public static Vector2 Sign(Vector2 value)
154165
/// <param name="value"></param>
155166
/// <returns></returns>
156167
[Pure]
168+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
157169
public static Vector3 Sign(Vector3 value)
158170
{
159171
return new Vector3(Sign(value.x), Sign(value.y), Sign(value.z));
@@ -167,6 +179,7 @@ public static Vector3 Sign(Vector3 value)
167179
/// <param name="max"></param>
168180
/// <returns></returns>
169181
[Pure]
182+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
170183
public static bool Between(float value, float min, float max)
171184
{
172185
return value >= min && value <= max;
@@ -180,6 +193,7 @@ public static bool Between(float value, float min, float max)
180193
/// <param name="max"></param>
181194
/// <returns></returns>
182195
[Pure]
196+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
183197
public static bool Between(int value, int min, int max)
184198
{
185199
return value >= min && value <= max;
@@ -191,6 +205,7 @@ public static bool Between(int value, int min, int max)
191205
/// <param name="value"></param>
192206
/// <returns></returns>
193207
[Pure]
208+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
194209
public static bool IsEven(int value)
195210
{
196211
return value % 2 == 0;
@@ -202,6 +217,7 @@ public static bool IsEven(int value)
202217
/// <param name="value"></param>
203218
/// <returns></returns>
204219
[Pure]
220+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
205221
public static bool IsOdd(int value)
206222
{
207223
return !IsEven(value);
@@ -215,6 +231,7 @@ public static bool IsOdd(int value)
215231
/// <param name="max"></param>
216232
/// <returns></returns>
217233
[Pure]
234+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
218235
public static float Map01(float value, float min, float max)
219236
{
220237
return (value - min) * 1f / (max - min);
@@ -228,6 +245,7 @@ public static float Map01(float value, float min, float max)
228245
/// <param name="max"></param>
229246
/// <returns></returns>
230247
[Pure]
248+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
231249
public static float Map10(float value, float min, float max)
232250
{
233251
return 1f - Map01(value, min, max);
@@ -243,6 +261,7 @@ public static float Map10(float value, float min, float max)
243261
/// <param name="to2"></param>
244262
/// <returns></returns>
245263
[Pure]
264+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
246265
public static float Map(float value, float from1, float from2, float to1, float to2)
247266
{
248267
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
@@ -254,6 +273,7 @@ public static float Map(float value, float from1, float from2, float to1, float
254273
/// <param name="degrees"></param>
255274
/// <returns></returns>
256275
[Pure]
276+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
257277
public static Vector2 GetDirection(float degrees)
258278
{
259279
return new Vector2(Mathf.Cos(degrees * Mathf.Deg2Rad), Mathf.Sin(degrees * Mathf.Deg2Rad));
@@ -265,6 +285,7 @@ public static Vector2 GetDirection(float degrees)
265285
/// <param name="direction"></param>
266286
/// <returns></returns>
267287
[Pure]
288+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
268289
public static float GetAngle(Vector2 direction)
269290
{
270291
return Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
@@ -277,6 +298,7 @@ public static float GetAngle(Vector2 direction)
277298
/// <param name="directionB"></param>
278299
/// <returns></returns>
279300
[Pure]
301+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
280302
public static float GetAngleBetween(Vector2 directionA, Vector2 directionB)
281303
{
282304
return GetAngle(directionA - directionB);
@@ -291,6 +313,7 @@ public static float GetAngleBetween(Vector2 directionA, Vector2 directionB)
291313
/// <param name="degrees"></param>
292314
/// <returns></returns>
293315
[Pure]
316+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
294317
public static bool WithinAngle(Vector2 source, Vector2 target, Vector2 coneDirection, float degrees)
295318
{
296319
Vector2 direction = (target - source).normalized;
@@ -306,6 +329,7 @@ public static bool WithinAngle(Vector2 source, Vector2 target, Vector2 coneDirec
306329
/// <param name="degrees"></param>
307330
/// <returns></returns>
308331
[Pure]
332+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
309333
public static Vector2 RotateVector(Vector2 value, float degrees)
310334
{
311335
return GetDirection(GetAngle(value) + degrees);
@@ -319,6 +343,7 @@ public static Vector2 RotateVector(Vector2 value, float degrees)
319343
/// <param name="pivot"></param>
320344
/// <returns></returns>
321345
[Pure]
346+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
322347
public static Vector2 RotateVector(Vector2 value, float degrees, Vector2 pivot)
323348
{
324349
Vector2 direction = value - pivot;

Runtime/RandomHelper.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Diagnostics.Contracts;
88
using System.Linq;
9+
using System.Runtime.CompilerServices;
910
using UnityEngine;
1011

1112
namespace DavidFDev.Maths
@@ -61,6 +62,7 @@ static RandomHelper()
6162
/// </summary>
6263
/// <returns></returns>
6364
[Pure]
65+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6466
public static bool NextBool()
6567
{
6668
return RNG.Next(1) == 0;
@@ -71,6 +73,7 @@ public static bool NextBool()
7173
/// </summary>
7274
/// <returns></returns>
7375
[Pure]
76+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7477
public static float NextFloat()
7578
{
7679
return (float)RNG.NextDouble();
@@ -82,6 +85,7 @@ public static float NextFloat()
8285
/// <param name="max"></param>
8386
/// <returns></returns>
8487
[Pure]
88+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8589
public static float NextFloat(float max)
8690
{
8791
return NextFloat() * max;
@@ -93,6 +97,7 @@ public static float NextFloat(float max)
9397
/// <param name="max"></param>
9498
/// <returns></returns>
9599
[Pure]
100+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
96101
public static int NextInt(int max)
97102
{
98103
return RNG.Next(max);
@@ -103,6 +108,7 @@ public static int NextInt(int max)
103108
/// </summary>
104109
/// <returns></returns>
105110
[Pure]
111+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
106112
public static float NextAngle()
107113
{
108114
return NextFloat(PI_2);
@@ -113,6 +119,7 @@ public static float NextAngle()
113119
/// </summary>
114120
/// <returns></returns>
115121
[Pure]
122+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
116123
public static Color NextColour()
117124
{
118125
return new Color(NextFloat(), NextFloat(), NextFloat());
@@ -124,6 +131,7 @@ public static Color NextColour()
124131
/// <param name="magnitude"></param>
125132
/// <returns></returns>
126133
[Pure]
134+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
127135
public static Vector2 NextVector2(float magnitude = 1f)
128136
{
129137
return MathsHelper.GetDirection(NextAngle() * Mathf.Rad2Deg) * magnitude;
@@ -135,6 +143,7 @@ public static Vector2 NextVector2(float magnitude = 1f)
135143
/// <param name="magnitude"></param>
136144
/// <returns></returns>
137145
[Pure]
146+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
138147
public static Vector3 NextVector3(float magnitude = 1f)
139148
{
140149
return MathsHelper.GetDirection(NextAngle() * Mathf.Rad2Deg) * magnitude;
@@ -147,6 +156,7 @@ public static Vector3 NextVector3(float magnitude = 1f)
147156
/// <param name="max"></param>
148157
/// <returns></returns>
149158
[Pure]
159+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
150160
public static float Range(float min, float max)
151161
{
152162
return min + NextFloat(max - min);
@@ -159,6 +169,7 @@ public static float Range(float min, float max)
159169
/// <param name="max"></param>
160170
/// <returns></returns>
161171
[Pure]
172+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
162173
public static Vector2 Range(Vector2 min, Vector2 max)
163174
{
164175
return min + new Vector2(NextFloat(max.x - min.x), NextFloat(max.y - min.y));
@@ -171,6 +182,7 @@ public static Vector2 Range(Vector2 min, Vector2 max)
171182
/// <param name="max"></param>
172183
/// <returns></returns>
173184
[Pure]
185+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
174186
public static Vector3 Range(Vector3 min, Vector3 max)
175187
{
176188
return min + new Vector3(NextFloat(max.x - min.x), NextFloat(max.y - min.y), NextFloat(max.z - min.z));
@@ -181,6 +193,7 @@ public static Vector3 Range(Vector3 min, Vector3 max)
181193
/// </summary>
182194
/// <returns></returns>
183195
[Pure]
196+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
184197
public static float MinusOneToOne()
185198
{
186199
return NextFloat(2f) - 1f;
@@ -191,6 +204,7 @@ public static float MinusOneToOne()
191204
/// </summary>
192205
/// <returns></returns>
193206
[Pure]
207+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
194208
public static int MinusOneOrOne()
195209
{
196210
return NextBool() ? -1 : 1;
@@ -202,6 +216,7 @@ public static int MinusOneOrOne()
202216
/// <param name="percent">0.0 - 1.0.</param>
203217
/// <returns></returns>
204218
[Pure]
219+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
205220
public static bool Chance(float percent)
206221
{
207222
return NextFloat() < percent;
@@ -213,6 +228,7 @@ public static bool Chance(float percent)
213228
/// <param name="percent">0 - 100.</param>
214229
/// <returns></returns>
215230
[Pure]
231+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
216232
public static bool Chance(int percent)
217233
{
218234
return NextInt(100) < percent;
@@ -225,6 +241,7 @@ public static bool Chance(int percent)
225241
/// <param name="collection"></param>
226242
/// <returns></returns>
227243
[Pure]
244+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
228245
public static T Choose<T>(this IReadOnlyCollection<T> collection)
229246
{
230247
return collection.ElementAt(NextInt(collection.Count));

0 commit comments

Comments
 (0)