Skip to content

Commit 217a07c

Browse files
committed
Use throwhelper to increase change of inlining
- Add a ThrowHelper utility class that provides non-returning methods for throwing exceptions, this in turn helps to reduce the code size, there-by increasing the chances of inlining simple (getter/setter) methods, which has cascading CQ improvments in its own. (See: dotnet/coreclr#6103) - Convert checks in the form of `(x < 0 || x > max)` to `(uint) x > max` to further reduce code-size and increase inlining chances - Update MSTest package deps - Drop netfx.props (which I introduces in a previous PR to support building on linux) and replace with the now standard/canonical way of referencing NetFX BCL on all platforms with a <PackageReference> - closes aeron-io#588
1 parent ad5a1cf commit 217a07c

File tree

5 files changed

+66
-47
lines changed

5 files changed

+66
-47
lines changed

csharp/netfx.props

Lines changed: 0 additions & 26 deletions
This file was deleted.

csharp/sbe-dll/ThrowHelper.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
3+
namespace Org.SbeTool.Sbe.Dll
4+
{
5+
/// <summary>
6+
/// Helper class that provides non-returning methods that throw common exception
7+
/// from the generated C# code
8+
/// </summary>
9+
public class ThrowHelper
10+
{
11+
/// <summary>
12+
/// Throws a <see cref="ArgumentOutOfRangeException"/> when the "count" parameter is out of range
13+
/// </summary>
14+
/// <param name="count">the parameter that triggered the exception</param>
15+
public static void ThrowCountOutOfRangeException(int count) =>
16+
throw new ArgumentOutOfRangeException("count", $"Outside allowed range: count={count}");
17+
18+
/// <summary>
19+
/// Throws a <see cref="InvalidOperationException"/> when an invalid operation is invoked on a message (for example: enumerating a group past it's maximal count)
20+
/// </summary>
21+
public static void ThrowInvalidOperationException() =>
22+
throw new InvalidOperationException();
23+
24+
/// <summary>
25+
/// Throws a <see cref="IndexOutOfRangeException" /> when the "index" parameter is out of range
26+
/// </summary>
27+
/// <param name="index">the parameter that triggered the exception</param>
28+
public static void ThrowIndexOutOfRangeException(int index) =>
29+
throw new IndexOutOfRangeException($"index out of range: index={index}");
30+
31+
/// <summary>
32+
/// Throws a <see cref="ArgumentOutOfRangeException"/> when a too-small <see cref="Span{T}"/>
33+
/// is provided to a getter
34+
/// </summary>
35+
/// <param name="length">The length of the too-small Span</param>
36+
public static void ThrowWhenSpanLengthTooSmall(int length) =>
37+
throw new ArgumentOutOfRangeException("dst", $"dst.Length={length} is too small.");
38+
39+
/// <summary>
40+
/// Throws a <see cref="ArgumentOutOfRangeException"/> when a too-large <see cref="Span{T}"/>
41+
/// is provided to a setter
42+
/// </summary>
43+
/// <param name="length">The length of the too-large Span</param>
44+
public static void ThrowWhenSpanLengthTooLarge(int length) =>
45+
throw new ArgumentOutOfRangeException("src", $"src.Length={length} is too large.");
46+
}
47+
}

csharp/sbe-dll/sbe-dll.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
<Import Project="..\netfx.props" />
3-
42
<PropertyGroup>
53
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
64
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
@@ -23,7 +21,8 @@
2321
</ItemGroup>
2422

2523
<ItemGroup>
26-
<PackageReference Include="System.Memory" Version="4.5.1" />
24+
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0-preview.2" PrivateAssets="All" />
25+
<PackageReference Include="System.Memory" Version="4.5.3" />
2726
</ItemGroup>
2827

2928
</Project>

csharp/sbe-tests/sbe-tests.csproj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
<Import Project="..\netfx.props" />
3-
42
<PropertyGroup>
53
<TargetFrameworks>net45;netcoreapp2.1</TargetFrameworks>
64
<RootNamespace>Org.SbeTool.Sbe.Tests</RootNamespace>
@@ -25,9 +23,10 @@
2523
</ItemGroup>
2624

2725
<ItemGroup>
28-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.1" />
29-
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
30-
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
26+
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0-preview.2" PrivateAssets="All" />
27+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
28+
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
29+
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
3130
</ItemGroup>
3231

3332
<ItemGroup>

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/csharp/CSharpGenerator.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,16 @@ private void generateGroupClassHeader(
210210
final String typeForBlockLength = cSharpTypeName(tokens.get(index + 2).encoding().primitiveType());
211211
final String typeForNumInGroup = cSharpTypeName(numInGroupToken.encoding().primitiveType());
212212

213+
final String throwCondition = numInGroupToken.encoding().applicableMinValue().longValue() == 0 ?
214+
"if ((uint) count > %3$d)\n" :
215+
"if (count < %2$d || count > %3$d)\n";
216+
213217
sb.append(String.format("\n" +
214218
indent + INDENT + "public void WrapForEncode(%1$s parentMessage, DirectBuffer buffer, int count)\n" +
215219
indent + INDENT + "{\n" +
216-
indent + INDENT + INDENT + "if (count < %2$d || count > %3$d)\n" +
220+
indent + INDENT + INDENT + throwCondition +
217221
indent + INDENT + INDENT + "{\n" +
218-
indent + INDENT + INDENT + INDENT + "throw new ArgumentOutOfRangeException(\"count\",\n" +
219-
indent + INDENT + INDENT + INDENT + INDENT + "\"Outside allowed range: count=\" + count +\n" +
220-
indent + INDENT + INDENT + INDENT + INDENT + "\", min=%2$d, max=%3$d\");\n" +
222+
indent + INDENT + INDENT + INDENT + "ThrowHelper.ThrowCountOutOfRangeException(count);\n" +
221223
indent + INDENT + INDENT + "}\n\n" +
222224
indent + INDENT + INDENT + "_parentMessage = parentMessage;\n" +
223225
indent + INDENT + INDENT + "_buffer = buffer;\n" +
@@ -258,7 +260,7 @@ private void generateGroupEnumerator(final StringBuilder sb, final String groupN
258260
indent + INDENT + "{\n" +
259261
indent + INDENT + INDENT + "if (_index + 1 >= _count)\n" +
260262
indent + INDENT + INDENT + "{\n" +
261-
indent + INDENT + INDENT + INDENT + "throw new InvalidOperationException();\n" +
263+
indent + INDENT + INDENT + INDENT + "ThrowHelper.ThrowInvalidOperationException();\n" +
262264
indent + INDENT + INDENT + "}\n\n" +
263265
indent + INDENT + INDENT + "_offset = _parentMessage.Limit;\n" +
264266
indent + INDENT + INDENT + "_parentMessage.Limit = _offset + _blockLength;\n" +
@@ -811,9 +813,9 @@ private CharSequence generateArrayProperty(
811813
sb.append(String.format("\n" +
812814
indent + "public %1$s Get%2$s(int index)\n" +
813815
indent + "{\n" +
814-
indent + INDENT + "if (index < 0 || index >= %3$d)\n" +
816+
indent + INDENT + "if ((uint) index >= %3$d)\n" +
815817
indent + INDENT + "{\n" +
816-
indent + INDENT + INDENT + "throw new IndexOutOfRangeException(\"index out of range: index=\" + index);\n" +
818+
indent + INDENT + INDENT + "ThrowHelper.ThrowIndexOutOfRangeException(index);\n" +
817819
indent + INDENT + "}\n\n" +
818820
"%4$s" +
819821
indent + INDENT + "return _buffer.%5$sGet%8$s(_offset + %6$d + (index * %7$d));\n" +
@@ -825,9 +827,9 @@ private CharSequence generateArrayProperty(
825827
sb.append(String.format("\n" +
826828
indent + "public void Set%1$s(int index, %2$s value)\n" +
827829
indent + "{\n" +
828-
indent + INDENT + "if (index < 0 || index >= %3$d)\n" +
830+
indent + INDENT + "if ((uint) index >= %3$d)\n" +
829831
indent + INDENT + "{\n" +
830-
indent + INDENT + INDENT + "throw new IndexOutOfRangeException(\"index out of range: index=\" + index);\n" +
832+
indent + INDENT + INDENT + "ThrowHelper.ThrowIndexOutOfRangeException(index);\n" +
831833
indent + INDENT + "}\n\n" +
832834
indent + INDENT + "_buffer.%4$sPut%7$s(_offset + %5$d + (index * %6$d), value);\n" +
833835
indent + "}\n",
@@ -852,8 +854,7 @@ private CharSequence generateArrayProperty(
852854
indent + INDENT + "const int length = %2$d;\n" +
853855
indent + INDENT + "if (dst.Length < length)\n" +
854856
indent + INDENT + "{\n" +
855-
indent + INDENT + INDENT +
856-
"throw new ArgumentOutOfRangeException($\"dst.Length={dst.Length} is too large.\");\n" +
857+
indent + INDENT + INDENT + "ThrowHelper.ThrowWhenSpanLengthTooSmall(dst.Length);\n" +
857858
indent + INDENT + "}\n\n" +
858859
"%3$s" +
859860
indent + INDENT + "_buffer.GetBytes(_offset + %4$d, dst);\n" +
@@ -874,8 +875,7 @@ private CharSequence generateArrayProperty(
874875
indent + INDENT + "const int length = %2$d;\n" +
875876
indent + INDENT + "if (src.Length > length)\n" +
876877
indent + INDENT + "{\n" +
877-
indent + INDENT + INDENT +
878-
"throw new ArgumentOutOfRangeException($\"src.Length={src.Length} is too large.\");\n" +
878+
indent + INDENT + INDENT + "ThrowHelper.ThrowWhenSpanLengthTooLarge(src.Length);\n" +
879879
indent + INDENT + "}\n\n" +
880880
indent + INDENT + "_buffer.SetBytes(_offset + %3$d, src);\n" +
881881
indent + "}\n",

0 commit comments

Comments
 (0)