avoid creating garbage when iterating over hashmaps

This commit is contained in:
Joel Dice 2008-11-22 16:38:41 -07:00
parent 4392b04fd0
commit fb9b2bf173
4 changed files with 52 additions and 49 deletions

View File

@ -770,12 +770,8 @@ parseInterfaceTable(Thread* t, Stream& s, object class_, object pool)
PROTECT(t, interfaceTable);
unsigned i = 0;
object it = hashMapIterator(t, map);
PROTECT(t, it);
for (; it; it = hashMapIteratorNext(t, it)) {
object interface = resolveClass
(t, tripleFirst(t, hashMapIteratorNode(t, it)));
for (HashMapIterator it(t, map); it.hasMore();) {
object interface = resolveClass(t, tripleFirst(t, it.next()));
if (UNLIKELY(t->exception)) return;
set(t, interfaceTable, ArrayBody + (i * BytesPerWord), interface);
@ -1243,10 +1239,8 @@ parseMethodTable(Thread* t, Stream& s, object class_, object pool)
if (classFlags(t, class_) & ACC_INTERFACE) {
PROTECT(t, vtable);
for (object it = hashMapIterator(t, virtualMap); it;
it = hashMapIteratorNext(t, it))
{
object method = tripleFirst(t, hashMapIteratorNode(t, it));
for (HashMapIterator it(t, virtualMap); it.hasMore();) {
object method = tripleFirst(t, it.next());
assert(t, arrayBody(t, vtable, methodOffset(t, method)) == 0);
set(t, vtable, ArrayBody + (methodOffset(t, method) * BytesPerWord),
method);

View File

@ -73,11 +73,6 @@
(type weakHashMap
(extends hashMap))
(type hashMapIterator
(object map)
(object node)
(unsigned index))
(type list
(uint32_t size)
(object front)

View File

@ -482,40 +482,6 @@ hashMapRemove(Thread* t, object map, object key,
return o;
}
object
hashMapIterator(Thread* t, object map)
{
object array = hashMapArray(t, map);
if (array) {
for (unsigned i = 0; i < arrayLength(t, array); ++i) {
if (arrayBody(t, array, i)) {
return makeHashMapIterator(t, map, arrayBody(t, array, i), i + 1);
}
}
}
return 0;
}
object
hashMapIteratorNext(Thread* t, object it)
{
object map = hashMapIteratorMap(t, it);
object node = hashMapIteratorNode(t, it);
unsigned index = hashMapIteratorIndex(t, it);
if (tripleThird(t, node)) {
return makeHashMapIterator(t, map, tripleThird(t, node), index);
} else {
object array = hashMapArray(t, map);
for (unsigned i = index; i < arrayLength(t, array); ++i) {
if (arrayBody(t, array, i)) {
return makeHashMapIterator(t, map, arrayBody(t, array, i), i + 1);
}
}
return 0;
}
}
void
listAppend(Thread* t, object list, object value)
{

View File

@ -93,6 +93,54 @@ treeInsertNode(Thread* t, Zone* zone, object tree, intptr_t key, object node,
object sentinal,
intptr_t (*compare)(Thread* t, intptr_t key, object b));
class HashMapIterator: public Thread::Protector {
public:
HashMapIterator(Thread* t, object map):
Protector(t), map(map), node(0), index(0)
{
find();
}
void find() {
object array = hashMapArray(t, map);
for (unsigned i = index; i < arrayLength(t, array); ++i) {
if (arrayBody(t, array, i)) {
node = arrayBody(t, array, i);
index = i + 1;
return;
}
}
node = 0;
}
bool hasMore() {
return node != 0;
}
object next() {
if (node) {
object n = node;
if (tripleThird(t, node)) {
node = tripleThird(t, node);
} else {
find();
}
return n;
} else {
return 0;
}
}
virtual void visit(Heap::Visitor* v) {
v->visit(&map);
v->visit(&node);
}
object map;
object node;
unsigned index;
};
} // vm
#endif//UTIL_H