Skip to content

Commit

Permalink
Additional value struct tests (#199)
Browse files Browse the repository at this point in the history
* Additional value struct tests

* More

* More tests
  • Loading branch information
jamescourtney authored Aug 24, 2021
1 parent fa2c7bf commit 7732270
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 8 deletions.
26 changes: 25 additions & 1 deletion src/Benchmarks/ExperimentalBenchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class Benchmark
private readonly byte[] data = new byte[10 * 1024 * 1024];

private ArrayInputBuffer inputBuffer;
//private UnsafeSpanWriter2 spanWriter;
private ValueTable valueTable;
private ValueTable_Unsafe unsafeValueTable;
private Table table;

[GlobalSetup]
public void Setup()
Expand All @@ -55,6 +57,10 @@ public void Setup()

ValueTable.Serializer.Write(data, t);
inputBuffer = new ArrayInputBuffer(data);

valueTable = new ValueTable(ValueTable.Serializer.Parse(data));
unsafeValueTable = new ValueTable_Unsafe(ValueTable_Unsafe.Serializer.Parse(data));
table = new Table(Table.Serializer.Parse(data));
}

[Benchmark]
Expand Down Expand Up @@ -83,6 +89,12 @@ public int ParseAndTraverse_Value()
return sum;
}

[Benchmark]
public int Serialize_Value()
{
return ValueTable.Serializer.Write(this.data, this.valueTable);
}

[Benchmark]
public int ParseAndTraverse_Value_Unsafe()
{
Expand All @@ -109,6 +121,12 @@ public int ParseAndTraverse_Value_Unsafe()
return sum;
}

[Benchmark]
public int Serialize_Value_Unsafe()
{
return ValueTable_Unsafe.Serializer.Write(this.data, this.unsafeValueTable);
}

[Benchmark]
public int ParseAndTraverse_Ref()
{
Expand All @@ -134,6 +152,12 @@ public int ParseAndTraverse_Ref()

return sum;
}

[Benchmark]
public int Serialize_Ref()
{
return Table.Serializer.Write(this.data, this.table);
}
}

public class Program
Expand Down
4 changes: 1 addition & 3 deletions src/FlatSharp/TypeModel/ValueStructTypeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,7 @@ public override void Initialize()
throw new InvalidFlatBufferDefinitionException($"Can't create type model from type {this.ClrType.GetCompilableTypeName()} because it is not public.");
}

this.CanMarshalWhenLittleEndian =
UnsafeSizeOf(this.ClrType) == this.inlineSize &&
Marshal.SizeOf(this.ClrType) == this.inlineSize;
this.CanMarshalWhenLittleEndian = UnsafeSizeOf(this.ClrType) == this.inlineSize;
}

private static int UnsafeSizeOf(Type t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ValueStructTestCases
[Fact]
public void Basics()
{
Assert.Equal(148, Marshal.SizeOf<ValueStruct>());
Assert.Equal(148, Unsafe.SizeOf<ValueStruct>());
}

[Fact]
Expand Down Expand Up @@ -334,8 +334,8 @@ private static void AssertStructsEqual(ValueStruct a , ValueStruct b)
Assert.Equal(a.D(i), b.D(i));
}

Span<byte> scratchA = stackalloc byte[Marshal.SizeOf<ValueStruct>()];
Span<byte> scratchB = stackalloc byte[Marshal.SizeOf<ValueStruct>()];
Span<byte> scratchA = stackalloc byte[Unsafe.SizeOf<ValueStruct>()];
Span<byte> scratchB = stackalloc byte[Unsafe.SizeOf<ValueStruct>()];

MemoryMarshal.Cast<byte, ValueStruct>(scratchA)[0] = a;
MemoryMarshal.Cast<byte, ValueStruct>(scratchB)[0] = b;
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/FlatSharpTests/OracleTests/AlignmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public class StructVectors
public FiveByteStruct AlignmentVec_0 { get; set; }

[FlatBufferItem(1)]
public FiveByteStruct AlignmentVec_1 { get; set; }
public ValueFiveByteStruct AlignmentVec_1 { get; set; }

[FlatBufferItem(2)]
public FiveByteStruct AlignmentVec_2 { get; set; }
Expand Down
Loading

0 comments on commit 7732270

Please sign in to comment.