aboutsummaryrefslogtreecommitdiff
path: root/csharp/ProtocolBuffers/CodedOutputStream.cs
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2008-08-14 20:38:06 +0100
committerJon Skeet <skeet@pobox.com>2008-08-14 20:38:06 +0100
commit38da52d3490a329be0407438eeddff6da65e5085 (patch)
tree8651cf7d93ba137e80b92f7e497ccdc33477e403 /csharp/ProtocolBuffers/CodedOutputStream.cs
parent272d384f6ad064756edc8d9778c5bcf667783821 (diff)
downloadprotobuf-38da52d3490a329be0407438eeddff6da65e5085.tar.gz
protobuf-38da52d3490a329be0407438eeddff6da65e5085.tar.bz2
protobuf-38da52d3490a329be0407438eeddff6da65e5085.zip
Micro-optimisations around varints and strings.
Diffstat (limited to 'csharp/ProtocolBuffers/CodedOutputStream.cs')
-rw-r--r--csharp/ProtocolBuffers/CodedOutputStream.cs44
1 files changed, 34 insertions, 10 deletions
diff --git a/csharp/ProtocolBuffers/CodedOutputStream.cs b/csharp/ProtocolBuffers/CodedOutputStream.cs
index e5765842..0abfa39a 100644
--- a/csharp/ProtocolBuffers/CodedOutputStream.cs
+++ b/csharp/ProtocolBuffers/CodedOutputStream.cs
@@ -172,15 +172,17 @@ namespace Google.ProtocolBuffers {
/// </summary>
public void WriteString(int fieldNumber, string value) {
WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
- // TODO(jonskeet): Optimise this if possible
- // Unfortunately there does not appear to be any way to tell Java to encode
- // UTF-8 directly into our buffer, so we have to let it create its own byte
- // array and then copy. In .NET we can do the same thing very easily,
- // so we don't need to worry about only writing one buffer at a time.
- // We can optimise later.
- byte[] bytes = Encoding.UTF8.GetBytes(value);
- WriteRawVarint32((uint)bytes.Length);
- WriteRawBytes(bytes);
+ // Optimise the case where we have enough space to write
+ // the string directly to the buffer, which should be common.
+ int length = Encoding.UTF8.GetByteCount(value);
+ WriteRawVarint32((uint) length);
+ if (limit - position >= length) {
+ Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, position);
+ position += length;
+ } else {
+ byte[] bytes = Encoding.UTF8.GetBytes(value);
+ WriteRawBytes(bytes);
+ }
}
/// <summary>
@@ -290,7 +292,7 @@ namespace Google.ProtocolBuffers {
WriteRawVarint32(WireFormat.MakeTag(fieldNumber, type));
}
- public void WriteRawVarint32(uint value) {
+ private void SlowWriteRawVarint32(uint value) {
while (true) {
if ((value & ~0x7F) == 0) {
WriteRawByte(value);
@@ -302,6 +304,28 @@ namespace Google.ProtocolBuffers {
}
}
+ /// <summary>
+ /// Writes a 32 bit value as a varint. The fast route is taken when
+ /// there's enough buffer space left to whizz through without checking
+ /// for each byte; otherwise, we resort to calling WriteRawByte each time.
+ /// </summary>
+ public void WriteRawVarint32(uint value) {
+ if (position + 5 > limit) {
+ SlowWriteRawVarint32(value);
+ return;
+ }
+
+ while (true) {
+ if ((value & ~0x7F) == 0) {
+ buffer[position++] = (byte) value;
+ return;
+ } else {
+ buffer[position++] = (byte)((value & 0x7F) | 0x80);
+ value >>= 7;
+ }
+ }
+ }
+
public void WriteRawVarint64(ulong value) {
while (true) {
if ((value & ~0x7FUL) == 0) {