Comment #6 on issue 2513 by [email protected]: life benchmark slowness
http://code.google.com/p/v8/issues/detail?id=2513

Let's look at the concrete code:

-- bool.js -----------------------------
function toInt(b) { return b ? 1 : 0; }
toInt(true);
toInt(false);
%OptimizeFunctionOnNextCall(toInt);
toInt(true);
----------------------------------------

One can see the code for toInt with a build of d8 containing a disassembler
(e.g. a debug build):

  d8 --allow-natives-syntax --print-opt-code --code-comments ~/bool.js

Ignoring some function prologue/epilogue code, the interesting part is:

0x3d62a77a 26 3db1808059 cmp eax, 0x598080b1 ;; object: 0x598080b1 <true>
0x3d62a77f  31  0f8417000000  jz 60  (0x3d62a79c)
0x3d62a785 37 3dc1808059 cmp eax, 0x598080c1 ;; object: 0x598080c1 <false>
0x3d62a78a  42  0f8405000000  jz 53  (0x3d62a795)
0x3d62a790 48 e975f88d0d jmp 0x4af0a00a ;; deoptimization bailout 1
0x3d62a795  53  33c0          xor eax,eax
0x3d62a797  55  e905000000    jmp 65  (0x3d62a7a1)
0x3d62a79c  60  b802000000    mov eax,0x2

Given the current way how v8 collects and uses type information, this is as good as it can get. Even if we somehow handle 'boolean | 0' specially, the resulting
code would not be any different. The only way this could be better is making
sure that the code above is only ever called with true/false, invalidating that code if that is not the case anymore. If we could guarantee that, a comparison and two conditional moves would suffice. But this is not the way v8 currently works, and this won't change very soon, so the ternary is *the* efficient way to convert booleans to numbers in v8. Thinking about it, if we change the way we
handle assumptions in the future, expressions like b?1:0 should be easily
compiled into the branch-free code, just like b|0.

In a nutshell: Use b?1:0, it will result in the best code you can get, and
probably this will stay the case in the forseeable future.


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" 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