Commit bcae726c authored by Kevin Modzelewski's avatar Kevin Modzelewski

Change freelist strategy: linked list instead of array

parent c454c3be
...@@ -1123,8 +1123,12 @@ template Box* Box::getattr<NOT_REWRITABLE>(BoxedString*, GetattrRewriteArgs*); ...@@ -1123,8 +1123,12 @@ template Box* Box::getattr<NOT_REWRITABLE>(BoxedString*, GetattrRewriteArgs*);
#define ARRAYLIST_FREELIST_SIZE 100 #define ARRAYLIST_FREELIST_SIZE 100
#define ARRAYLIST_NUM_FREELISTS 4 #define ARRAYLIST_NUM_FREELISTS 4
#define MAX_FREELIST_SIZE (INITIAL_ARRAY_SIZE * (1 << (ARRAYLIST_NUM_FREELISTS - 1))) #define MAX_FREELIST_SIZE (INITIAL_ARRAY_SIZE * (1 << (ARRAYLIST_NUM_FREELISTS - 1)))
HCAttrs::AttrList* attrlist_freelist[ARRAYLIST_NUM_FREELISTS][ARRAYLIST_FREELIST_SIZE]; struct Freelist {
int attrlist_freelist_size[ARRAYLIST_NUM_FREELISTS]; int size;
HCAttrs::AttrList* next_free;
};
Freelist attrlist_freelist[ARRAYLIST_NUM_FREELISTS];
int freelist_index[] int freelist_index[]
= { 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }; = { 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
static_assert(sizeof(freelist_index) / sizeof(freelist_index[0]) == MAX_FREELIST_SIZE + 1, ""); static_assert(sizeof(freelist_index) / sizeof(freelist_index[0]) == MAX_FREELIST_SIZE + 1, "");
...@@ -1147,15 +1151,16 @@ static int freelistIndex(int n) { ...@@ -1147,15 +1151,16 @@ static int freelistIndex(int n) {
} }
static HCAttrs::AttrList* allocFromFreelist(int freelist_idx) { static HCAttrs::AttrList* allocFromFreelist(int freelist_idx) {
int size = attrlist_freelist_size[freelist_idx]; auto&& freelist = attrlist_freelist[freelist_idx];
int size = freelist.size;
if (size) { if (size) {
auto rtn = attrlist_freelist[freelist_idx][size - 1]; auto rtn = freelist.next_free;
attrlist_freelist_size[freelist_idx] = size - 1; freelist.size = size - 1;
freelist.next_free = *reinterpret_cast<HCAttrs::AttrList**>(rtn);
#ifndef NDEBUG #ifndef NDEBUG
int nattrs = (1 << freelist_idx) * INITIAL_ARRAY_SIZE; int nattrs = (1 << freelist_idx) * INITIAL_ARRAY_SIZE;
memset(rtn, 0xcb, sizeof(HCAttrs::AttrList) + nattrs * sizeof(Box*)); memset(rtn, 0xcb, sizeof(HCAttrs::AttrList) + nattrs * sizeof(Box*));
attrlist_freelist[freelist_idx][size - 1] = NULL;
#endif #endif
return rtn; return rtn;
} }
...@@ -1176,7 +1181,8 @@ static HCAttrs::AttrList* allocAttrs(int nattrs) { ...@@ -1176,7 +1181,8 @@ static HCAttrs::AttrList* allocAttrs(int nattrs) {
static void freeAttrs(HCAttrs::AttrList* attrs, int nattrs) { static void freeAttrs(HCAttrs::AttrList* attrs, int nattrs) {
if (nattrs <= MAX_FREELIST_SIZE) { if (nattrs <= MAX_FREELIST_SIZE) {
int idx = freelistIndex(nattrs); int idx = freelistIndex(nattrs);
int size = attrlist_freelist_size[idx]; auto&& freelist = attrlist_freelist[idx];
int size = freelist.size;
// TODO: should drop an old item from the freelist, not a new one // TODO: should drop an old item from the freelist, not a new one
if (size == ARRAYLIST_FREELIST_SIZE) { if (size == ARRAYLIST_FREELIST_SIZE) {
...@@ -1185,8 +1191,9 @@ static void freeAttrs(HCAttrs::AttrList* attrs, int nattrs) { ...@@ -1185,8 +1191,9 @@ static void freeAttrs(HCAttrs::AttrList* attrs, int nattrs) {
#ifndef NDEBUG #ifndef NDEBUG
memset(attrs, 0xdb, sizeof(HCAttrs::AttrList) + nattrs * sizeof(Box*)); memset(attrs, 0xdb, sizeof(HCAttrs::AttrList) + nattrs * sizeof(Box*));
#endif #endif
attrlist_freelist[idx][size] = attrs; *reinterpret_cast<HCAttrs::AttrList**>(attrs) = freelist.next_free;
attrlist_freelist_size[idx]++; freelist.next_free = attrs;
freelist.size++;
return; return;
} }
} }
......
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