JS speed - Konq vs. Mozilla

Koos Vriezen koos.vriezen at xs4all.nl
Tue Sep 3 23:23:15 BST 2002


On Tue, 3 Sep 2002, Koos Vriezen wrote:

Hi,

Attached a little bigger patch that makes KJS::Collector::allocate a
bit faster and less memory hungry.
Now, it removes this loop
  while (*r)
    r++;
by remembering where it stopped the last time and swapping all free slots
at the end of a block.
This patch also sets the currentBlock to the first block that contains
free space. I've tested this on a page that creates a lot of instances of
function objects. The number of allocated blocks is reduced from 344 to 80
after loading.

> I think there is more to gain here, eg. larger blocks (now that scanning
> blocks goes faster), already delete blocks in '2nd step: free memory'
> step, smarter currentBlock setting after deleting the last block, keep
> some empty blocks in reserve, defragment empty slots. I'll try some tests
> on these.

Did the 'smarter currentBlock setting' and 'defragment empty slots' now.
Defragmenting could be improved by merging not fully filled blocks.

Regards,

Koos

-------------- next part --------------
Index: collector.cpp
===================================================================
RCS file: /home/kde/kdelibs/kjs/collector.cpp,v
retrieving revision 1.33
diff -u -3 -p -r1.33 collector.cpp
--- collector.cpp	2002/08/22 13:37:51	1.33
+++ collector.cpp	2002/09/03 22:06:52
@@ -38,6 +38,7 @@ namespace KJS {
     ~CollectorBlock();
     int size;
     int filled;
+    int firstfree;
     ValueImp** mem;
     CollectorBlock *prev, *next;
   };
@@ -49,6 +50,7 @@ using namespace KJS;
 CollectorBlock::CollectorBlock(int s)
   : size(s),
     filled(0),
+    firstfree(0),
     prev(0L),
     next(0L)
 {
@@ -134,11 +136,8 @@ void* Collector::allocate(size_t s)
     block = tmp;
   }
   currentBlock = block;
-  // look for a free spot in the block
-  ValueImp **r = block->mem;
-  while (*r)
-    r++;
-  *r = m;
+  // fill free spot in the block
+  *(block->mem + block->firstfree++) = m;
   filled++;
   block->filled++;
 
@@ -223,13 +222,27 @@ bool Collector::collect()
   block = root;
   while (block) {
     ValueImp **r = block->mem;
+    bool firstfreeset = false;
     int del = 0;
     for (int i = 0; i < block->size; i++, r++) {
       ValueImp *imp = (*r);
-      if (imp && (imp->_flags & ValueImp::VI_DESTRUCTED) != 0) {
-	free(imp);
-        *r = 0L;
-        del++;
+      if (imp) {
+        if ((imp->_flags & ValueImp::VI_DESTRUCTED) != 0) {
+	  free(imp);
+          *r = 0L;
+          del++;
+          if (!firstfreeset) {
+            firstfreeset = true;
+            block->firstfree = r - block->mem;
+          }
+        } else if (firstfreeset) {
+            *(block->mem + block->firstfree) = imp;
+            block->firstfree++;
+            *r = 0L;
+        }
+      } else if (!firstfreeset) {
+        firstfreeset = true;
+        block->firstfree = r - block->mem;
       }
     }
     filled -= del;
@@ -241,6 +254,8 @@ bool Collector::collect()
 
   // delete the empty containers
   block = root;
+  currentBlock = 0L;
+  CollectorBlock *last = root;
   while (block) {
     CollectorBlock *next = block->next;
     if (block->filled == 0) {
@@ -250,13 +265,18 @@ bool Collector::collect()
         root = next;
       if (next)
         next->prev = block->prev;
-      if (block == currentBlock) // we don't want a dangling pointer
-        currentBlock = 0L;
       assert(block != root);
       delete block;
+    } else if (!currentBlock) {
+        if (block->filled < block->size)
+          currentBlock = block;
+        else
+          last = block;
     }
     block = next;
   }
+  if (!currentBlock)
+    currentBlock = last;
 #if 0
   // This is useful to track down memory leaks
   static int s_count = 0;


More information about the kfm-devel mailing list