Patchwork [1,of,2,V2] py3: optimize py3 compat.bytechr using Struct.pack

login
register
mail settings
Submitter via Mercurial-devel
Date March 15, 2017, 5:33 p.m.
Message ID <db74b2599efc1997b0e7.1489599191@martinvonz.mtv.corp.google.com>
Download mbox | patch
Permalink /patch/19366/
State Accepted
Headers show

Comments

via Mercurial-devel - March 15, 2017, 5:33 p.m.
# HG changeset patch
# User Martin von Zweigbergk <martinvonz@google.com>
# Date 1489595450 25200
#      Wed Mar 15 09:30:50 2017 -0700
# Node ID db74b2599efc1997b0e7e6bb7b699717029c11d8
# Parent  e83302d437485620c9bc25921f724af34cd5dd55
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.
Yuya Nishihara - March 16, 2017, 1:03 p.m.
On Wed, 15 Mar 2017 10:33:11 -0700, Martin von Zweigbergk via Mercurial-devel wrote:
> # HG changeset patch
> # User Martin von Zweigbergk <martinvonz@google.com>
> # Date 1489595450 25200
> #      Wed Mar 15 09:30:50 2017 -0700
> # Node ID db74b2599efc1997b0e7e6bb7b699717029c11d8
> # Parent  e83302d437485620c9bc25921f724af34cd5dd55
> py3: optimize py3 compat.bytechr using Struct.pack

Nice. Queued these, thanks.

Patch

diff -r e83302d43748 -r db74b2599efc mercurial/pycompat.py
--- a/mercurial/pycompat.py	Mon Mar 13 21:43:17 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"""