summaryrefslogtreecommitdiff
path: root/net
diff options
context:
space:
mode:
authorWouter van Oortmerssen <aardappel@gmail.com>2016-08-24 09:26:06 -0700
committerGitHub <noreply@github.com>2016-08-24 09:26:06 -0700
commit79d127c86336936f69369d7730d5da78338f5b2b (patch)
treebd857613f6f6e3a2bb30ef7e4e2488ffb5f15979 /net
parent96ab6ade5a1f0af33c37be78f448e68d30b55b34 (diff)
parent4802e8a285e20a1403c6570690ea02813c967f0b (diff)
downloadflatbuffers-79d127c86336936f69369d7730d5da78338f5b2b.tar.gz
flatbuffers-79d127c86336936f69369d7730d5da78338f5b2b.tar.bz2
flatbuffers-79d127c86336936f69369d7730d5da78338f5b2b.zip
Merge pull request #2133 from evolutional/cs-bounds-check
C#: added #define BYTEBUFFER_NO_BOUNDS_CHECK
Diffstat (limited to 'net')
-rwxr-xr-xnet/FlatBuffers/ByteBuffer.cs22
1 files changed, 17 insertions, 5 deletions
diff --git a/net/FlatBuffers/ByteBuffer.cs b/net/FlatBuffers/ByteBuffer.cs
index 5fa1ac7a..37a2c7e6 100755
--- a/net/FlatBuffers/ByteBuffer.cs
+++ b/net/FlatBuffers/ByteBuffer.cs
@@ -14,7 +14,20 @@
* limitations under the License.
*/
-//#define UNSAFE_BYTEBUFFER // uncomment this line to use faster ByteBuffer
+// There are 2 #defines that have an impact on performance of this ByteBuffer implementation
+//
+// UNSAFE_BYTEBUFFER
+// This will use unsafe code to manipulate the underlying byte array. This
+// can yield a reasonable performance increase.
+//
+// BYTEBUFFER_NO_BOUNDS_CHECK
+// This will disable the bounds check asserts to the byte array. This can
+// yield a small performance gain in normal code..
+//
+// Using UNSAFE_BYTEBUFFER and BYTEBUFFER_NO_BOUNDS_CHECK together can yield a
+// performance gain of ~15% for some operations, however doing so is potentially
+// dangerous. Do so at your own risk!
+//
using System;
@@ -22,9 +35,6 @@ namespace FlatBuffers
{
/// <summary>
/// Class to mimic Java's ByteBuffer which is used heavily in Flatbuffers.
- /// If your execution environment allows unsafe code, you should enable
- /// unsafe code in your project and #define UNSAFE_BYTEBUFFER to use a
- /// MUCH faster version of ByteBuffer.
/// </summary>
public class ByteBuffer
{
@@ -126,11 +136,14 @@ namespace FlatBuffers
}
#endif // !UNSAFE_BYTEBUFFER
+
private void AssertOffsetAndLength(int offset, int length)
{
+ #if !BYTEBUFFER_NO_BOUNDS_CHECK
if (offset < 0 ||
offset > _buffer.Length - length)
throw new ArgumentOutOfRangeException();
+ #endif
}
public void PutSbyte(int offset, sbyte value)
@@ -200,7 +213,6 @@ namespace FlatBuffers
public unsafe void PutUlong(int offset, ulong value)
{
AssertOffsetAndLength(offset, sizeof(ulong));
-
fixed (byte* ptr = _buffer)
{
*(ulong*)(ptr + offset) = BitConverter.IsLittleEndian