Commit 0cdacdfb authored by Eric Erhardt's avatar Eric Erhardt Committed by Wouter van Oortmerssen

Remove byte* property in ByteBufferAllocator (#5191)

* Remove byte* property in ByteBufferAllocator.

This allows consumers to read/write into native memory, but without
having to always pin the managed `byte[]` when working with managed
memory. This allows for users to not need to Dispose() ByteBuffers
when they are using the default ByteArrayAllocator class.

Instead, we use `Span<byte> GetSpan()` methods to get access to the
underlying memory buffer.

Fix #5181

* Add a set of benchmark tests.

* Add ReadOnly spans.

This allows consumers to use ReadOnlyMemory<byte> as the backing storage
for ByteBuffers, which is useful in read-only scenarios.

* Run tests using ENABLE_SPAN_T in appveyor.

* Fix FlatBuffers.Test.csproj to work on older MSBuild versions.

* Change the test script to test UNSAFE_BYTEBUFFER

* Address PR feedback.

Remove IDisposable from ByteBuffer.

* Respond to PR feedback.
parent bb584420
...@@ -92,6 +92,9 @@ test_script: ...@@ -92,6 +92,9 @@ test_script:
- "cd FlatBuffers.Test" - "cd FlatBuffers.Test"
- "msbuild.exe /property:Configuration=Release;OutputPath=tempcs /verbosity:minimal FlatBuffers.Test.csproj" - "msbuild.exe /property:Configuration=Release;OutputPath=tempcs /verbosity:minimal FlatBuffers.Test.csproj"
- "tempcs\\FlatBuffers.Test.exe" - "tempcs\\FlatBuffers.Test.exe"
# Run tests with UNSAFE_BYTEBUFFER
- "msbuild.exe /property:Configuration=Release;UnsafeByteBuffer=true;OutputPath=tempcsUnsafe /verbosity:minimal FlatBuffers.Test.csproj"
- "tempcsUnsafe\\FlatBuffers.Test.exe"
# TODO: add more languages. # TODO: add more languages.
- "cd ..\\.." - "cd ..\\.."
......
This diff is collapsed.
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using BenchmarkDotNet.Attributes;
using MyGame.Example;
namespace FlatBuffers.Benchmarks
{
//[EtwProfiler] - needs elevated privileges
[MemoryDiagnoser]
public class FlatBufferBuilderBenchmark
{
private const int NumberOfRows = 10_000;
[Benchmark]
public void BuildNestedMonster()
{
const string nestedMonsterName = "NestedMonsterName";
const short nestedMonsterHp = 600;
const short nestedMonsterMana = 1024;
for (int i = 0; i < NumberOfRows; i++)
{
// Create nested buffer as a Monster type
var fbb1 = new FlatBufferBuilder(16);
var str1 = fbb1.CreateString(nestedMonsterName);
Monster.StartMonster(fbb1);
Monster.AddName(fbb1, str1);
Monster.AddHp(fbb1, nestedMonsterHp);
Monster.AddMana(fbb1, nestedMonsterMana);
var monster1 = Monster.EndMonster(fbb1);
Monster.FinishMonsterBuffer(fbb1, monster1);
var fbb1Bytes = fbb1.SizedByteArray();
fbb1 = null;
// Create a Monster which has the first buffer as a nested buffer
var fbb2 = new FlatBufferBuilder(16);
var str2 = fbb2.CreateString("My Monster");
var nestedBuffer = Monster.CreateTestnestedflatbufferVector(fbb2, fbb1Bytes);
Monster.StartMonster(fbb2);
Monster.AddName(fbb2, str2);
Monster.AddHp(fbb2, 50);
Monster.AddMana(fbb2, 32);
Monster.AddTestnestedflatbuffer(fbb2, nestedBuffer);
var monster = Monster.EndMonster(fbb2);
Monster.FinishMonsterBuffer(fbb2, monster);
}
}
[Benchmark]
public void BuildMonster()
{
for (int i = 0; i < NumberOfRows; i++)
{
var builder = new FlatBufferBuilder(16);
var str1 = builder.CreateString("MonsterName");
Monster.StartMonster(builder);
Monster.AddName(builder, str1);
Monster.AddHp(builder, 600);
Monster.AddMana(builder, 1024);
Monster.AddColor(builder, Color.Blue);
Monster.AddTestbool(builder, true);
Monster.AddTestf(builder, 0.3f);
Monster.AddTestf2(builder, 0.2f);
Monster.AddTestf3(builder, 0.1f);
var monster1 = Monster.EndMonster(builder);
Monster.FinishMonsterBuffer(builder, monster1);
}
}
[Benchmark]
public void TestTables()
{
FlatBufferBuilder builder = new FlatBufferBuilder(1024 * 1024 * 32);
for (int x = 0; x < 500000; ++x)
{
var offset = builder.CreateString("T");
builder.StartObject(4);
builder.AddDouble(3.2);
builder.AddDouble(4.2);
builder.AddDouble(5.2);
builder.AddOffset(offset.Value);
builder.EndObject();
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);UNSAFE_BYTEBUFFER;BYTEBUFFER_NO_BOUNDS_CHECK;ENABLE_SPAN_T</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.3" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.11.3" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\net\FlatBuffers\*.cs" Link="FlatBuffers\%(FileName).cs" />
<Compile Include="..\MyGame\**\*.cs" Link="MyGame\Example\%(FileName).cs" />
</ItemGroup>
</Project>
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using BenchmarkDotNet.Running;
namespace FlatBuffers.Benchmarks
{
public static class Program
{
public static void Main(string[] args)
{
BenchmarkSwitcher
.FromAssembly(typeof(Program).Assembly)
.Run(args);
}
}
}
\ No newline at end of file
...@@ -31,6 +31,10 @@ ...@@ -31,6 +31,10 @@
<PropertyGroup> <PropertyGroup>
<StartupObject /> <StartupObject />
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(UnsafeByteBuffer)' == 'true'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);UNSAFE_BYTEBUFFER</DefineConstants>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core"> <Reference Include="System.Core">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment