From b8187e5b8231148ba532a3f5dae035adfb43346b Mon Sep 17 00:00:00 2001 From: Oli Wilkinson Date: Fri, 11 Dec 2015 14:57:59 -0500 Subject: [PATCH] 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); }