changeset 31424:4acc49335a6e

py3: optimize py3 compat.bytechr using Struct.pack With Python 3.4.3, timeit says 0.437 usec -> 0.0685 usec. With Python 3.6, timeit says 0.157 usec -> 0.0907 usec. So it's faster on both versions, but the speedup varies a lot. Thanks to Gregory Szorc for the suggestion.
author Martin von Zweigbergk <martinvonz@google.com>
date Wed, 15 Mar 2017 09:30:50 -0700
parents 568d80b24b3a
children 63a39d647888
files mercurial/pycompat.py
diffstat 1 files changed, 2 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/pycompat.py	Wed Mar 15 19:26:20 2017 -0700
+++ b/mercurial/pycompat.py	Wed Mar 15 09:30:50 2017 -0700
@@ -38,6 +38,7 @@
     import builtins
     import functools
     import io
+    import struct
 
     fsencode = os.fsencode
     fsdecode = os.fsdecode
@@ -73,8 +74,7 @@
     if getattr(sys, 'argv', None) is not None:
         sysargv = list(map(os.fsencode, sys.argv))
 
-    def bytechr(i):
-        return bytes([i])
+    bytechr = struct.Struct('>B').pack
 
     def iterbytestr(s):
         """Iterate bytes as if it were a str object of Python 2"""