lua-users home
lua-l archive

Re: Ternary operator patch

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 9/21/2010 7:42 AM, Ryota Hirose wrote:
Hello, Lua Hackers,
2010年9月19日 Tom N Harris wrote:
 - The script code and compiled binary is shorter than
 other notation. So
 the code will run fast.
 Can you give an example of how the bytecode would be shorter?
 As far as I can tell, the same instructions should be
 generated as using an explicit if-then-else.
I just compiled following code by luac with my patch:
# cat b.lua
x = if a then b else c
y = a and b or c
if a then z = b else z = c end
# src/luac -l b.lua
main <b.lua:0,0> (24 instructions, 96 bytes at 0x10e94c8)
0+ params, 2 slots, 0 upvalues, 0 locals, 6 constants, 0 functions
[snip]
16 [5] GETGLOBAL 0 -2 ; a
17 [5] TEST 0 0 0
18 [5] JMP 3 ; to 22
19 [5] GETGLOBAL 0 -3 ; b
20 [5] SETGLOBAL 0 -6 ; z
21 [5] JMP 2 ; to 24
22 [5] GETGLOBAL 0 -4 ; c
23 [5] SETGLOBAL 0 -6 ; z
24 [5] RETURN 0 1
So, source line 1 was compiled to 7 lines binary, line 3 was 8
lines, line 5 was 9 lines.
Look closely, why are you counting the RETURN?
So this is a 'win'? Colour me unimpressed.
--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia

AltStyle によって変換されたページ (->オリジナル) /