JS speed - Konq vs. Mozilla
Koos Vriezen
koos.vriezen at xs4all.nl
Tue Sep 3 17:58:22 BST 2002
Hi,
Attached a small patch that makes KJS::Collector::allocate a bit faster.
It accelerates this loop
while (*r)
r++;
by remembering where it stopped the last time.
Did some test with this example:
function Bench1(){
var n=0;
for(n=0;n<1000;n++)
MyInnerLoop1();
}
function MyInnerLoop1(){
var b;
for(b=0;b<10;b++);
}
Before the patch (time .libs/testkjs ~/bench.js) 3x:
real 0m1.238s
user 0m1.120s
sys 0m0.020s
real 0m1.226s
user 0m1.080s
sys 0m0.020s
real 0m1.232s
user 0m1.120s
sys 0m0.010s
With the patch:
real 0m1.023s
user 0m0.970s
sys 0m0.010s
real 0m1.017s
user 0m0.970s
sys 0m0.010s
real 0m1.025s
user 0m0.980s
sys 0m0.010s
Gain is approx. 16%.
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 block in reserve, defragment empty slots. I'll try some tests
on these.
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 16:53:42
@@ -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)
{
@@ -135,9 +137,10 @@ void* Collector::allocate(size_t s)
}
currentBlock = block;
// look for a free spot in the block
- ValueImp **r = block->mem;
+ ValueImp **r = block->mem + block->firstfree;
while (*r)
r++;
+ block->firstfree = 1 + (r - block->mem);
*r = m;
filled++;
block->filled++;
@@ -223,13 +226,22 @@ 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 && !firstfreeset) {
+ firstfreeset = true;
+ block->firstfree = r - block->mem;
+ }
if (imp && (imp->_flags & ValueImp::VI_DESTRUCTED) != 0) {
free(imp);
*r = 0L;
del++;
+ if (!firstfreeset) {
+ firstfreeset = true;
+ block->firstfree = r - block->mem;
+ }
}
}
filled -= del;
More information about the kfm-devel
mailing list