summaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
authorOli Wilkinson <oli@evolutional.co.uk>2015-12-11 14:57:59 -0500
committerOli Wilkinson <oli@evolutional.co.uk>2015-12-11 14:57:59 -0500
commitb8187e5b8231148ba532a3f5dae035adfb43346b (patch)
tree7464826f77afd2cfc11b4ef854585f8ca2d74f1e /net
parent42b48bd55faf0e4a2a957f03e1bd3817fd6b25f0 (diff)
downloadflatbuffers-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)
Diffstat (limited to 'net')
-rw-r--r--net/FlatBuffers/FlatBufferBuilder.cs11
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);
}