summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1991-04-04 10:48:33 (GMT)
committerGuido van Rossum <guido@python.org>1991-04-04 10:48:33 (GMT)
commitdaa8bb334d77f6d25a4b6dfb1c02f11423011e50 (patch)
tree586f882e9f6bd39ae6ab25cb0eb2b011df61605b /Objects
parente6c67a72637a9cb9166d702b02cff39877ac19bb (diff)
downloadcpython-daa8bb334d77f6d25a4b6dfb1c02f11423011e50.zip
cpython-daa8bb334d77f6d25a4b6dfb1c02f11423011e50.tar.gz
cpython-daa8bb334d77f6d25a4b6dfb1c02f11423011e50.tar.bz2
Optimized single-character strings gotten from s[i].
Diffstat (limited to 'Objects')
-rw-r--r--Objects/stringobject.c36
1 files changed, 23 insertions, 13 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 9b11f92..68f5c3a 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -241,28 +241,38 @@ stringslice(a, i, j)
return newsizedstringobject(a->ob_sval + i, (int) (j-i));
}
+#ifdef __STDC__
+#include <limits.h>
+#else
+#ifndef UCHAR_MAX
+#define UCHAR_MAX 255
+#endif
+#endif
+
+static object *characters[UCHAR_MAX + 1];
+
static object *
stringitem(a, i)
stringobject *a;
register int i;
{
- /* This is optimized since this is a common operation! */
-
- register stringobject *op;
+ int c;
+ object *v;
if (i < 0 || i >= a->ob_size) {
err_setstr(IndexError, "string index out of range");
return NULL;
}
- op = (stringobject *)
- malloc(sizeof(stringobject) + sizeof(char));
- if (op == NULL)
- return err_nomem();
- NEWREF(op);
- op->ob_type = &Stringtype;
- op->ob_size = 1;
- op->ob_sval[0] = a->ob_sval[i];
- op->ob_sval[1] = '\0';
- return (object *) op;
+ c = a->ob_sval[i] & UCHAR_MAX;
+ v = characters[c];
+ if (v == NULL) {
+ v = newsizedstringobject((char *)NULL, 1);
+ if (v == NULL)
+ return NULL;
+ characters[c] = v;
+ ((stringobject *)v)->ob_sval[0] = c;
+ }
+ INCREF(v);
+ return v;
}
static int