Commit bc3e118d authored by Boxiang Sun's avatar Boxiang Sun
parent bd39c49a
......@@ -131,15 +131,15 @@ const int BoxedList::INITIAL_CAPACITY = 8;
// TODO the inliner doesn't want to inline these; is there any point to having them in the inline section?
void BoxedList::shrink() {
// TODO more attention to the shrink condition to avoid frequent shrink and alloc
if (capacity > size * 3) {
int new_capacity = std::max(static_cast<int64_t>(INITIAL_CAPACITY), capacity / 2);
if (allocated > size * 3) {
int new_allocated = std::max(static_cast<int64_t>(INITIAL_CAPACITY), allocated / 2);
if (size > 0) {
elts = GCdArray::grow(elts, new_capacity);
capacity = new_capacity;
elts = GCdArray::grow(elts, new_allocated);
allocated = new_allocated;
} else if (size == 0) {
delete elts;
elts = NULL;
capacity = 0;
allocated = 0;
}
}
}
......@@ -149,14 +149,14 @@ extern "C" void listAppendArrayInternal(Box* s, Box** v, int nelts) {
assert(PyList_Check(s));
BoxedList* self = static_cast<BoxedList*>(s);
assert(self->size <= self->capacity);
assert(self->size <= self->allocated);
self->ensure(nelts);
for (int i = 0; i < nelts; i++) {
Py_INCREF(v[i]);
}
assert(self->size <= self->capacity);
assert(self->size <= self->allocated);
memcpy(&self->elts->elts[self->size], &v[0], nelts * sizeof(Box*));
self->size += nelts;
......
......@@ -22,23 +22,23 @@ namespace pyston {
// TODO the inliner doesn't want to inline these; is there any point to having them in the inline section?
inline void BoxedList::grow(int min_free) {
if (capacity == 0) {
if (allocated == 0) {
const int INITIAL_CAPACITY = 8;
int initial = std::max(INITIAL_CAPACITY, min_free);
elts = new (initial) GCdArray();
capacity = initial;
allocated = initial;
} else {
int new_capacity = std::max(capacity * 2, size + min_free);
elts = GCdArray::grow(elts, new_capacity);
capacity = new_capacity;
int new_allocated = std::max(allocated * 2, size + min_free);
elts = GCdArray::grow(elts, new_allocated);
allocated = new_allocated;
}
}
inline void BoxedList::ensure(int min_free) {
if (unlikely(size + min_free > capacity)) {
if (unlikely(size + min_free > allocated)) {
grow(min_free);
}
assert(capacity >= size + min_free);
assert(allocated >= size + min_free);
}
// TODO the inliner doesn't want to inline these; is there any point to having them in the inline section?
......@@ -48,10 +48,10 @@ extern "C" inline void listAppendInternalStolen(Box* s, Box* v) {
assert(PyList_Check(s));
BoxedList* self = static_cast<BoxedList*>(s);
assert(self->size <= self->capacity);
assert(self->size <= self->allocated);
self->ensure(1);
assert(self->size < self->capacity);
assert(self->size < self->allocated);
self->elts->elts[self->size] = v;
self->size++;
}
......
......@@ -1426,7 +1426,7 @@ int BoxedList::clear(Box* _a) noexcept {
this list, we make it empty first. */
i = Py_SIZE(a);
Py_SIZE(a) = 0;
a->capacity = 0;
a->allocated = 0;
auto old_elts = a->elts;
a->elts = NULL;
while (--i >= 0) {
......
......@@ -718,9 +718,9 @@ private:
public:
Py_ssize_t size;
GCdArray* elts;
Py_ssize_t capacity;
Py_ssize_t allocated;
BoxedList() __attribute__((visibility("default"))) : size(0), elts(NULL), capacity(0) {}
BoxedList() __attribute__((visibility("default"))) : size(0), elts(NULL), allocated(0) {}
void ensure(int min_free);
void shrink();
......@@ -737,7 +737,7 @@ static_assert(sizeof(BoxedList) >= sizeof(PyListObject), "");
static_assert(offsetof(BoxedList, size) == offsetof(PyListObject, ob_size), "");
static_assert(offsetof(BoxedList, elts) == offsetof(PyListObject, ob_item), "");
static_assert(offsetof(GCdArray, elts) == 0, "");
static_assert(offsetof(BoxedList, capacity) == offsetof(PyListObject, allocated), "");
static_assert(offsetof(BoxedList, allocated) == offsetof(PyListObject, allocated), "");
#define PyTuple_MAXSAVESIZE 20 /* Largest tuple to save on free list */
#define PyTuple_MAXFREELIST 2000 /* Maximum number of tuples of each size to save */
......
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