New submission from Serhiy Storchaka: One of purposes of the STACK_GLOBAL opcode introduced in pickle protocol 4 is to avoid repeating module name for different globals of the same module.
>>> pickletools.dis(pickletools.optimize(pickle.dumps([sys.getsizeof, >>> sys.intern], 4))) 0: \x80 PROTO 4 2: \x95 FRAME 33 11: ] EMPTY_LIST 12: ( MARK 13: \x8c SHORT_BINUNICODE 'sys' 18: \x94 MEMOIZE (as 0) 19: \x8c SHORT_BINUNICODE 'getsizeof' 30: \x93 STACK_GLOBAL 31: h BINGET 0 33: \x8c SHORT_BINUNICODE 'intern' 41: \x93 STACK_GLOBAL 42: e APPENDS (MARK at 12) 43: . STOP highest protocol among opcodes = 4 But this doesn't work with the itertools module. >>> pickletools.dis(pickletools.optimize(pickle.dumps([itertools.chain, >>> itertools.accumulate], 4))) 0: \x80 PROTO 4 2: \x95 FRAME 47 11: ] EMPTY_LIST 12: ( MARK 13: \x8c SHORT_BINUNICODE 'itertools' 24: \x8c SHORT_BINUNICODE 'chain' 31: \x93 STACK_GLOBAL 32: \x8c SHORT_BINUNICODE 'itertools' 43: \x8c SHORT_BINUNICODE 'accumulate' 55: \x93 STACK_GLOBAL 56: e APPENDS (MARK at 12) 57: . STOP highest protocol among opcodes = 4 That is because the __module__ attribute of itertools members is not interned. >>> sys.getsizeof.__module__ is sys.intern.__module__ True >>> itertools.chain.__module__ is itertools.chain.__module__ False In addition to inefficient pickle this perhaps leads to small performance hit on accessing the __module__ attribute or using its value as dictionary key. ---------- components: Extension Modules messages: 256322 nosy: alexandre.vassalotti, pitrou, rhettinger, serhiy.storchaka priority: normal severity: normal status: open title: The __module__ attribute of itertools members is not interned type: performance versions: Python 3.6 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue25856> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com