Skip to content

ZODB.utils.p64 and u64 could be faster and provide better error reports #216

Description

@jamadden

They currently use struct.[un]pack(">Q", x) which parses the format string each time. Using a pre-allocated Struct object can be quite a bit faster

# Python 2.7
$ python -m perf timeit -s 'from struct import pack' 'pack(">Q", 1234567)'
.....................
Mean +- std dev: 151 ns +- 5 ns
$ python -m perf timeit -s 'from struct import Struct; pack = Struct(">Q").pack' 'pack(1234567)'
.....................
Mean +- std dev: 107 ns +- 6 ns
# Python 3.7
$ python -m perf timeit -s 'from struct import pack' 'pack(">Q", 1234567)'
.....................
Mean +- std dev: 119 ns +- 6 ns
$ python -m perf timeit -s 'from struct import Struct; pack = Struct(">Q").pack' 'pack(1234567)'
.....................
Mean +- std dev: 89.7 ns +- 3.4 ns

Additionally, when there is an error in packing, the resulting struct.error just says something like error: integer out of range for 'Q' format code, without saying what the faulting value is. It would be nice if that information was available. We've run into scenarios where RelStorage over a MySQL connection can start throwing that exception for no apparent reason from storage.new_oid() and knowing the value could help debugging. I think just __traceback_info__ would be enough, and probably cheaper than a try/except that raises a different exception.

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions