mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 06:11:38 +00:00
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)
This commit is contained in:
@@ -276,12 +276,11 @@ namespace FlatBuffers
|
|||||||
|
|
||||||
public StringOffset CreateString(string s)
|
public StringOffset CreateString(string s)
|
||||||
{
|
{
|
||||||
NotNested();
|
NotNested();
|
||||||
byte[] utf8 = Encoding.UTF8.GetBytes(s);
|
AddByte(0);
|
||||||
AddByte((byte)0);
|
var utf8StringLen = Encoding.UTF8.GetByteCount(s);
|
||||||
StartVector(1, utf8.Length, 1);
|
StartVector(1, utf8StringLen, 1);
|
||||||
Buffer.BlockCopy(utf8, 0, _bb.Data, _space -= utf8.Length,
|
Encoding.UTF8.GetBytes(s, 0, s.Length, _bb.Data, _space -= utf8StringLen);
|
||||||
utf8.Length);
|
|
||||||
return new StringOffset(EndVector().Value);
|
return new StringOffset(EndVector().Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user