aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d>2009-04-18 02:01:27 +0000
committerGravatar kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d>2009-04-18 02:01:27 +0000
commita8e8ccf29c081739ed5b2addf9df0f272f01ed5e (patch)
treedd1c6ee5a2f983156667ac9be8948f6d8f317ec5
parent6dcd46c8d21708451bc2f25128816037f8129ceb (diff)
Improve performance of Python serialization. Patch from Will Pierce.
-rw-r--r--CONTRIBUTORS.txt2
-rwxr-xr-xpython/google/protobuf/internal/wire_format.py18
2 files changed, 14 insertions, 6 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index b01fed62..259a6f7e 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -65,3 +65,5 @@ Patch contributors:
* Added generation of field number constants.
Wink Saville <wink@google.com>
* Fixed initialization ordering problem in logging code.
+ Will Pierce <willp@nuclei.com>
+ * Small patch improving performance of in Python serialization.
diff --git a/python/google/protobuf/internal/wire_format.py b/python/google/protobuf/internal/wire_format.py
index 531c9b85..950267f9 100755
--- a/python/google/protobuf/internal/wire_format.py
+++ b/python/google/protobuf/internal/wire_format.py
@@ -227,13 +227,19 @@ def TagByteSize(field_number):
# Private helper function for the *ByteSize() functions above.
def _VarUInt64ByteSizeNoTag(uint64):
- """Returns the bytes required to serialize a single varint.
+ """Returns the number of bytes required to serialize a single varint
+ using boundary value comparisons. (unrolled loop optimization -WPierce)
uint64 must be unsigned.
"""
+ if uint64 <= 0x7f: return 1
+ if uint64 <= 0x3fff: return 2
+ if uint64 <= 0x1fffff: return 3
+ if uint64 <= 0xfffffff: return 4
+ if uint64 <= 0x7ffffffff: return 5
+ if uint64 <= 0x3ffffffffff: return 6
+ if uint64 <= 0x1ffffffffffff: return 7
+ if uint64 <= 0xffffffffffffff: return 8
+ if uint64 <= 0x7fffffffffffffff: return 9
if uint64 > UINT64_MAX:
raise message.EncodeError('Value out of range: %d' % uint64)
- bytes = 1
- while uint64 > 0x7f:
- bytes += 1
- uint64 >>= 7
- return bytes
+ return 10