Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a4dfc8

Browse files
committed
Another ugly inlining hack, expanding the two PyDict_GetItem() calls
in LOAD_GLOBAL. Besides saving a C function call, it saves checks whether f_globals and f_builtins are dicts, and extracting and testing the string object's hash code is done only once. We bail out of the inlining if the name is not exactly a string, or when its hash is -1; because of interning, neither should ever happen. I believe interning guarantees that the hash code is set, and I believe that the 'names' tuple of a code object always contains interned strings, but I'm not assuming that -- I'm simply testing hash != -1. On my home machine, this makes a pystone variant with new-style classes and slots run at the same speed as classic pystone! (With new-style classes but without slots, it is still a lot slower.)
1 parent e3a8e7e commit 3a4dfc8

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

Python/ceval.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1709,13 +1709,37 @@ eval_frame(PyFrameObject *f)
17091709

17101710
case LOAD_GLOBAL:
17111711
w = GETITEM(names, oparg);
1712+
if (PyString_CheckExact(w)) {
1713+
long hash = ((PyStringObject *)w)->ob_shash;
1714+
if (hash != -1) {
1715+
/* Inline the PyDict_GetItem() calls */
1716+
PyDictObject *d;
1717+
d = (PyDictObject *)(f->f_globals);
1718+
x = d->ma_lookup(d, w, hash)->me_value;
1719+
if (x != NULL) {
1720+
Py_INCREF(x);
1721+
PUSH(x);
1722+
continue;
1723+
}
1724+
d = (PyDictObject *)(f->f_builtins);
1725+
x = d->ma_lookup(d, w, hash)->me_value;
1726+
if (x != NULL) {
1727+
Py_INCREF(x);
1728+
PUSH(x);
1729+
continue;
1730+
}
1731+
goto load_global_error;
1732+
}
1733+
}
1734+
/* This is the un-inlined version of the code above */
17121735
x = PyDict_GetItem(f->f_globals, w);
17131736
if (x == NULL) {
17141737
x = PyDict_GetItem(f->f_builtins, w);
17151738
if (x == NULL) {
1739+
load_global_error:
17161740
format_exc_check_arg(
17171741
PyExc_NameError,
1718-
GLOBAL_NAME_ERROR_MSG ,w);
1742+
GLOBAL_NAME_ERROR_MSG, w);
17191743
break;
17201744
}
17211745
}

0 commit comments

Comments
 (0)