New submission from Sergey B Kirpichev <skirpic...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue44154>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to