Skip to content

Commit 64faeab

Browse files
Fix T4, Add InliningOptions, Fix some tests.
1 parent cc399f9 commit 64faeab

File tree

11 files changed

+108
-284
lines changed

11 files changed

+108
-284
lines changed

.editorconfig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,6 @@ csharp_style_throw_expression = true:suggestion
368368
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
369369
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
370370

371-
csharp_style_var_for_built_in_types = false:silent
371+
csharp_style_var_for_built_in_types = never
372372
csharp_style_var_when_type_is_apparent = true:warning
373373
csharp_style_var_elsewhere = false:warning
374-
375-
csharp_prefer_simple_using_statement = false:silent

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,4 @@ __pycache__/
286286
*.btm.cs
287287
*.odx.cs
288288
*.xsd.cs
289+
src/SharedInfrastructure/Guard.Numeric.cs

src/SharedInfrastructure/Guard.Numeric.tt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// Licensed under the Apache License, Version 2.0.
66

77
using System;
8-
using System.Diagnostics;
9-
using System.Diagnostics.CodeAnalysis;
108
using System.Runtime.CompilerServices;
119

1210
namespace SixLabors
@@ -34,7 +32,7 @@ for (var i = 0; i < types.Length; i++)
3432
/// <exception cref="ArgumentException">
3533
/// <paramref name="value"/> is greater than the maximum value.
3634
/// </exception>
37-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35+
[MethodImpl(InliningOptions.ShortMethod)]
3836
public static void MustBeLessThan(<#=T#> value, <#=T#> max, string parameterName)
3937
{
4038
if (value >= max)
@@ -53,7 +51,7 @@ for (var i = 0; i < types.Length; i++)
5351
/// <exception cref="ArgumentException">
5452
/// <paramref name="value"/> is greater than the maximum value.
5553
/// </exception>
56-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
54+
[MethodImpl(InliningOptions.ShortMethod)]
5755
public static void MustBeLessThanOrEqualTo(<#=T#> value, <#=T#> max, string parameterName)
5856
{
5957
if (value > max)
@@ -72,7 +70,7 @@ for (var i = 0; i < types.Length; i++)
7270
/// <exception cref="ArgumentException">
7371
/// <paramref name="value"/> is less than the minimum value.
7472
/// </exception>
75-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
73+
[MethodImpl(InliningOptions.ShortMethod)]
7674
public static void MustBeGreaterThan(<#=T#> value, <#=T#> min, string parameterName)
7775
{
7876
if (value <= min)
@@ -91,7 +89,7 @@ for (var i = 0; i < types.Length; i++)
9189
/// <exception cref="ArgumentException">
9290
/// <paramref name="value"/> is less than the minimum value.
9391
/// </exception>
94-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
92+
[MethodImpl(InliningOptions.ShortMethod)]
9593
public static void MustBeGreaterThanOrEqualTo(<#=T#> value, <#=T#> min, string parameterName)
9694
{
9795
if (value < min)
@@ -111,7 +109,7 @@ for (var i = 0; i < types.Length; i++)
111109
/// <exception cref="ArgumentException">
112110
/// <paramref name="value"/> is less than the minimum value of greater than the maximum value.
113111
/// </exception>
114-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
112+
[MethodImpl(InliningOptions.ShortMethod)]
115113
public static void MustBeBetweenOrEqualTo(<#=T#> value, <#=T#> min, <#=T#> max, string parameterName)
116114
{
117115
if (value < min || value > max)

src/SharedInfrastructure/Guard.cs

Lines changed: 13 additions & 203 deletions
Large diffs are not rendered by default.

src/SharedInfrastructure/HashCode.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#pragma warning disable SA1636, SA1600, SA1503, SA1202, SA1101, SA1132, SA1309, SA1520, SA1108, SA1203, SA1028, SA1512, SA1308
2-
1+
// <auto-generated />
32
// SOURCE: https://github.com/dotnet/corefx/blob/master/src/Common/src/CoreLib/System/HashCode.cs
43

54
// Licensed to the .NET Foundation under one or more agreements.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
// Uncomment this for verbose profiler results. DO NOT PUSH TO MAIN!
5+
// #define PROFILING
6+
using System.Runtime.CompilerServices;
7+
8+
namespace SixLabors
9+
{
10+
/// <summary>
11+
/// Global inlining options. Helps temporarily disable inlining for better profiler output.
12+
/// </summary>
13+
internal static class InliningOptions
14+
{
15+
#if PROFILING
16+
public const MethodImplOptions HotPath = MethodImplOptions.NoInlining;
17+
public const MethodImplOptions ShortMethod = MethodImplOptions.NoInlining;
18+
#else
19+
#if SUPPORTS_HOTPATH
20+
public const MethodImplOptions HotPath = MethodImplOptions.AggressiveOptimization;
21+
#else
22+
public const MethodImplOptions HotPath = MethodImplOptions.AggressiveInlining;
23+
#endif
24+
public const MethodImplOptions ShortMethod = MethodImplOptions.AggressiveInlining;
25+
#endif
26+
public const MethodImplOptions ColdPath = MethodImplOptions.NoInlining;
27+
}
28+
}

src/SharedInfrastructure/SharedInfrastructure.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<Compile Include="$(MSBuildThisFileDirectory)ExcludeFromCodeCoverageAttribute.cs" />
1414
<Compile Include="$(MSBuildThisFileDirectory)Guard.cs" />
1515
<Compile Include="$(MSBuildThisFileDirectory)HashCode.cs" />
16+
<Compile Include="$(MSBuildThisFileDirectory)InliningOptions.cs" />
1617
<Compile Include="$(MSBuildThisFileDirectory)MathF.cs" />
1718
<Compile Include="$(MSBuildThisFileDirectory)ThrowHelper.cs" />
1819
</ItemGroup>
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<PropertyGroup Label="Globals">
4-
<ProjectGuid>68a8cc40-6aed-4e96-b524-31b1158fdeea</ProjectGuid>
5-
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
6-
</PropertyGroup>
7-
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
8-
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
9-
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
10-
<PropertyGroup />
11-
<Import Project="SharedInfrastructure.projitems" Label="Shared" />
12-
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
3+
<PropertyGroup Label="Globals">
4+
<ProjectGuid>68a8cc40-6aed-4e96-b524-31b1158fdeea</ProjectGuid>
5+
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
6+
</PropertyGroup>
7+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
8+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
9+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
10+
<Import Project="SharedInfrastructure.projitems" Label="Shared" />
11+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
1312
</Project>

src/SharedInfrastructure/ThrowHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ public static void ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo<T>(
8787
[MethodImpl(MethodImplOptions.NoInlining)]
8888
public static void ThrowArgumentOutOfRangeExceptionForMustBeSizedAtLeast(int minLength, string parameterName)
8989
{
90-
ThrowArgumentException($"Span-s must be at least of length {minLength}!", parameterName);
90+
ThrowArgumentException($"Spans must be at least of length {minLength}!", parameterName);
9191
}
9292

9393
/// <summary>
9494
/// Throws a new <see cref="ArgumentException"/>.
9595
/// </summary>
96-
/// <param name="name">The argument name.</param>
9796
/// <param name="message">The message to include in the exception.</param>
97+
/// <param name="name">The argument name.</param>
9898
/// <exception cref="ArgumentException">Thrown with <paramref name="message"/> and <paramref name="name"/>.</exception>
9999
[MethodImpl(MethodImplOptions.NoInlining)]
100-
public static void ThrowArgumentException(string name, string message)
100+
public static void ThrowArgumentException(string message, string name)
101101
{
102102
throw new ArgumentException(message, name);
103103
}

tests/SharedInfrastructure.Tests/GuardTests.cs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void NotNull_WhenNull_Throws()
2222
[Fact]
2323
public void NotNull_WhenNotNull()
2424
{
25-
Foo foo = new Foo();
25+
var foo = new Foo();
2626
Guard.NotNull(foo, nameof(foo));
2727
}
2828

@@ -133,10 +133,8 @@ public void MustBeLessThan_IsLess_ThrowsNoException()
133133
[InlineData(1, 1)]
134134
public void MustBeLessThan_IsGreaterOrEqual_ThrowsNoException(int value, int max)
135135
{
136-
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
137-
{
138-
Guard.MustBeLessThan(value, max, "myParamName");
139-
});
136+
ArgumentOutOfRangeException exception =
137+
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeLessThan(value, max, "myParamName"));
140138

141139
Assert.Equal("myParamName", exception.ParamName);
142140
Assert.Contains($"Value {value} must be less than {max}.", exception.Message);
@@ -153,10 +151,8 @@ public void MustBeLessThanOrEqualTo_IsLessOrEqual_ThrowsNoException(int value, i
153151
[Fact]
154152
public void MustBeLessThanOrEqualTo_IsGreater_ThrowsNoException()
155153
{
156-
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
157-
{
158-
Guard.MustBeLessThanOrEqualTo(2, 1, "myParamName");
159-
});
154+
ArgumentOutOfRangeException exception =
155+
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeLessThanOrEqualTo(2, 1, "myParamName"));
160156

161157
Assert.Equal("myParamName", exception.ParamName);
162158
Assert.Contains($"Value 2 must be less than or equal to 1.", exception.Message);
@@ -173,10 +169,8 @@ public void MustBeGreaterThan_IsGreater_ThrowsNoException()
173169
[InlineData(1, 1)]
174170
public void MustBeGreaterThan_IsLessOrEqual_ThrowsNoException(int value, int min)
175171
{
176-
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
177-
{
178-
Guard.MustBeGreaterThan(value, min, "myParamName");
179-
});
172+
ArgumentOutOfRangeException exception =
173+
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeGreaterThan(value, min, "myParamName"));
180174

181175
Assert.Equal("myParamName", exception.ParamName);
182176
Assert.Contains($"Value {value} must be greater than {min}.", exception.Message);
@@ -193,10 +187,8 @@ public void MustBeGreaterThanOrEqualTo_IsGreaterOrEqual_ThrowsNoException(int va
193187
[Fact]
194188
public void MustBeGreaterThanOrEqualTo_IsLess_ThrowsNoException()
195189
{
196-
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
197-
{
198-
Guard.MustBeGreaterThanOrEqualTo(1, 2, "myParamName");
199-
});
190+
ArgumentOutOfRangeException exception =
191+
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeGreaterThanOrEqualTo(1, 2, "myParamName"));
200192

201193
Assert.Equal("myParamName", exception.ParamName);
202194
Assert.Contains($"Value 1 must be greater than or equal to 2.", exception.Message);
@@ -216,10 +208,8 @@ public void MustBeBetweenOrEqualTo_IsBetweenOrEqual_ThrowsNoException(int value,
216208
[InlineData(4, 1, 3)]
217209
public void MustBeBetweenOrEqualTo_IsLessOrGreater_ThrowsNoException(int value, int min, int max)
218210
{
219-
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>(() =>
220-
{
221-
Guard.MustBeBetweenOrEqualTo(value, min, max, "myParamName");
222-
});
211+
ArgumentOutOfRangeException exception =
212+
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeBetweenOrEqualTo(value, min, max, "myParamName"));
223213

224214
Assert.Equal("myParamName", exception.ParamName);
225215
Assert.Contains($"Value {value} must be greater than or equal to {min} and less than or equal to {max}.", exception.Message);
@@ -236,13 +226,11 @@ public void MustBeSizedAtLeast_Array_LengthIsGreaterOrEqual_ThrowsNoException(in
236226
[Fact]
237227
public void MustBeSizedAtLeast_Array_LengthIsLess_ThrowsException()
238228
{
239-
ArgumentException exception = Assert.Throws<ArgumentException>(() =>
240-
{
241-
Guard.MustBeSizedAtLeast<int>(new int[] { 1, 2 }, 3, "myParamName");
242-
});
229+
ArgumentException exception =
230+
Assert.Throws<ArgumentException>(() => Guard.MustBeSizedAtLeast<int>(new int[] { 1, 2 }, 3, "myParamName"));
243231

244232
Assert.Equal("myParamName", exception.ParamName);
245-
Assert.Contains("The size must be at least 3", exception.Message);
233+
Assert.Contains("Spans must be at least of length 3", exception.Message);
246234
}
247235
}
248236
}

0 commit comments

Comments
 (0)