Kaydet (Commit) c91ed400 authored tarafından Neal Norwitz's avatar Neal Norwitz

SF #561244, Micro optimizations

Initialize the small integers and __builtins__ in startup.
This removes some if conditions.
Change XDECREF to DECREF for values which shouldn't be NULL.
üst 83f898c8
...@@ -30,6 +30,7 @@ PyAPI_DATA(PyTypeObject) PyInt_Type; ...@@ -30,6 +30,7 @@ PyAPI_DATA(PyTypeObject) PyInt_Type;
#define PyInt_Check(op) PyObject_TypeCheck(op, &PyInt_Type) #define PyInt_Check(op) PyObject_TypeCheck(op, &PyInt_Type)
#define PyInt_CheckExact(op) ((op)->ob_type == &PyInt_Type) #define PyInt_CheckExact(op) ((op)->ob_type == &PyInt_Type)
PyAPI_FUNC(int) PyInt_Init(void);
PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int); PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int);
#ifdef Py_USING_UNICODE #ifdef Py_USING_UNICODE
PyAPI_FUNC(PyObject *) PyInt_FromUnicode(Py_UNICODE*, int, int); PyAPI_FUNC(PyObject *) PyInt_FromUnicode(Py_UNICODE*, int, int);
......
...@@ -100,6 +100,7 @@ PyAPI_FUNC(PyObject *) _PySys_Init(void); ...@@ -100,6 +100,7 @@ PyAPI_FUNC(PyObject *) _PySys_Init(void);
PyAPI_FUNC(void) _PyImport_Init(void); PyAPI_FUNC(void) _PyImport_Init(void);
PyAPI_FUNC(void) _PyExc_Init(void); PyAPI_FUNC(void) _PyExc_Init(void);
PyAPI_FUNC(void) _PyImportHooks_Init(void); PyAPI_FUNC(void) _PyImportHooks_Init(void);
PyAPI_FUNC(int) PyFrame_Init(void);
/* Various internal finalizers */ /* Various internal finalizers */
PyAPI_FUNC(void) _PyExc_Fini(void); PyAPI_FUNC(void) _PyExc_Fini(void);
......
...@@ -401,9 +401,9 @@ frame_dealloc(PyFrameObject *f) ...@@ -401,9 +401,9 @@ frame_dealloc(PyFrameObject *f)
} }
Py_XDECREF(f->f_back); Py_XDECREF(f->f_back);
Py_XDECREF(f->f_code); Py_DECREF(f->f_code);
Py_XDECREF(f->f_builtins); Py_DECREF(f->f_builtins);
Py_XDECREF(f->f_globals); Py_DECREF(f->f_globals);
Py_XDECREF(f->f_locals); Py_XDECREF(f->f_locals);
Py_XDECREF(f->f_trace); Py_XDECREF(f->f_trace);
Py_XDECREF(f->f_exc_type); Py_XDECREF(f->f_exc_type);
...@@ -525,21 +525,23 @@ PyTypeObject PyFrame_Type = { ...@@ -525,21 +525,23 @@ PyTypeObject PyFrame_Type = {
0, /* tp_dict */ 0, /* tp_dict */
}; };
static PyObject *builtin_object;
int PyFrame_Init()
{
builtin_object = PyString_InternFromString("__builtins__");
return (builtin_object != NULL);
}
PyFrameObject * PyFrameObject *
PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals, PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
PyObject *locals) PyObject *locals)
{ {
PyFrameObject *back = tstate->frame; PyFrameObject *back = tstate->frame;
static PyObject *builtin_object;
PyFrameObject *f; PyFrameObject *f;
PyObject *builtins; PyObject *builtins;
int extras, ncells, nfrees; int extras, ncells, nfrees;
if (builtin_object == NULL) {
builtin_object = PyString_InternFromString("__builtins__");
if (builtin_object == NULL)
return NULL;
}
#ifdef Py_DEBUG #ifdef Py_DEBUG
if (code == NULL || globals == NULL || !PyDict_Check(globals) || if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
(locals != NULL && !PyDict_Check(locals))) { (locals != NULL && !PyDict_Check(locals))) {
...@@ -802,4 +804,6 @@ PyFrame_Fini(void) ...@@ -802,4 +804,6 @@ PyFrame_Fini(void)
--numfree; --numfree;
} }
assert(numfree == 0); assert(numfree == 0);
Py_XDECREF(builtin_object);
builtin_object = NULL;
} }
...@@ -78,7 +78,7 @@ fill_free_list(void) ...@@ -78,7 +78,7 @@ fill_free_list(void)
#define NSMALLPOSINTS 100 #define NSMALLPOSINTS 100
#endif #endif
#ifndef NSMALLNEGINTS #ifndef NSMALLNEGINTS
#define NSMALLNEGINTS 1 #define NSMALLNEGINTS 5
#endif #endif
#if NSMALLNEGINTS + NSMALLPOSINTS > 0 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
/* References to small integers are saved in this array so that they /* References to small integers are saved in this array so that they
...@@ -97,8 +97,8 @@ PyInt_FromLong(long ival) ...@@ -97,8 +97,8 @@ PyInt_FromLong(long ival)
{ {
register PyIntObject *v; register PyIntObject *v;
#if NSMALLNEGINTS + NSMALLPOSINTS > 0 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS && if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
(v = small_ints[ival + NSMALLNEGINTS]) != NULL) { v = small_ints[ival + NSMALLNEGINTS];
Py_INCREF(v); Py_INCREF(v);
#ifdef COUNT_ALLOCS #ifdef COUNT_ALLOCS
if (ival >= 0) if (ival >= 0)
...@@ -118,13 +118,6 @@ PyInt_FromLong(long ival) ...@@ -118,13 +118,6 @@ PyInt_FromLong(long ival)
free_list = (PyIntObject *)v->ob_type; free_list = (PyIntObject *)v->ob_type;
PyObject_INIT(v, &PyInt_Type); PyObject_INIT(v, &PyInt_Type);
v->ob_ival = ival; v->ob_ival = ival;
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
/* save this one for a following allocation */
Py_INCREF(v);
small_ints[ival + NSMALLNEGINTS] = v;
}
#endif
return (PyObject *) v; return (PyObject *) v;
} }
...@@ -945,6 +938,26 @@ PyTypeObject PyInt_Type = { ...@@ -945,6 +938,26 @@ PyTypeObject PyInt_Type = {
(freefunc)int_free, /* tp_free */ (freefunc)int_free, /* tp_free */
}; };
int
PyInt_Init(void)
{
PyIntObject *v;
int ival;
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
if ((free_list = fill_free_list()) == NULL)
return 0;
/* PyObject_New is inlined */
v = free_list;
free_list = (PyIntObject *)v->ob_type;
PyObject_INIT(v, &PyInt_Type);
v->ob_ival = ival;
small_ints[ival + NSMALLNEGINTS] = v;
}
#endif
return 1;
}
void void
PyInt_Fini(void) PyInt_Fini(void)
{ {
......
...@@ -124,6 +124,12 @@ Py_Initialize(void) ...@@ -124,6 +124,12 @@ Py_Initialize(void)
_Py_ReadyTypes(); _Py_ReadyTypes();
if (!PyFrame_Init())
Py_FatalError("Py_Initialize: can't init frames");
if (!PyInt_Init())
Py_FatalError("Py_Initialize: can't init ints");
interp->modules = PyDict_New(); interp->modules = PyDict_New();
if (interp->modules == NULL) if (interp->modules == NULL)
Py_FatalError("Py_Initialize: can't make modules dictionary"); Py_FatalError("Py_Initialize: can't make modules dictionary");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment