Sure, theoretically the optimization could be extended to "floor of
multiplication". That's probably an uncommon case, though, so it may not be
worth it.


On Sun, Apr 7, 2013 at 11:43 PM, Ben Noordhuis <[email protected]> wrote:

> On Sun, Apr 7, 2013 at 11:02 PM, Jakob Kummerow <[email protected]>
> wrote:
> > This is expected. V8 has a special optimization for "Math.floor of a
> > division".
> >
> > The only way of "fixing" this would be to remove that optimization, in
> which
> > case the "/2" case would become about as slow as the "*.5" case. That's
> not
> > what you want, is it? ;-)
> >
> > Background:
> > All of this is due to JavaScript, where every number is a double. As you
> > know, integers are faster to handle on the CPU, so JS engines try to
> detect
> > cases where they can internally represent/manipulate numbers as integers
> > without violating JS semantics. Essentially, in the "floor of div" case
> V8
> > decides that it can treat the numbers as integers, and further that
> division
> > by constant 2 can be done by bit shift, so in optimized code it actually
> > emits a bit shift instruction. In the "*.5" case, on the other hand, a
> > regular floating-point multiplication is performed, and the resulting
> > floating-point number is then rounded (well, floored) to integer, which
> is a
> > bit slower.
>
> Is there anything intrinsically unsafe in the example the OP posted?
> If Math.floor is untainted (i.e. not monkey-patched by the user) and
> the multiplier is a literal, it should be safe in most cases to
> convert it to integer math, right?  The benefits may not outweigh the
> increased complexity, of course, but it doesn't look like it's
> inherently impossible.
>
> (I'm not volunteering a patch, by the way. :-))
>
> --
> --
> v8-users mailing list
> [email protected]
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to