This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients ezio.melotti, pitrou, vstinner
Date 2012-09-22.00:11:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1348272680.09.0.237238464287.issue16001@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2012-09-22 00:11:20vstinnersetrecipients: + vstinner, pitrou, ezio.melotti
2012-09-22 00:11:20vstinnersetmessageid: <1348272680.09.0.237238464287.issue16001@psf.upfronthosting.co.za>
2012-09-22 00:11:19vstinnerlinkissue16001 messages
2012-09-22 00:11:18vstinnercreate