Advertisement
C0BRA

Lua Metatable Test

Sep 30th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. tl;dr: abc:func() = less opcodes, but slightly slower (due to metatable lookup in C),
  2. meta.func(abc) = 1 or 2 more opcodes, depending on Lua 5.1 or 5.2, but executes quicker
  3. as no need to lookup the metatable.
  4.  
  5.  
  6. (10:29:37) Kobra: infact
  7. (10:29:39) Kobra: ima test
  8. (10:29:39) Sykranos: like my bonepos func? its gonna give me the edge in hvh
  9. (10:29:39) Kobra: wait
  10. (10:30:04) Sykranos: was gonna do a full out bonescan
  11. (10:30:06) Sykranos: but too laggy
  12. (10:35:48) Kobra: rofl
  13. (10:35:51) Kobra: you're not gona belive this
  14. (10:35:54) Sykranos: wot
  15. (10:36:01) Kobra: test:func("world")
  16. (10:36:03) Kobra: uses less opcodes
  17. (10:36:04) Kobra: than
  18. (10:36:10) Kobra: meta.func(test, "world")
  19. (10:36:18) Sykranos: :O
  20. (10:36:20) Kobra: function <test.lua:15,18> (6 instructions at 0xf0be20)
  21. 0 params, 4 slots, 1 upvalue, 1 local, 3 constants, 0 functions
  22. 1 [16] GETTABUP 0 0 -1 ; _ENV "new"
  23. 2 [16] CALL 0 1 2
  24. 3 [17] SELF 1 0 -2 ; "func"
  25. 4 [17] LOADK 3 -3 ; "world"
  26. 5 [17] CALL 1 3 1
  27. 6 [18] RETURN 0 1
  28.  
  29. function <test.lua:20,23> (7 instructions at 0xf0b780)
  30. 0 params, 4 slots, 2 upvalues, 1 local, 3 constants, 0 functions
  31. 1 [21] GETTABUP 0 0 -1 ; _ENV "new"
  32. 2 [21] CALL 0 1 2
  33. 3 [22] GETTABUP 1 1 -2 ; meta "func"
  34. 4 [22] MOVE 2 0
  35. 5 [22] LOADK 3 -3 ; "world"
  36. 6 [22] CALL 1 3 1
  37. 7 [23] RETURN 0 1
  38.  
  39. (10:36:28) Kobra: top one is test:func()
  40. (10:36:33) Sykranos: so
  41. (10:36:38) Sykranos: i should be doinn
  42. (10:36:47) Sykranos: local alive = _R.Player.Alive
  43. (10:36:48) Sykranos: ?
  44. (10:37:03) Kobra: no
  45. (10:37:07) Kobra: ply:Alive()
  46. (10:37:12) Sykranos: but
  47. (10:37:14) Sykranos: muh local copy
  48. (10:37:15) Kobra: no metatable
  49. (10:37:18) Kobra: srsly
  50. (10:37:19) Kobra: it's quicker
  51. (10:37:20) Kobra: lol
  52. (10:37:24) Sykranos: okay
  53. (10:38:02) Kobra: lol
  54. (10:38:05) Kobra: that's halarious
  55. (10:38:09) Kobra: i thought it was quicker
  56. (10:38:34) Kobra: wait
  57. (10:38:36) Kobra: gmod uses
  58. (10:38:37) Kobra: lua 5.1
  59. (10:38:48) Sykranos: so is what im doing quicker or not
  60. (10:38:49) Kobra: oh my
  61. (10:38:50) Kobra: HAHAHA
  62. (10:38:56) Kobra: function <test.lua:15,18> (6 instructions, 24 bytes at 0x2035460)
  63. 0 params, 4 slots, 0 upvalues, 1 local, 3 constants, 0 functions
  64. 1 [16] GETGLOBAL 0 -1 ; new
  65. 2 [16] CALL 0 1 2
  66. 3 [17] SELF 1 0 -2 ; "func"
  67. 4 [17] LOADK 3 -3 ; "world"
  68. 5 [17] CALL 1 3 1
  69. 6 [18] RETURN 0 1
  70.  
  71. function <test.lua:20,23> (8 instructions, 32 bytes at 0x2034830)
  72. 0 params, 4 slots, 1 upvalue, 1 local, 3 constants, 0 functions
  73. 1 [21] GETGLOBAL 0 -1 ; new
  74. 2 [21] CALL 0 1 2
  75. 3 [22] GETUPVAL 1 0 ; meta
  76. 4 [22] GETTABLE 1 1 -2 ; "func"
  77. 5 [22] MOVE 2 0
  78. 6 [22] LOADK 3 -3 ; "world"
  79. 7 [22] CALL 1 3 1
  80. 8 [23] RETURN 0 1
  81.  
  82. (10:39:01) Kobra: lua 5.1 is even SLOWER
  83. (10:39:07) Kobra: doing the manuyal meta way
  84. (10:39:15) Kobra: 2 instructions more
  85. (10:39:45) Kobra: in lua 5.2
  86. (10:39:46) Kobra: 3 [22] GETUPVAL 1 0 ; meta
  87. 4 [22] GETTABLE 1 1 -2 ; "func"
  88. (10:39:52) Kobra: ins converted into 1 opcode
  89. (10:39:55) Kobra: that's the difference
  90. (10:40:02) Kobra: 3 [22] GETTABUP 1 1 -2 ; meta "func"
  91. (10:40:14) Sykranos: muh local copies
  92. (10:41:03) Kobra: wait
  93. (10:41:09) Kobra: im going to test
  94. (10:41:10) Sykranos: you know what would be cool?
  95. (10:41:13) Kobra: by having empty func
  96. (10:41:15) Sykranos: lua bindings to C for trace arrays
  97. (10:41:23) Sykranos: then an auto wall would be feasible
  98. (10:41:39) Sykranos: so would a bonescan
  99. (10:41:54) Kobra: 3 [22] GETTABUP 1 1 -2 ; meta "func"
  100. 4 [22] MOVE 2 0
  101. 5 [22] LOADK 3 -3 ; "world"
  102. 6 [22] CALL 1 3 1
  103. 7 [23] RETURN 0 1
  104.  
  105. (10:41:59) Kobra: = manual meta way
  106. (10:42:04) Kobra: those are the relevant opcodes
  107. (10:42:21) Kobra: this is
  108. (10:42:27) Kobra: meta way
  109. (10:42:28) Kobra: 3 [17] SELF 1 0 -2 ; "func"
  110. 4 [17] LOADK 3 -3 ; "world"
  111. 5 [17] CALL 1 3 1
  112.  
  113. (10:42:38) Sykranos: its too early in the morning for this
  114. (10:42:43) Sykranos: WHICH 1 IS FASTER?!
  115. (10:42:56) Kobra: _R.Player.Alive(pl) is slower
  116. (10:43:01) Kobra: pl:Alive() is faster
  117. (10:43:52) Kobra: eh
  118. (10:43:54) Kobra: so you can see
  119. (10:43:57) Kobra: this will make more sense
  120. (10:43:58) Kobra: function a()
  121. local test = new()
  122. test:func("world")
  123. end
  124.  
  125. function b()
  126. local test = new()
  127. meta.func(test, "world")
  128. end
  129.  
  130. function c()
  131. local test = new()
  132. end
  133.  
  134. (10:44:09) Kobra: function <test.lua:15,18> (6 instructions at 0x1ee1e20)
  135. 0 params, 4 slots, 1 upvalue, 1 local, 3 constants, 0 functions
  136. 1 [16] GETTABUP 0 0 -1 ; _ENV "new"
  137. 2 [16] CALL 0 1 2
  138. 3 [17] SELF 1 0 -2 ; "func"
  139. 4 [17] LOADK 3 -3 ; "world"
  140. 5 [17] CALL 1 3 1
  141. 6 [18] RETURN 0 1
  142.  
  143. function <test.lua:20,23> (7 instructions at 0x1ee1780)
  144. 0 params, 4 slots, 2 upvalues, 1 local, 3 constants, 0 functions
  145. 1 [21] GETTABUP 0 0 -1 ; _ENV "new"
  146. 2 [21] CALL 0 1 2
  147. 3 [22] GETTABUP 1 1 -2 ; meta "func"
  148. 4 [22] MOVE 2 0
  149. 5 [22] LOADK 3 -3 ; "world"
  150. 6 [22] CALL 1 3 1
  151. 7 [23] RETURN 0 1
  152.  
  153. function <test.lua:25,27> (3 instructions at 0x1ee2950)
  154. 0 params, 2 slots, 1 upvalue, 1 local, 1 constant, 0 functions
  155. 1 [26] GETTABUP 0 0 -1 ; _ENV "new"
  156. 2 [26] CALL 0 1 2
  157. 3 [27] RETURN 0 1
  158.  
  159. (10:45:15) Sykranos: ALRIGHT
  160. (10:45:20) Kobra: hmmm, maybe the metamethods being called
  161. (10:45:25) Kobra: slows it down?
  162. (10:45:30) Kobra: idk
  163. (10:45:33) Kobra: ill test
  164. (10:45:39) Kobra: via bruiteforce
  165. (10:48:33) Sykranos: what if i used rawget to access it
  166. (10:49:06) Kobra: well
  167. (10:49:11) Kobra: then you're calling rawget
  168. (10:49:21) Sykranos: but not calling _newindex
  169. (10:50:00) Kobra: yeah, but your replacing the __index call (newindex is setting) with the rawget
  170. (10:50:06) Kobra: the __index call isn't done either
  171. (10:50:11) Kobra: it's only done IF there is a metatable
  172. (10:50:18) Sykranos: dig bicks
  173. (10:50:22) Kobra: and that check is done in C
  174. (10:50:24) Kobra: a 22.514733076096
  175. b 21.983674049377
  176.  
  177. (10:50:30) Kobra: so, even tho it's more opcodes
  178. (10:50:33) Kobra: it's quicker
  179. (10:50:44) Kobra: i presume the metatable lookup is slightly slower
  180. (10:50:50) Kobra: but, it's so negligable
  181. (10:51:02) Kobra: i'd keep the erm
  182. (10:51:09) Kobra: abc:def()
  183. (10:51:12) Kobra: for reduced bugs
  184. (10:51:15) Kobra: simpler code
  185. (10:51:18) Kobra: and easier to maintain
  186. (10:51:20) Kobra: and read
  187. (10:51:26) Sykranos: wudeva
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement