[New-bugs-announce] [issue16001] small ints: cache string representation

STINNER Victor report at bugs.python.org
Sat Sep 22 02:11:20 CEST 2012


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 <report at bugs.python.org>
<http://bugs.python.org/issue16001>
_______________________________________


More information about the New-bugs-announce mailing list