Nice catch - pun intended.

The reason for the slowdown is the use of try/catch.

The scope introduced by the catch clause (containing the variable "e" here)
is implemented similarly to the "with" construction. It introduces a scope
object which is potentially mutable, which makes variable lookups harder to
optimize and therefore more expensive (e.g., the lookup of the Math object).

A much superior way to test for the non-existence of the FudgeSystem
variable is:

  if (typeof FudgeSystem == "undefined") {

    // define FudgeSystem, i.e., the contents of the catch clause.

  }
If you make this change, the difference goes away.
Kinds regards
Lasse.
On Mon, Mar 30, 2009 at 10:59 PM, <codesite-nore...@google.
com> wrote:

>
> Status: New
> Owner: ----
>
> New issue 293 by sgbeal: Call of member func, even called as non-member,
> 3-4 times slower than global func
> http://code.google.com/p/v8/issues/detail?id=293
>
> Calling two code-identical functions, where one is global scope and one is
> a member, requires radically different call times. The member func, even if
> it is called without a 'this' object, requires 3-4 times as long to call.
>
> Demonstration code follows. Jump to the bottom and follow the testDF()
> call. The point of interest is marked with "HOLY COW".
>
>
> try
> {
>     undefined === FudgeSystem;
> }
> catch(e){
> FudgeSystem = (function() {
>     var fudge = {};
>     fudge.rollDF = function (n) {
>         if( 0 == arguments.length ) n = 4;
>         var rc = 0;
>         var df = function() { return Math.floor( (Math.random() * 1000 %
> (3))) - 1; };
>         for( var i = 0; i < n; ++i )
>         {
>             rc += df();
>         }
>         return rc;
>     };
>     return fudge;
> })();
> }
>
> // Identical to FudgeSystem.rollDF, but is not a member func:
> function rollDF(n) {
>     if( 0 == arguments.length ) n = 4;
>     var rc = 0;
>     var df = function() { return Math.floor( (Math.random() * 1000 % (3)))
> - 1; };
>     for( var i = 0; i < n; ++i )
>     {
>             rc += df();
>     }
>     return rc;
> }
>
> function testDF()
> {
>     var n = 10000;
>     var dN = 4;
>     print( new Date() );
>     print('Rolling',n,'sets of',dN+'dF...');
>     var func =
>         FudgeSystem.rollDF; // HOLY COW! This is about 3.5x slower!
>         //rollDF; // much faster!
>     for( var i = 0; i < n; ++i )
>     {
>         func(dN);
>     }
>     print( new Date() );
> }
> testDF();
>
>
> Sample output from that program, using both approaches to
> calling 'func(dN)':
>
> Member func:
>
> step...@jareth:~/cvs/v8-juice/js-snippets/gaming$ time v8-juice-shell
> speed.js
> Mon Mar 30 2009 22:48:25 GMT+0200 (CEST)
> Rolling 10000 sets of 4dF...
> Mon Mar 30 2009 22:48:25 GMT+0200 (CEST)
>
> real    0m0.438s
> user    0m0.432s
> sys     0m0.008s
>
> Non-member func:
>
> step...@jareth:~/cvs/v8-juice/js-snippets/gaming$ time v8-juice-shell
> speed.js
> Mon Mar 30 2009 22:48:32 GMT+0200 (CEST)
> Rolling 10000 sets of 4dF...
> Mon Mar 30 2009 22:48:33 GMT+0200 (CEST)
>
> real    0m1.626s
> user    0m1.624s
> sys     0m0.000s
>
>
> i'm on Ubuntu Linux on an i86/32, but i assume that this is a
> platform-independent behaviour.
>
>
> --
> You received this message because you are listed in the owner
> or CC fields of this issue, or because you starred this issue.
> You may adjust your issue notification preferences at:
> http://code.google.com/hosting/settings
>
> >
>


-- 
Lasse R.H. Nielsen
l...@google.com
'Faith without judgement merely degrades the spirit divine'

--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
v8-dev@googlegroups.com
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to