Skip to content

Commit

Permalink
Refactor exceptions and string concat (#420)
Browse files Browse the repository at this point in the history
Switch exceptions to be non-inlined methods from a helper class. This reduces JIT code size.
  • Loading branch information
jamescourtney committed Feb 16, 2024
1 parent 6260caf commit a1fad43
Show file tree
Hide file tree
Showing 49 changed files with 419 additions and 253 deletions.
2 changes: 1 addition & 1 deletion src/Benchmarks/Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<TargetFrameworks>net8.0</TargetFrameworks>
<DelaySign>false</DelaySign>
<SignAssembly>false</SignAssembly>
<DefineConstants>$(DefineContants);CURRENT_VERSION_ONLY;FLATSHARP_7_0_0_OR_GREATER;RUN_COMPARISON_BENCHMARKS</DefineConstants>
<DefineConstants>$(DefineContants);CURRENT_VERSION_ONLY;FLATSHARP_7_0_0_OR_GREATER</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions src/Benchmarks/Benchmark/FBBench/FBBenchCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -932,15 +932,19 @@ public class Foo
public struct Foo_ValueType
{
[ProtoMember(1), FieldOffset(0)]
[FlatBufferMetadataAttribute(FlatBufferMetadataKind.Accessor, "", "Id")]
public ulong Id;

[ProtoMember(2), FieldOffset(8)]
[FlatBufferMetadataAttribute(FlatBufferMetadataKind.Accessor, "", "Count")]
public short Count;

[ProtoMember(3), FieldOffset(10)]
[FlatBufferMetadataAttribute(FlatBufferMetadataKind.Accessor, "", "Prefix")]
public sbyte Prefix;

[ProtoMember(4), FieldOffset(12)]
[FlatBufferMetadataAttribute(FlatBufferMetadataKind.Accessor, "", "Length")]
public uint Length;
}

Expand All @@ -967,15 +971,19 @@ public class Bar
public struct Bar_ValueType
{
[ProtoMember(1), FieldOffset(0)]
[FlatBufferMetadataAttribute(FlatBufferMetadataKind.Accessor, "", "Parent")]
public Foo_ValueType Parent;

[ProtoMember(2), FieldOffset(16)]
[FlatBufferMetadataAttribute(FlatBufferMetadataKind.Accessor, "", "Time")]
public int Time;

[ProtoMember(3), FieldOffset(20)]
[FlatBufferMetadataAttribute(FlatBufferMetadataKind.Accessor, "", "Ratio")]
public float Ratio;

[ProtoMember(4), FieldOffset(24)]
[FlatBufferMetadataAttribute(FlatBufferMetadataKind.Accessor, "", "Size")]
public ushort Size;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Benchmarks/Benchmark/FBBench/FBSerializeBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class FBSerializeBench : FBBenchCore

[Benchmark]
public override void FlatSharp_Serialize() => base.FlatSharp_Serialize();

[Benchmark]
public override void FlatSharp_Serialize_ValueStructs() => base.FlatSharp_Serialize_ValueStructs();

Expand Down
11 changes: 11 additions & 0 deletions src/Benchmarks/Benchmark/FBBench/WriteThroughBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public class Table
public struct ValueStruct_Small
{
[FieldOffset(0)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "Foo")]
public int Foo;
}

Expand All @@ -139,24 +140,34 @@ public struct ValueStruct_Small
public struct ValueStruct_Large
{
[FieldOffset(0)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "A")]
public int A;
[FieldOffset(4)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "B")]
public int B;
[FieldOffset(8)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "C")]
public int C;
[FieldOffset(12)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "D")]
public int D;
[FieldOffset(16)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "E")]
public int E;
[FieldOffset(20)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "F")]
public int F;
[FieldOffset(24)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "G")]
public int G;
[FieldOffset(28)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "H")]
public int H;
[FieldOffset(32)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "I")]
public int I;
[FieldOffset(36)]
[FlatBufferMetadata(FlatBufferMetadataKind.Accessor, "", "J")]
public int J;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Benchmarks/Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public static void Main(string[] args)
.WithLaunchCount(7)
.WithWarmupCount(3)
.WithIterationCount(5)
.WithRuntime(CoreRuntime.Core70);
//.WithEnvironmentVariable(new EnvironmentVariable("DOTNET_TieredPGO", "1"));
.WithRuntime(CoreRuntime.Core80);
//.WithEnvironmentVariable(new EnvironmentVariable("DOTNET_TieredPGO", "0"));

var config = DefaultConfig.Instance
.AddColumn(new[] { StatisticColumn.P25, StatisticColumn.P95 })
Expand All @@ -56,7 +56,7 @@ public static void Main(string[] args)
summaries.Add(BenchmarkRunner.Run(typeof(FBBench.FBSharedStringBench), config));
summaries.Add(BenchmarkRunner.Run(typeof(FBBench.WriteThroughBench), config));
#if CURRENT_VERSION_ONLY
summaries.Add(BenchmarkRunner.Run(typeof(SerializationContextBenchmark), config));
//summaries.Add(BenchmarkRunner.Run(typeof(SerializationContextBenchmark), config));
#endif

foreach (var item in summaries)
Expand Down
34 changes: 23 additions & 11 deletions src/Benchmarks/ExperimentalBenchmark/Benchmark.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,30 @@ attribute "fs_writeThrough";

namespace BenchmarkCore;

struct ValueStruct (fs_valueStruct)
{
a : int;
struct Foo {
id:ulong;
count:short;
prefix:byte;
length:uint;
}

struct RefStruct
{
a : int (fs_writeThrough);
struct Bar {
parent:Foo;
time:int;
ratio:float;
size:ushort;
}

table Outer (fs_serializer:"Lazy")
{
value_items : [ ValueStruct ];
ref_items : [ RefStruct ];
}
table FooBar {
sibling:Bar;
name:string;
rating:double;
postfix:ubyte;
}

table FooBarContainer (fs_serializer) {
list:[FooBar]; // 3 copies of the above
initialized:bool;
fruit:short;
location:string;
}
97 changes: 34 additions & 63 deletions src/Benchmarks/ExperimentalBenchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,88 +24,59 @@
using FlatSharp.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;

namespace BenchmarkCore
{
public class IterationTests
public class Program
{
public const int Length = 1000;
public byte[] buffer;

[Params(FlatBufferDeserializationOption.Lazy, FlatBufferDeserializationOption.Progressive)]
public FlatBufferDeserializationOption Option { get; set; }
private FooBarContainer container;
private byte[] buffer;

[GlobalSetup]
public void Setup()
{
var outer = new Outer { RefItems = new List<RefStruct>(), ValueItems = new List<ValueStruct>(), };

for (int i = 0; i < Length; ++i)
this.container = new()
{
outer.RefItems.Add(new RefStruct { A = i });
outer.ValueItems.Add(new ValueStruct { A = i });
}


buffer = new byte[Outer.Serializer.GetMaxSize(outer)];
Outer.Serializer.Write(buffer, outer);
Fruit = 1,
Initialized = true,
Location = "somewhere",
List = Enumerable.Range(0, 30).Select(x => new FooBar
{
Name = x.ToString(),
Postfix = (byte)x,
Rating = x,
Sibling = new Bar
{
Parent = new Foo
{
Count = 30,
Id = 10,
Length = 20,
Prefix = 40,
},
Ratio = 10,
Size = 10,
Time = 10,
}
}).ToList(),
};

this.buffer = new byte[1024 * 1024];
}

[Benchmark]
public int RefNormalTraversal()
public void Serialize()
{
var outer = Outer.Serializer.Parse(buffer, this.Option);

int sum = 0;
IList<RefStruct> items = outer.RefItems!;

for (int i = 0; i < Length; ++i)
{
var item = items[i];
item.A += 3;
}

return sum;
FooBarContainer.Serializer.Write(this.buffer, this.container);
}

[Benchmark]
public int ValueNormalTraversal()
{
var outer = Outer.Serializer.Parse(buffer, this.Option);

int sum = 0;
IList<ValueStruct> items = outer.ValueItems!;

if (items is null)
{
return sum;
}

for (int i = 0; i < Length; ++i)
{
sum += items[i].A;
}

return sum;
}
}

public class Program
{
public static void Main(string[] args)
{
//BenchmarkRunner.Run(typeof(Program).Assembly);
IterationTests t = new();

t.Option = FlatBufferDeserializationOption.Lazy;
t.Setup();

while (true)
{
t.RefNormalTraversal();
}
BenchmarkRunner.Run(typeof(Program).Assembly);
}
}
}
6 changes: 0 additions & 6 deletions src/Benchmarks/ExperimentalBenchmark/buildClr.cmd

This file was deleted.

8 changes: 0 additions & 8 deletions src/Benchmarks/ExperimentalBenchmark/jitDump.cmd

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0</TargetFrameworks>
<DelaySign>false</DelaySign>
<SignAssembly>false</SignAssembly>
<!--<PublishAot>true</PublishAot>-->
<IsPackable>false</IsPackable>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.10" />
<PackageReference Include="FlatSharp.Compiler" Version="7.4.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FlatSharp.Runtime" Version="7.4.0" />
</ItemGroup>

<ItemGroup>
<Compile Include="../ExperimentalBenchmark/Program.cs" />
<FlatSharpSchema Include="../ExperimentalBenchmark/Benchmark.fbs" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion src/FlatSharp.Compiler/SchemaModel/RpcServiceSchemaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

using FlatSharp.CodeGen;
using FlatSharp.Compiler.Schema;

namespace FlatSharp.Compiler.SchemaModel;
Expand Down Expand Up @@ -96,7 +97,7 @@ static Serializer()
public static ISerializer<T> Value
{{
get => __value;
set => __value = value ?? throw new ArgumentNullException(nameof(value));
set => __value = value ?? {typeof(FSThrow).GGCTN()}.{nameof(FSThrow.ArgumentNull)}<ISerializer<T>>(nameof(value));
}}
}}
");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

using FlatSharp.CodeGen;
using FlatSharp.Compiler.Schema;

namespace FlatSharp.Compiler.SchemaModel;
Expand Down Expand Up @@ -155,7 +156,7 @@ public void WriteCode(CodeWriter writer, CompileContext context)
writer.AppendLine($"case {i}: return thisItem.{this.Properties[i].FieldName};");
}

writer.AppendLine($"default: throw new IndexOutOfRangeException();");
writer.AppendLine($"default: return {typeof(FSThrow).GGCTN()}.{nameof(FSThrow.IndexOutOfRange)}<{typeName}>();");
}
}

Expand All @@ -175,7 +176,7 @@ public void WriteCode(CodeWriter writer, CompileContext context)
writer.AppendLine($"case {i}: thisItem.{this.Properties[i].FieldName} = value; break;");
}

writer.AppendLine($"default: throw new IndexOutOfRangeException();");
writer.AppendLine($"default: {typeof(FSThrow).GGCTN()}.{nameof(FSThrow.IndexOutOfRange)}(); break;");
}
}
}
Expand Down
Loading

0 comments on commit a1fad43

Please sign in to comment.