changeset 23789:94951db84fc0

util: introduce unpacker This allows taking advantage of Python 2.5+'s struct.Struct, which provides a slightly faster unpack due to reusing formats. Sadly, .unpack_from is significantly slower.
author Matt Mackall <mpm@selenic.com>
date Sat, 10 Jan 2015 21:18:31 -0600
parents 316ad725a1dd
children 48284b8f6073
files mercurial/util.py
diffstat 1 files changed, 10 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Sat Jan 10 21:13:10 2015 -0600
+++ b/mercurial/util.py	Sat Jan 10 21:18:31 2015 -0600
@@ -19,7 +19,7 @@
 import errno, shutil, sys, tempfile, traceback
 import re as remod
 import os, time, datetime, calendar, textwrap, signal, collections
-import imp, socket, urllib
+import imp, socket, urllib, struct
 import gc
 
 if os.name == 'nt':
@@ -229,6 +229,15 @@
 import subprocess
 closefds = os.name == 'posix'
 
+def unpacker(fmt):
+    """create a struct unpacker for the specified format"""
+    try:
+        # 2.5+
+        return struct.Struct(fmt).unpack
+    except NameError:
+        # 2.4
+        return lambda buf: struct.unpack(fmt)
+
 def popen2(cmd, env=None, newlines=False):
     # Setting bufsize to -1 lets the system decide the buffer size.
     # The default for bufsize is 0, meaning unbuffered. This leads to