On 30.03.2011 08:31, thorsten.i.r...@jyu.fi wrote:

How do arrays and objects count? If an object counts as one reference,
then it's very efficient to use I guess - if every key in the hash counts
as one, then certainly less so.

 Right. I believe arrays and hashs may both be implemented efficiently - so
may count as one Nasal object only. But I haven't looked into the details of
the Nasal engine itself - I just checked the discussed performance issues
and briefly looked at the garbage collector. I'm sure Andy would know all
the tricks and details, of how efficient specific constructs are :).

Can loops pile up garbage checks only for elements referenced
(theoretically) within the loop (regardless if they are actually
executed), or do they trigger the whole possible pile of Nasal to be
searched? So if someone fixes a badly written script which runs every
frame although it really doesn't need to - will that affect the load
generated by that particular script, or will it have a more general

 Garbage collection is a global thing and cannot be restricted to a local
context. It always requires a complete search. Even if "usless" handlers are
executing, eventually their repeated execution will also trigger the garbage
collector, which then needs to recurse through all references. The load
(delay) caused by the garbage collector will hit a random Nasal line, which
happened to be running out of "certain resources". Again, Andy would
probably know the details of what exactly the conditions for triggering the
garbage collector are, and what kind of operations consume these resources -
so make the g/c happen more often.

However, I have attached a simple patch which I used to generate the
statistics. So, if you're interested in optimizing the performance of your
module, apply this locally and see how the numbers and the g/c frequency are
influenced by specific changes. You'll see how many unique objects were
found, how many references had to be searched (which is what consumes most
of the time, since every reference has to be traversed), and of course
when/how often the garbage collector runs.

diff --git a/simgear/nasal/gc.c b/simgear/nasal/gc.c
--- a/simgear/nasal/gc.c
+++ b/simgear/nasal/gc.c
@@ -1,9 +1,16 @@
 #include "nasal.h"
 #include "data.h"
 #include "code.h"
 #define MIN_BLOCK_SIZE 32
+    static int g_ObjectCount = 0;
+    static int g_RefCount = 0;
 static void reap(struct naPool* p);
 static void mark(naRef r);
@@ -37,6 +44,11 @@ static void garbageCollect()
     int i;
     struct Context* c;
+    g_ObjectCount = 0;
+    g_RefCount = 0;
     globals->allocCount = 0;
     c = globals->allContexts;
     while(c) {
@@ -74,6 +86,11 @@ static void garbageCollect()
         globals->deadBlocks = naAlloc(sizeof(void*) * globals->deadsz);
     globals->needGC = 0;
+    printf("**** Nasal garbage collection statistics: objects: %u, references: %u\n",
+            g_ObjectCount,g_RefCount);
 void naModLock()
@@ -226,11 +243,21 @@ static void mark(naRef r)
     int i;
+    // count every traversed reference
+    g_RefCount++;
     if(IS_NUM(r) || IS_NIL(r))
     if(PTR(r).obj->mark == 1)
+    // count unique objects
+    g_ObjectCount++;
     PTR(r).obj->mark = 1;
     switch(PTR(r).obj->type) {

Create and publish websites with WebMatrix
Use the most popular FREE web apps or write code yourself; 
WebMatrix provides all the features you need to develop and 
publish your website. http://p.sf.net/sfu/ms-webmatrix-sf
Flightgear-devel mailing list

Reply via email to