aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers/Bytes.cs
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-06-08 15:50:58 -0500
committerrogerk <devnull@localhost>2011-06-08 15:50:58 -0500
commit2772dfe8a1eae7c942bb50d84bda3f45b5d7b683 (patch)
tree0171c98d38d419780317398595c511810a19e203 /src/ProtocolBuffers/Bytes.cs
parent0e2d144eb6e08f841ed4476cfff23ad462bbbcd9 (diff)
downloadprotobuf-2772dfe8a1eae7c942bb50d84bda3f45b5d7b683.tar.gz
protobuf-2772dfe8a1eae7c942bb50d84bda3f45b5d7b683.tar.bz2
protobuf-2772dfe8a1eae7c942bb50d84bda3f45b5d7b683.zip
Performance fix for float/double write bytes. Performance fix, do not use Array.Copy.
Diffstat (limited to 'src/ProtocolBuffers/Bytes.cs')
-rw-r--r--src/ProtocolBuffers/Bytes.cs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/ProtocolBuffers/Bytes.cs b/src/ProtocolBuffers/Bytes.cs
new file mode 100644
index 00000000..88bc16f8
--- /dev/null
+++ b/src/ProtocolBuffers/Bytes.cs
@@ -0,0 +1,32 @@
+namespace Google.ProtocolBuffers
+{
+ /// <summary>
+ /// Provides a utility routine to copy small arrays much more quickly than Buffer.BlockCopy
+ /// </summary>
+ static class Bytes
+ {
+ /// <summary>
+ /// The threshold above which you should use Buffer.BlockCopy rather than Bytes.Copy
+ /// </summary>
+ const int CopyThreshold = 12;
+ /// <summary>
+ /// Determines which copy routine to use based on the number of bytes to be copied.
+ /// </summary>
+ public static void Copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
+ {
+ if (count > CopyThreshold)
+ global::System.Buffer.BlockCopy(src, srcOffset, dst, dstOffset, count);
+ else
+ ByteCopy(src, srcOffset, dst, dstOffset, count);
+ }
+ /// <summary>
+ /// Copyies the bytes provided with a for loop, faster when there are only a few bytes to copy
+ /// </summary>
+ public static void ByteCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
+ {
+ int stop = srcOffset + count;
+ for (int i = srcOffset; i < stop; i++)
+ dst[dstOffset++] = src[i];
+ }
+ }
+} \ No newline at end of file