It's important to understand that the overhead of context switching in V8
is pretty painful when compared to Python. I'm doing a bit of work myself
embedding v8 in a fairly intensive environment, and it's been a hell of a
learning experience. There's a *metric s*** ton* of difference between the
standard Python interpreter and v8 and how they are interpreted.

A key thing to understand about v8 is that it JIT compiles your Javascript
straight into machine language ( via the meta-lang pipeline: Crankshaft->
Helium-> Lithium ). This makes context switches far, far more costly,
because v8 does not actually deal directly ( much ) with its abstractions.
It does use them in formulation but then discards them when no longer
needed. This means if you go a-fetching v8 has to do some searching and
constructing before it can make your switch, unless you've asked v8 to
'hold onto' something ( which in turn, slows down the VM slightly via the
GC and forces some parts of the VM to not optimize because your essentially
telling the interpreter "this is volatile" ). This is part of the reason
why v8 encourages you to use Local Handles whenever possible.

The smackdown here isn't that hard  - your looking at about 0.00032ms or so
( off the top of my head ) - but you take an array of say, a million
elements, and there you go. Suddenly your looking at 1/3 seconds time just
to iterate over a massive JS array in C++.

Compare this to Python, or Ruby, or Perl, most interpreters of which keep
their abstractions available ( in memory ) and deal with them directly.
Context switches are not as painful because Python/Ruby/Perl do not have to
do any building up on "fetch" or "switch", they simply grab the
abstraction's pointer and go. So its really best to limit those as much as
possible.

If you do proceed, some things to keep in mind:

-As I mentioned, big arrays are painful if you need to access them in C. I
would recommend following Node's lead and using "buffer" patterns where
appropriate. In other words, try to make your switches as 'meaty' as
possible.

-It seems to be more painful to access JS from C++ ( via ->Get() ) then it
does to callback C++ from JS ( via FunctionTemplates ).

-... but it doesn't seem that painful to call JS functions, or to create
one-use JS Objects for your arguments in C++ ( via Function->Call() and
v8::Object::New ).

These are just my (not particularly scientific) observations.

Best of luck!

On Tue, Jun 12, 2012 at 6:29 PM, Joshua Holbrook <[email protected]>wrote:

> > people said that at first about numerical calculations in Python before
> NumPy existed
>
> I doubt it. Scientists *love* having a scripting language on top of
> their fortrans. It's a very old idea.
>
> --Josh
>
> On Tue, Jun 12, 2012 at 3:15 PM, Matt <[email protected]> wrote:
> > On Tue, Jun 12, 2012 at 6:09 PM, Joshua Holbrook <
> [email protected]>
> > wrote:
> >>
> >> > it's not something that can't be done, it just hasn't been done yet,
> >> > IMHO.
> >>
> >> Right. It's doable, it's just not actually that great of an idea.
> >
> >
> > Hah, well people said that at first about numerical calculations in
> Python
> > before NumPy existed, and Perl before PDL existed - after all that's what
> > Mathematica and FORTRAN were for ;-)
> >
> > --
> > Job Board: http://jobs.nodejs.org/
> > Posting guidelines:
> > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> > You received this message because you are subscribed to the Google
> > Groups "nodejs" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> > http://groups.google.com/group/nodejs?hl=en?hl=en
>
>
>
> --
> Joshua Holbrook
> Engineer
> Nodejitsu Inc.
> [email protected]
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to