Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

recycle array objects instead of GC them

19 views
Skip to first unread message

franck.fr

unread,
Nov 14, 2006, 9:37:44 AM11/14/06
to
Hello,

In my application, I use a huge number of Array (as vector) like this: body1.position = [1,2,3];
This make the GC running very often, and causes big lags in my application.
Is it possible to tell SpiderMonkey to reuse unreferenced array objects instead of create new ones ?

Franck.

Mike Shaver

unread,
Nov 14, 2006, 4:10:11 PM11/14/06
to dev-tech-...@lists.mozilla.org

No, that's not currently possible.

Mike

franck.fr

unread,
Nov 15, 2006, 4:43:58 AM11/15/06
to


Thanks for your response.

But I wondering if there is a way to workaround the lake of recycling :

My first idea is to create a Vector class then :
- detect in a GC callback ( in state JSGC_MARK_END ) Vector objects that are about to be finalized (JS_IsAboutToBeFinalized)
- unmark these Vecor for being GC
- store these object in a list
Later, when I need a new Vector object, I just have to pop it from the list.

My second idea is to create a pool of Vector in a js object ( this create a reference to the object and avoid it from being GC ),
an then, when I need a new (or unused) vector object, I just have to get a Vector from the pool and check if its reference counter is 1.


how to unmark an object from being GC ?
how to get the reference counter of an object ( obj->map->nrefs ?? )


Franck

Mike Shaver

unread,
Nov 15, 2006, 5:40:15 PM11/15/06
to dev-tech-...@lists.mozilla.org
On 11/15/06, franck.fr <spamma...@gmail.com> wrote:
> how to unmark an object from being GC ?

Objects cannot be resurrected.

If the state that you want to avoid recreating is the private pointer,
then you could recycle that part of it yourself. The creation of the
JSObject itself is typically not very expensive.

> how to get the reference counter of an object ( obj->map->nrefs ?? )

Objects in SpiderMonkey aren't reference counted, so you can't know
the number of references to a given object (though you do know that
it's zero when the object is finalized).

Mike

Chris Dillman

unread,
Nov 16, 2006, 2:48:17 AM11/16/06
to dev-tech-...@lists.mozilla.org

Can JS do Coroutines?

Or could that be faked or hacked into to spidermonkey easily?


--
---------------------------------------------

Email: chr...@plaidworld.com
iChat / AIM: crack...@mac.com

Plaid World Studios http://www.plaidworld.com

Jean-Christophe PLAGNIOL-VILLARD

unread,
Nov 16, 2006, 5:02:46 PM11/16/06
to Chris Dillman, dev-tech-...@lists.mozilla.org

If i'm not wrong you can save your context and using it throw the exception
mecanisme.

As you do with this code to inject some code inside the VM part

JSString* jsstr;
// if we get errors during error reporting we report those
if ( ((jsstr=JS_NewStringCopyZ(cx, msg)))
&& (JS_AddNamedRoot(cx,&jsstr,"jsstr")) ) {
jsval dummy;
// We can't use JS_EvaluateScript since the stack would be wrong
JSFunction *func;
JSObject* fobj;
const char* fbody="throw new Exception(msg);";
const char* argnames[]={"msg"};
if ((func=JS_CompileFunction(cx, obj, NULL,
1, argnames,
fbody, strlen(fbody),
NULL, 0))) {
// root function
if ( ((fobj = JS_GetFunctionObject(func)))
&& (JS_AddNamedRoot(cx, &fobj, "fobj")) ) {
jsval args[]={STRING_TO_JSVAL(jsstr)};
JS_CallFunction(cx, obj, func, 1, args, &dummy);
JS_RemoveRoot(cx, &fobj);
}
}
JS_RemoveRoot(cx,&jsstr);

Le 16/11/06 8:48, « Chris Dillman » <chr...@plaidworld.com> a écrit :

> Coroutines


Gordon Smith

unread,
Nov 16, 2006, 8:56:26 PM11/16/06
to Mike Shaver, dev-tech-...@lists.mozilla.org
Wouldn't it tax the GC less if you wrote code like

body1.position[0] = 1;
body1.position[1] = 2;
body1.position[2] = 3;

which stuffs the existing array with new values rather than replacing it
with a newly created arrray?

- Gordon


-----Original Message-----
From: dev-tech-js-e...@lists.mozilla.org
[mailto:dev-tech-js-e...@lists.mozilla.org] On Behalf Of Mike
Shaver
Sent: Tuesday, November 14, 2006 1:10 PM
To: dev-tech-...@lists.mozilla.org
Subject: Re: recycle array objects instead of GC them

On 11/14/06, franck.fr <spamma...@gmail.com> wrote:
> Hello,
>
> In my application, I use a huge number of Array (as vector) like this:
body1.position = [1,2,3];
> This make the GC running very often, and causes big lags in my
application.
> Is it possible to tell SpiderMonkey to reuse unreferenced array
objects instead of create new ones ?

No, that's not currently possible.

Mike
_______________________________________________
dev-tech-js-engine mailing list
dev-tech-...@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-tech-js-engine

Chris Dillman

unread,
Nov 17, 2006, 2:33:24 AM11/17/06
to dev-tech-...@lists.mozilla.org
>If i'm not wrong you can save your context and using it throw the exception
>mecanisme.
>
>As you do with this code to inject some code inside the VM part


Im not sure I follow...

But are you saing that if I throw an exception.

I can preserve the state of the function I exited and resume later?

Like calling Yield in some languages that support Coroutines?

>JSString* jsstr;
> // if we get errors during error reporting we report those
> if ( ((jsstr=JS_NewStringCopyZ(cx, msg)))
> && (JS_AddNamedRoot(cx,&jsstr,"jsstr")) ) {
> jsval dummy;
> // We can't use JS_EvaluateScript since the stack would be wrong
> JSFunction *func;
> JSObject* fobj;
> const char* fbody="throw new Exception(msg);";
> const char* argnames[]={"msg"};
> if ((func=JS_CompileFunction(cx, obj, NULL,
> 1, argnames,
> fbody, strlen(fbody),
> NULL, 0))) {
> // root function
> if ( ((fobj = JS_GetFunctionObject(func)))
> && (JS_AddNamedRoot(cx, &fobj, "fobj")) ) {
> jsval args[]={STRING_TO_JSVAL(jsstr)};
> JS_CallFunction(cx, obj, func, 1, args, &dummy);
> JS_RemoveRoot(cx, &fobj);
> }
> }
> JS_RemoveRoot(cx,&jsstr);
>
>Le 16/11/06 8:48, « Chris Dillman » <chr...@plaidworld.com> a écrit :
>
>> Coroutines

0 new messages