From b8187e5b8231148ba532a3f5dae035adfb43346b Mon Sep 17 00:00:00 2001 From: Oli Wilkinson Date: Fri, 11 Dec 2015 14:57:59 -0500 Subject: [PATCH 1/3] Performance tweak to FlatBufferBuilder.CreateString method to remove the unnecessary byte buffer allocation (See https://github.com/google/flatbuffers/issues/55#issuecomment-164031718 for stats) --- net/FlatBuffers/FlatBufferBuilder.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs index 3866d8340..5230b32fb 100644 --- a/net/FlatBuffers/FlatBufferBuilder.cs +++ b/net/FlatBuffers/FlatBufferBuilder.cs @@ -276,12 +276,11 @@ namespace FlatBuffers public StringOffset CreateString(string s) { - NotNested(); - byte[] utf8 = Encoding.UTF8.GetBytes(s); - AddByte((byte)0); - StartVector(1, utf8.Length, 1); - Buffer.BlockCopy(utf8, 0, _bb.Data, _space -= utf8.Length, - utf8.Length); + NotNested(); + AddByte(0); + var utf8StringLen = Encoding.UTF8.GetByteCount(s); + StartVector(1, utf8StringLen, 1); + Encoding.UTF8.GetBytes(s, 0, s.Length, _bb.Data, _space -= utf8StringLen); return new StringOffset(EndVector().Value); } From be11d2b6ef71f367817000b853724982992d2e45 Mon Sep 17 00:00:00 2001 From: Oli Wilkinson Date: Sat, 12 Dec 2015 11:39:57 -0500 Subject: [PATCH 2/3] C# performance optimization to Pad/Prep methods --- net/FlatBuffers/ByteBuffer.cs | 7 +++++++ net/FlatBuffers/FlatBufferBuilder.cs | 8 +++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/net/FlatBuffers/ByteBuffer.cs b/net/FlatBuffers/ByteBuffer.cs index e2a5f2d80..37779b593 100755 --- a/net/FlatBuffers/ByteBuffer.cs +++ b/net/FlatBuffers/ByteBuffer.cs @@ -146,6 +146,13 @@ namespace FlatBuffers _buffer[offset] = value; } + public void PutByte(int offset, byte value, int count) + { + AssertOffsetAndLength(offset, sizeof(byte) * count); + for (var i = 0; i < count; ++i) + _buffer[offset + i] = value; + } + // this method exists in order to conform with Java ByteBuffer standards public void Put(int offset, byte value) { diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs index 5230b32fb..7d2115885 100644 --- a/net/FlatBuffers/FlatBufferBuilder.cs +++ b/net/FlatBuffers/FlatBufferBuilder.cs @@ -69,10 +69,7 @@ namespace FlatBuffers public void Pad(int size) { - for (var i = 0; i < size; i++) - { - _bb.PutByte(--_space, 0); - } + _bb.PutByte(_space -= size, 0, size); } // Doubles the size of the ByteBuffer, and copies the old data towards @@ -116,7 +113,8 @@ namespace FlatBuffers _space += (int)_bb.Length - oldBufSize; } - Pad(alignSize); + if (alignSize > 0) + Pad(alignSize); } public void PutBool(bool x) From 99249ada014fcef009e1de98d23af13921e4630e Mon Sep 17 00:00:00 2001 From: Aaron Critchley Date: Tue, 15 Dec 2015 21:06:01 +0000 Subject: [PATCH 3/3] Linking to SO tag and improving readability --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 40430e20c..9a4bd5598 100755 --- a/readme.md +++ b/readme.md @@ -18,8 +18,7 @@ Windows, OS X, Linux), see `docs/html/index.html` Discuss FlatBuffers with other developers and users on the [FlatBuffers Google Group][]. File issues on the [FlatBuffers Issues Tracker][] -or post your questions to [stackoverflow.com][] with a mention of -**flatbuffers**. +or post your questions to [stackoverflow.com][] using the [`flatbuffers` tag][]. For applications on Google Play that integrate this tool, usage is tracked. This tracking is done automatically using the embedded version string @@ -34,3 +33,4 @@ you would leave it in. [FlatBuffers Issues Tracker]: http://github.com/google/flatbuffers/issues [stackoverflow.com]: http://www.stackoverflow.com [landing page]: http://google.github.io/flatbuffers + [`flatbuffers` tag]: https://stackoverflow.com/questions/tagged/flatbuffers