summaryrefslogtreecommitdiffstats
path: root/Objects/intobject.c
diff options
context:
space:
mode:
authorSjoerd Mullender <sjoerd@acm.org>1993-10-15 16:18:48 (GMT)
committerSjoerd Mullender <sjoerd@acm.org>1993-10-15 16:18:48 (GMT)
commit842d2ccdcd540399501a918b9724d2eaf5599f39 (patch)
treea62399047b129d4fe4f5db57a925fa7f135cabd2 /Objects/intobject.c
parent21d335ed9ef6fbb16341809fe224b45a3f41243f (diff)
downloadcpython-842d2ccdcd540399501a918b9724d2eaf5599f39.zip
cpython-842d2ccdcd540399501a918b9724d2eaf5599f39.tar.gz
cpython-842d2ccdcd540399501a918b9724d2eaf5599f39.tar.bz2
intobject.c: Save references to small integers, so that they can be
shared. The default is to save references to the integers in the range -1..99. The lower limit can be set by defining NSMALLNEGINTS (absolute value of smallest integer to be saved) and NSMALLPOSINTS (1 more than the largest integer to be saved). tupleobject.c: Save a reference to the empty tuple to be returned whenever a tuple of size 0 is requested. Tuples of size 1 upto, but not including, MAXSAVESIZE (default 20) are put in free lists when deallocated. When MAXSAVESIZE equals 1, only share references to the empty tuple, when MAXSAVESIZE equals 0, don't include the code at all and revert to the old behavior. object.c: Print some more statistics when COUNT_ALLOCS is defined.
Diffstat (limited to 'Objects/intobject.c')
-rw-r--r--Objects/intobject.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c
index 816a411..6fcbe69 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -75,12 +75,42 @@ fill_free_list()
}
static intobject *free_list = NULL;
+#ifndef NSMALLPOSINTS
+#define NSMALLPOSINTS 100
+#endif
+#ifndef NSMALLNEGINTS
+#define NSMALLNEGINTS 1
+#endif
+#if NSMALLNEGINTS + NSMALLPOSINTS > 0
+/* References to small integers are saved in this array so that they
+ can be shared.
+ The integers that are saved are those in the range
+ -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
+*/
+static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
+#endif
+#ifdef COUNT_ALLOCS
+int quick_int_allocs, quick_neg_int_allocs;
+#endif
object *
newintobject(ival)
long ival;
{
register intobject *v;
+#if NSMALLNEGINTS + NSMALLPOSINTS > 0
+ if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
+ (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
+ INCREF(v);
+#ifdef COUNT_ALLOCS
+ if (ival >= 0)
+ quick_int_allocs++;
+ else
+ quick_neg_int_allocs++;
+#endif
+ return (object *) v;
+ }
+#endif
if (free_list == NULL) {
if ((free_list = fill_free_list()) == NULL)
return NULL;
@@ -90,6 +120,13 @@ newintobject(ival)
v->ob_type = &Inttype;
v->ob_ival = ival;
NEWREF(v);
+#if NSMALLNEGINTS + NSMALLPOSINTS > 0
+ if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
+ /* save this one for a following allocation */
+ INCREF(v);
+ small_ints[ival + NSMALLNEGINTS] = v;
+ }
+#endif
return (object *) v;
}