New submission from STINNER Victor: Integers in range [-5; 256] are singleton. It is possible to cache their string representation (in base 10) to make str(int) faster, but also to reduce memory consumption (and memory fragmentation, Unicode strings don't use free list).
Attached patch implements this micro-optimization. It reuses singleton for latin1 characters (so digits 0..9): str(0) is chr(48). - /* Special-case boolean: we want 0/1 */ - if (PyBool_Check(val)) - result = PyNumber_ToBase(val, 10); - else - result = Py_TYPE(val)->tp_str(val); + result = PyNumber_ToBase(val, 10); This change is required because Py_TYPE(val)->tp_str(val); may return a singleton, whereas formatlong() requires a "mutable" string (string not used yet). See also issue #10044. ---------- components: Unicode files: small_ints_cache_str.patch keywords: patch messages: 170938 nosy: ezio.melotti, haypo, pitrou priority: normal severity: normal status: open title: small ints: cache string representation type: performance versions: Python 3.4 Added file: http://bugs.python.org/file27254/small_ints_cache_str.patch _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue16001> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com