Subj : Re: Performance Advice on Maintaining Many Contexts To : netscape.public.mozilla.jseng From : peter.kyme Date : Thu Mar 03 2005 03:52 am Brendan Eich wrote in message news:<422666B8.9090900@meer.net>... > Peter Kyme wrote: > > My problem involves a system containing a large number (> 100) of > > objects, each of which has an associated Spidermonkey context. > > > > The objects execute a small javascript repeatedly, iterating until > > termination. This javascript is user defined, and so may contain > > arbitrary local variables, > > > Do you really mean local as in function-local, or just top-level vars > private to the script? If the latter, you need a global object per > script to hold those global vars, but not a context per script unless > you are executing scripts concurrently. > I just meant top-level vars private to the script. I now see that I only need a global object per script, not a seperate context as well. My design involves having a static class that contains C++ functions that are callable from javascript. I was using an STL map to associate JS contexts with the target C++ object. From what I now understand I should use JS_GetGlobalObject to get the current global object of the context, and use that as the key for my map. However, I also have the situation where one of the C++ callback functions can create a new C++ object and associated JS global object, which is then executed once before the callback returns control to the original JS code. eg: JS_SetGlobalObject(cx, gobj1) globalObject1 -> running JS code, which calls 'fork' C++ function globalObject1 -> C++ 'fork' function, creates new globalObject2 JS_SetGlobalObject(cx, gobj2) globalObject2 -> runs JS code, completes JS_SetGlobalObject(cx, gobj1) globalObject1 -> returns control to original JS code Is this sort of behaviour acceptable? Or would I need seperate contexts, even though there aren't strictly seperate execution threads. > > > which was my motivation for creating a > > context per object (no multithreading is involved). > > > A JSContext is the state you need to execute concurrently, so if you > have only one thread, you need no more than one JSContext. > > Why did you make a context per object? > > > > This appears to work well, but I've encountered a nasty performance > > issue at cleanup time. Running the program through once, without doing > > any memory cleanup takes ~1.2s. If I then add code to destroy the JS > > contexts before exits, the program take ~17s to execute. > > > What version of the engine are you using? > > /be I'm running 1.5-rc6a, compiled on Linux. The above numbers were for the debug version of libjs, the optimised version comes down to 9s. Thanks for your help. .