[New-bugs-announce] [issue44154] Optimize Fraction pickling

Sergey B Kirpichev report at bugs.python.org
Sun May 16 23:58:34 EDT 2021


New submission from Sergey B Kirpichev <skirpichev at gmail.com>:

The current version of the Fraction.__reduce__() method uses str(), which produces bigger dumps, esp. for large components.

C.f.:
>>> import random, pickle
>>> from fractions import Fraction as F
>>> random.seed(1); a = F(*random.random().as_integer_ratio())
>>> for proto in range(pickle.HIGHEST_PROTOCOL + 1):
...     print(len(pickle.dumps(a, proto)))
... 
71
70
71
71
77
77
>>> b = a**13
>>> for proto in range(pickle.HIGHEST_PROTOCOL + 1):
...     print(len(pickle.dumps(b, proto)))
... 
444
443
444
444
453
453

vs the attached patch:
>>> for proto in range(pickle.HIGHEST_PROTOCOL + 1):
...     print(len(pickle.dumps(a, proto)))
... 
71
68
49
49
59
59
>>> for proto in range(pickle.HIGHEST_PROTOCOL + 1):
...     print(len(pickle.dumps(b, proto)))
... 
444
441
204
204
214
214

Testing for non-default protocols was also added.  Let me know if all this does make sense as a PR.

----------
components: Library (Lib)
files: fractions-pickle.diff
keywords: patch
messages: 393781
nosy: Sergey.Kirpichev
priority: normal
severity: normal
status: open
title: Optimize Fraction pickling
versions: Python 3.11
Added file: https://bugs.python.org/file50047/fractions-pickle.diff

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44154>
_______________________________________


More information about the New-bugs-announce mailing list