summaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
authorWouter van Oortmerssen <wvo@google.com>2015-12-14 15:16:46 -0800
committerWouter van Oortmerssen <wvo@google.com>2015-12-14 15:16:46 -0800
commit2ce5d6c2b664db93fad07c35fd87df8ffb647bb6 (patch)
tree526d96e78eea4ad364e9f3a447c13b99b45e5d60 /net
parent4dcaec7938e0a9fe9f0451fe296b6151e3554275 (diff)
parentbe11d2b6ef71f367817000b853724982992d2e45 (diff)
downloadflatbuffers-2ce5d6c2b664db93fad07c35fd87df8ffb647bb6.tar.gz
flatbuffers-2ce5d6c2b664db93fad07c35fd87df8ffb647bb6.tar.bz2
flatbuffers-2ce5d6c2b664db93fad07c35fd87df8ffb647bb6.zip
Merge pull request #2090 from evolutional/cs-perf-string
C# - Performance optimizations
Diffstat (limited to 'net')
-rwxr-xr-xnet/FlatBuffers/ByteBuffer.cs7
-rw-r--r--net/FlatBuffers/FlatBufferBuilder.cs19
2 files changed, 15 insertions, 11 deletions
diff --git a/net/FlatBuffers/ByteBuffer.cs b/net/FlatBuffers/ByteBuffer.cs
index e2a5f2d8..37779b59 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 3866d834..7d211588 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)
@@ -276,12 +274,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);
}