Commit 4f83635c authored by Kevin Modzelewski's avatar Kevin Modzelewski

Extract the logic from DEFAULT_CLASS_SIMPLE

so that it can be used in more places.  Looking at pyxl_bench2_10x,
most of the calls to PyType_GenericAlloc are from the dict and list
iterator classes, which don't use DEFAULT_CLASS_SIMPLE (or DEFAULT_CLASS)
since they use the same C++ class with multiple Python classes.  So
by extracting out the core of that function, it's now usable by those
other classes.

Also convert some things to DEFAULT_CLASS_SIMPLE while we're at it.
parent 1d1236b2
This diff is collapsed.
......@@ -26,7 +26,7 @@ BoxedDictIterator::BoxedDictIterator(BoxedDict* d) : d(d), it(d->d.begin()), itE
Box* dict_iter(Box* s) noexcept {
assert(PyDict_Check(s));
BoxedDict* self = static_cast<BoxedDict*>(s);
return new (&PyDictIterKey_Type) BoxedDictIterator(self);
return new (&PyDictIterKey_Type, FAST_GC) BoxedDictIterator(self);
}
Box* dictIterKeys(Box* s) {
......@@ -36,13 +36,13 @@ Box* dictIterKeys(Box* s) {
Box* dictIterValues(Box* s) {
assert(PyDict_Check(s));
BoxedDict* self = static_cast<BoxedDict*>(s);
return new (&PyDictIterValue_Type) BoxedDictIterator(self);
return new (&PyDictIterValue_Type, FAST_GC) BoxedDictIterator(self);
}
Box* dictIterItems(Box* s) {
assert(PyDict_Check(s));
BoxedDict* self = static_cast<BoxedDict*>(s);
return new (&PyDictIterItem_Type) BoxedDictIterator(self);
return new (&PyDictIterItem_Type, FAST_GC) BoxedDictIterator(self);
}
Box* dictIterIter(Box* s) {
......
......@@ -33,7 +33,7 @@ Box* listIterIter(Box* s) {
Box* listIter(Box* s) noexcept {
assert(PyList_Check(s));
BoxedList* self = static_cast<BoxedList*>(s);
return new BoxedListIterator(self, 0);
return new (list_iterator_cls, FAST_GC) BoxedListIterator(self, 0);
}
Box* listiterHasnext(Box* s) {
......@@ -89,7 +89,7 @@ Box* listiter_next(Box* s) noexcept {
Box* listReversed(Box* s) {
assert(PyList_Check(s));
BoxedList* self = static_cast<BoxedList*>(s);
return new (list_reverse_iterator_cls) BoxedListIterator(self, self->size - 1);
return new (list_reverse_iterator_cls, FAST_GC) BoxedListIterator(self, self->size - 1);
}
Box* listreviterHasnext(Box* s) {
......
......@@ -28,8 +28,6 @@ public:
int pos;
BoxedListIterator(BoxedList* l, int start);
DEFAULT_CLASS(list_iterator_cls);
static void dealloc(BoxedListIterator* o) noexcept {
PyObject_GC_UnTrack(o);
Py_XDECREF(o->l);
......
......@@ -33,7 +33,7 @@ public:
BoxedLong() __attribute__((visibility("default"))) {}
DEFAULT_CLASS(long_cls);
DEFAULT_CLASS_SIMPLE(long_cls, false);
};
extern "C" Box* createLong(llvm::StringRef s);
......
......@@ -2214,7 +2214,7 @@ public:
b = NULL;
}
DEFAULT_CLASS(attrwrapper_cls);
DEFAULT_CLASS_SIMPLE(attrwrapper_cls, true);
BORROWED(Box*) getUnderlying() {
......
......@@ -281,6 +281,42 @@ protected:
friend void setupThread();
};
template <bool is_gc> void* Box::newFast(size_t size, BoxedClass* cls) {
ALLOC_STATS(cls);
assert(cls->tp_alloc == PyType_GenericAlloc);
assert(cls->tp_itemsize == 0);
assert(cls->tp_basicsize == size);
assert(cls->is_pyston_class);
assert(cls->attrs_offset == 0);
assert(is_gc == PyType_IS_GC(cls));
bool is_heaptype = false;
assert(is_heaptype == (bool)(cls->tp_flags & Py_TPFLAGS_HEAPTYPE));
/* Don't allocate classes through this -- we need to keep track of all class objects. */
assert(cls != type_cls);
/* note: we want to use size instead of tp_basicsize, since size is a compile-time constant */
void* mem;
if (is_gc)
mem = _PyObject_GC_Malloc(size);
else
mem = PyObject_MALLOC(size);
assert(mem);
Box* rtn = static_cast<Box*>(mem);
if (is_heaptype)
Py_INCREF(cls);
PyObject_INIT(rtn, cls);
if (is_gc)
_PyObject_GC_TRACK(rtn);
return rtn;
/* TODO: there should be a way to not have to do this nested inlining by hand */
}
// Corresponds to PyHeapTypeObject. Very similar to BoxedClass, but allocates some extra space for
// structures that otherwise might get allocated statically. For instance, tp_as_number for builtin
// types will usually point to a `static PyNumberMethods` object, but for a heap-allocated class it
......
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