annotate Include/stringobject.h @ 5003:65aace2012e5 legacy-trunk

Two speedup hacks. Caching the hash saves recalculation of a string's hash value. Interning strings (which requires hash caching) tries to ensure that only one string object with a given value exists, so equality tests are one pointer comparison. Together, these can speed the interpreter up by as much as 20%. Each costs the size of a long or pointer per string object. In addition, interned strings live until the end of times. If you are concerned about memory footprint, simply comment the #define out here (and rebuild everything!).
author Guido van Rossum <guido@python.org>
date Sat, 18 Jan 1997 07:53:23 +0000
parents 90c21dde4284
children 60fdb1db1175
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1329
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1 #ifndef Py_STRINGOBJECT_H
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2 #define Py_STRINGOBJECT_H
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
3 #ifdef __cplusplus
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
4 extern "C" {
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
5 #endif
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
6
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
7 /***********************************************************
2058
508036dbba3b Added 1995 copyright.
Guido van Rossum <guido@python.org>
parents: 1794
diff changeset
8 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
508036dbba3b Added 1995 copyright.
Guido van Rossum <guido@python.org>
parents: 1794
diff changeset
9 The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
10
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
11 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
12
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
13 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
14 documentation for any purpose and without fee is hereby granted,
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
15 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
16 both that copyright notice and this permission notice appear in
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
17 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
18 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
19 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
20 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
21 permission.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
22
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
23 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
24 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
25 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
26
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
27 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
28 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
29 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
30 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
31 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
32 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
33 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3833
diff changeset
34 PERFORMANCE OF THIS SOFTWARE.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
35
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
36 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
37
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 /* String object interface */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 /*
2129
05360cd616ae The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents: 2058
diff changeset
41 Type PyStringObject represents a character string. An extra zero byte is
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 reserved at the end to ensure it is zero-terminated, but a size is
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 present so strings with null bytes in them can be represented. This
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 is an immutable object type.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 There are functions to create new string objects, to test
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 an object for string-ness, and to get the
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 string value. The latter function returns a null pointer
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 if the object is not of the proper type.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 There is a variant that takes an explicit size as well as a
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 variant that assumes a zero-terminated string. Note that none of the
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 functions should be applied to nil objects.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
5003
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
55 /* Two speedup hacks. Caching the hash saves recalculation of a
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
56 string's hash value. Interning strings (which requires hash
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
57 caching) tries to ensure that only one string object with a given
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
58 value exists, so equality tests are one pointer comparison.
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
59 Together, these can speed the interpreter up by as much as 20%.
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
60 Each costs the size of a long or pointer per string object. In
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
61 addition, interned strings live until the end of times. If you are
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
62 concerned about memory footprint, simply comment the #define out
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
63 here (and rebuild everything!). */
3833
f0efff28a24d Turn on CACHE_HASH, for 2% speedier dict lookups
Guido van Rossum <guido@python.org>
parents: 2436
diff changeset
64 #define CACHE_HASH
5003
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
65 #ifdef CACHE_HASH
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
66 #define INTERN_STRINGS
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
67 #endif
3833
f0efff28a24d Turn on CACHE_HASH, for 2% speedier dict lookups
Guido van Rossum <guido@python.org>
parents: 2436
diff changeset
68
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 typedef struct {
2129
05360cd616ae The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents: 2058
diff changeset
70 PyObject_VAR_HEAD
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1329
diff changeset
71 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1329
diff changeset
72 long ob_shash;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1329
diff changeset
73 #endif
5003
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
74 #ifdef INTERN_STRINGS
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
75 PyObject *ob_sinterned;
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
76 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 char ob_sval[1];
2129
05360cd616ae The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents: 2058
diff changeset
78 } PyStringObject;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
2436
fa98dda07877 make the type a parameter of the DL_IMPORT macro, for Borland C
Guido van Rossum <guido@python.org>
parents: 2146
diff changeset
80 extern DL_IMPORT(PyTypeObject) PyString_Type;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
2129
05360cd616ae The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents: 2058
diff changeset
82 #define PyString_Check(op) ((op)->ob_type == &PyString_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
4654
df81856db58c Add const to error and newstring functions
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
84 extern PyObject *PyString_FromStringAndSize Py_PROTO((const char *, int));
df81856db58c Add const to error and newstring functions
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
85 extern PyObject *PyString_FromString Py_PROTO((const char *));
2129
05360cd616ae The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents: 2058
diff changeset
86 extern int PyString_Size Py_PROTO((PyObject *));
05360cd616ae The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents: 2058
diff changeset
87 extern char *PyString_AsString Py_PROTO((PyObject *));
05360cd616ae The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents: 2058
diff changeset
88 extern void PyString_Concat Py_PROTO((PyObject **, PyObject *));
2146
9afdbf944c2a new names for lots of new functions
Guido van Rossum <guido@python.org>
parents: 2129
diff changeset
89 extern void PyString_ConcatAndDel Py_PROTO((PyObject **, PyObject *));
2129
05360cd616ae The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents: 2058
diff changeset
90 extern int _PyString_Resize Py_PROTO((PyObject **, int));
05360cd616ae The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents: 2058
diff changeset
91 extern PyObject *PyString_Format Py_PROTO((PyObject *, PyObject *));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
5003
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
93 #ifdef INTERN_STRINGS
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
94 extern void PyString_InternInPlace Py_PROTO((PyObject **));
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
95 extern PyObject *PyString_InternFromString Py_PROTO((const char *));
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
96 #else
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
97 #define PyString_InternInPlace(p)
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
98 #define PyString_InternFromString(cp) PyString_FromString(cp)
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
99 #endif
65aace2012e5 Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents: 4897
diff changeset
100
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 /* Macro, trading safety for speed */
4897
90c21dde4284 added PyString_GET_SIZE macro
Barry Warsaw <barry@python.org>
parents: 4654
diff changeset
102 #define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
90c21dde4284 added PyString_GET_SIZE macro
Barry Warsaw <barry@python.org>
parents: 4654
diff changeset
103 #define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
1329
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
104
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
105 #ifdef __cplusplus
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
106 }
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
107 #endif
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
108 #endif /* !Py_STRINGOBJECT_H */