Message235600
Proposed patch makes faster default __reduce__ implementation for the case when there is no non-trivial __init__ defined (e.g. for named tuples). In this case __reduce__ will return (cls, newargs) instead of (copyreg.__newobj__, (cls,) + newargs).
>>> pickletools.dis(pickletools.optimize(pickle.dumps(turtle.Vec2D(12, 34), 3)))
Before:
0: \x80 PROTO 3
2: c GLOBAL 'turtle Vec2D'
16: K BININT1 12
18: K BININT1 34
20: \x86 TUPLE2
21: \x81 NEWOBJ
22: . STOP
After:
0: \x80 PROTO 3
2: c GLOBAL 'turtle Vec2D'
16: K BININT1 12
18: K BININT1 34
20: \x86 TUPLE2
21: R REDUCE
22: . STOP
Pickled size is the same, but pickling is faster. The benefit is in avoiding of importing copyreg.__newobj__ and allocating new tuple (cls,) + newargs.
Microbenchmarks results:
$ ./python -m timeit -s "import pickle; from turtle import Vec2D; a = [Vec2D(i, i+0.1) for i in range(1000)]" -- "pickle.dumps(a)"
Before: 100 loops, best of 3: 16.3 msec per loop
After: 100 loops, best of 3: 15.2 msec per loop
$ ./python -m timeit -s "import copy; from turtle import Vec2D; a = [Vec2D(i, i+0.1) for i in range(1000)]" -- "copy.deepcopy(a)"
Before: 10 loops, best of 3: 96.6 msec per loop
After: 10 loops, best of 3: 88.7 msec per loop |
|
Date |
User |
Action |
Args |
2015-02-09 11:00:44 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, pitrou, alexandre.vassalotti |
2015-02-09 11:00:44 | serhiy.storchaka | set | messageid: <1423479644.09.0.947345755494.issue23419@psf.upfronthosting.co.za> |
2015-02-09 11:00:43 | serhiy.storchaka | link | issue23419 messages |
2015-02-09 11:00:43 | serhiy.storchaka | create | |
|