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.
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 fasterAdditionally, when there is an error in packing, the resulting
struct.errorjust says something likeerror: 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 fromstorage.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.