Why can this code be slow ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Why can this code be slow ?

Marten Feldtmann-2
I'm developing for setup boxes  and have trouble while starting up Amber application on these boxes. the following code in boot.js is executed with "smalltalk.object" and it creates the base structure of the whole system.

While this code takes around 0.2 seconds on a normal PC based browser, the code takes around 8 seconds on these small boxes (Amber in deploy=true mode).

ANY idea what could be so difficult in this code for Javascript interpreters on small boxes

   st.initClass = function(klass) {
        var subclasses = st.subclasses(klass);
        var methods, prototype = klass.fn.prototype;

        if(klass.superclass && klass.superclass !== nil) {
            methods = st.methods(klass.superclass);

            //Methods linking
            for(var keys = Object.keys(methods), i=0; i<keys.length; i++) {
                var key = keys[i];
                if(!prototype.methods[key]) {
                    prototype.inheritedMethods[key] = methods[key];
                    Object.defineProperty(prototype, methods[key].jsSelector, {
                        value: methods[key].fn, configurable: true, writable: true
                    });
                }
            }
        }

        for(var i=0; i<subclasses.length; i++) {
            st.initClass(subclasses[i]);
        }
    };

Reply | Threaded
Open this post in threaded view
|

Re: Why can this code be slow ?

SebastianHC
Have you already tried to store repeaded call into arrays into temporary
variables?
And have you already tried to replace the for-loop in the end by:

for (var i=0, il=subclasses.length; i<il; i++) {
    st.initClass(subclasses[i]);
     };


Might be faster.
Sebastian



Am 06.12.2012 05:23, schrieb Marten Feldtmann:

>
>    st.initClass = function(klass) {
>         var subclasses = st.subclasses(klass);
>         var methods, prototype = klass.fn.prototype;
>
>         if(klass.superclass && klass.superclass !== nil) {
>             methods = st.methods(klass.superclass);
>
>             //Methods linking
>             for(var keys = Object.keys(methods), i=0; i<keys.length;
> i++) {
>                 var key = keys[i];
>                 if(!prototype.methods[key]) {
>                     prototype.inheritedMethods[key] = methods[key];
>                     Object.defineProperty(prototype,
> methods[key].jsSelector, {
>                         value: methods[key].fn, configurable: true,
> writable: true
>                     });
>                 }
>             }
>         }
>
>         for(var i=0; i<subclasses.length; i++) {
>             st.initClass(subclasses[i]);
>         }
>     };

Reply | Threaded
Open this post in threaded view
|

Re: Why can this code be slow ?

Marten Feldtmann-2
Yes, I've changed the loops, but actually did not get any real speed improvements.

In total the system creates 24000 "instances" of Object.Property - and this takes time and the boxes I have to use are slow - around 50 to 100 times slower than a normal PC browser or the HbbTV emulator from Opera on my PC. Systems like the Volkbox or receiver from Humax need around 8 seconds to get this job done - but PC are doing this within 0.2 seconds.

My Galaxy S1 - by the way - needs 4 seconds for this stuff. Actually on my smartphone I am used to wait and actually I would accept 8 seconds - but in front of my tv set, this is far too long.

One idea could be to reduce subclassing in a radical way and throw away not needed classes.


Reply | Threaded
Open this post in threaded view
|

Re: Why can this code be slow ?

Herby Vojčík
Well, if the sheer number of property descriptors passed into
Object.defineProperty is the problem, you can reuse the descriptor objects.

Instead of
   Object.defineProperty(target, "name", { value: ...,
     enumerable: x, configurable: y, writable: z });
called many times in a loop, you should do
   var prop_xyz = { enumerable: x, configurable: y, writable: z };
(for all combinations of x, y and z you are using),

and in the loop just:
   prop_xyz.value = ...;
   Object.defineProperty(target, "name", prop_xyz);

Maybe that can help.

Herby

Marten Feldtmann wrote:

> Yes, I've changed the loops, but actually did not get any real speed
> improvements.
>
> In total the system creates 24000 "instances" of Object.Property - and
> this takes time and the boxes I have to use are slow - around 50 to 100
> times slower than a normal PC browser or the HbbTV emulator from Opera
> on my PC. Systems like the Volkbox or receiver from Humax need around 8
> seconds to get this job done - but PC are doing this within 0.2 seconds.
>
> My Galaxy S1 - by the way - needs 4 seconds for this stuff. Actually on
> my smartphone I am used to wait and actually I would accept 8 seconds -
> but in front of my tv set, this is far too long.
>
> One idea could be to reduce subclassing in a radical way and throw away
> not needed classes.
>
>