Message170938
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. |
|
Date |
User |
Action |
Args |
2012-09-22 00:11:20 | vstinner | set | recipients:
+ vstinner, pitrou, ezio.melotti |
2012-09-22 00:11:20 | vstinner | set | messageid: <1348272680.09.0.237238464287.issue16001@psf.upfronthosting.co.za> |
2012-09-22 00:11:19 | vstinner | link | issue16001 messages |
2012-09-22 00:11:18 | vstinner | create | |
|