This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: uuid.bytes optimization
Type: performance Stage: resolved
Components: Benchmarks, Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: alex, kevinlondon, python-dev, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2014-08-04 02:05 by kevinlondon, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
uuid_bytes_update.patch kevinlondon, 2014-08-04 02:05 A patch with the proposed changes. review
modernize_uuid.patch serhiy.storchaka, 2014-08-04 08:09 review
Messages (10)
msg224671 - (view) Author: Kevin London (kevinlondon) * Date: 2014-08-04 02:05
Generating the byte representation of a UUID object can be a little faster by using binascii's unhexlify on the hex value of the UUID object. In my testing, I saw about a 5.5x speed increase with the attached changes. 

Here are a set of benchmarks that I ran, which are inspired by Wang Chun's benchmarks on http://bugs.python.org/issue5885:

https://gist.github.com/kevinlondon/d3bb32d5a784f78731fa

My times:

kevin$ python uuid_benchmark.py 100000
Original Average: 8.049 microseconds
Updated Average: 1.447 microseconds
 
I re-ran all of the tests with the patched uuid module and they passed. Here's my patchcheck output as well:

kevin$ make patchcheck
./python.exe ./Tools/scripts/patchcheck.py
Getting the list of files that have been added/changed ... 1 file
Fixing whitespace ... 0 files
Fixing C file whitespace ... 0 files
Fixing docs whitespace ... 0 files
Docs modified ... NO
Misc/ACKS updated ... NO
Misc/NEWS updated ... NO
configure regenerated ... not needed
pyconfig.h.in regenerated ... not needed

Thanks!
msg224672 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2014-08-04 02:10
`self.int.to_bytes(16, byteorder='big')` looks to be even faster (about 3x on my machine)
msg224673 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2014-08-04 02:10
What I said only applies to the Python3 version of this patch.

Thanks for submitting this.
msg224676 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2014-08-04 03:26
I also like the way the patch cleans-up the code.
msg224697 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-08-04 08:09
If such changes are acceptable, here is a part of my large patch fo modernizing stdlib sources.

Some microbenchmarks:

$ ./python -m timeit -s "from uuid import NAMESPACE_DNS as u" -- "u.bytes"
$ ./python -m timeit -s "from uuid import NAMESPACE_DNS as u" -- "u.bytes_le"
$ ./python -m timeit -s "from uuid import UUID" -- "UUID(bytes_le=b'abcdefghijklmnop')"

Before patch:
10000 loops, best of 3: 66.9 usec per loop
10000 loops, best of 3: 102 usec per loop
10000 loops, best of 3: 65.2 usec per loop

After patch:
100000 loops, best of 3: 3.98 usec per loop
100000 loops, best of 3: 10.8 usec per loop
10000 loops, best of 3: 32.1 usec per loop
msg224700 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-08-04 08:28
Just noticed that this patch also fixes a bug. When os.popen() fails in _ipconfig_getnode(), pipe is not set, and then NameError is raised in the finally branch.
msg226498 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-09-06 17:36
Alex, Raymond, could you please make a review of my patch?
msg226499 - (view) Author: Alex Gaynor (alex) * (Python committer) Date: 2014-09-06 17:55
Patch looks good to me.
msg226506 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-09-06 19:21
New changeset f7b5038d3102 by Serhiy Storchaka in branch 'default':
Issue #22131: Modernized the code of the uuid module.
http://hg.python.org/cpython/rev/f7b5038d3102

New changeset d8c6b15a2ae3 by Serhiy Storchaka in branch '2.7':
Issue #22131: Fixed a bug in handling an error occured during reading from
http://hg.python.org/cpython/rev/d8c6b15a2ae3

New changeset 8a61a287776d by Serhiy Storchaka in branch '3.4':
Issue #22131: Fixed a bug in handling an error occured during reading from
http://hg.python.org/cpython/rev/8a61a287776d
msg226507 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-09-06 19:51
Thank you Alex.
History
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66329
2014-09-06 19:51:21serhiy.storchakasetstatus: open -> closed
messages: + msg226507

assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2014-09-06 19:21:25python-devsetnosy: + python-dev
messages: + msg226506
2014-09-06 17:55:03alexsetmessages: + msg226499
2014-09-06 17:36:48serhiy.storchakasetmessages: + msg226498
2014-08-04 13:45:11pitrousetstage: patch review
versions: - Python 2.7
2014-08-04 12:14:02ronaldoussorensetassignee: ronaldoussoren -> (no value)

components: - macOS
nosy: - ronaldoussoren
2014-08-04 08:28:50serhiy.storchakasetmessages: + msg224700
2014-08-04 08:09:00serhiy.storchakasetfiles: + modernize_uuid.patch
nosy: + serhiy.storchaka
messages: + msg224697

2014-08-04 03:26:45rhettingersetnosy: + rhettinger
messages: + msg224676
2014-08-04 02:11:03alexsetversions: + Python 3.5
2014-08-04 02:10:58alexsetmessages: + msg224673
2014-08-04 02:10:00alexsetnosy: + alex
messages: + msg224672
2014-08-04 02:05:56kevinlondoncreate