diff options
author | Oli Wilkinson <oli@evolutional.co.uk> | 2015-12-11 14:57:59 -0500 |
---|---|---|
committer | Oli Wilkinson <oli@evolutional.co.uk> | 2015-12-11 14:57:59 -0500 |
commit | b8187e5b8231148ba532a3f5dae035adfb43346b (patch) | |
tree | 7464826f77afd2cfc11b4ef854585f8ca2d74f1e | |
parent | 42b48bd55faf0e4a2a957f03e1bd3817fd6b25f0 (diff) | |
download | flatbuffers-b8187e5b8231148ba532a3f5dae035adfb43346b.tar.gz flatbuffers-b8187e5b8231148ba532a3f5dae035adfb43346b.tar.bz2 flatbuffers-b8187e5b8231148ba532a3f5dae035adfb43346b.zip |
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)
-rw-r--r-- | net/FlatBuffers/FlatBufferBuilder.cs | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs index 3866d834..5230b32f 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); } |