Originally Posted by MunkDev
It works more closely to A, but that's not the whole story. Look at this image:
Imagine that a is a reference to the function you want to upvalue. By accessing the function through a variable, you're essentially just retrieving the pointer to the memory where the function is stored ( b). When you upvalue a function, you're copying the contents of a, that is to say the memory address to your function, but not the actual function. The function you end up calling is the same, whether global or local in your scope.
|
Exactly, by upvalues you only save that _G function call nothing else. But you see how inefficient to call _G in an OnUpdate function, or any other function that gets triggered frequently.
Global call: Pointer to the global table
_G -> lookup for the subtable
_G.func -> get the table's pointer value -> call the C function from the returned pointer
(Since lua does not support multithreading calling _G and accessing it's subtable's value will take 2 cycles!)
Upvalued call: Pointer to the upvalued
func function -> get the table's pointer value -> call the C function from the returned pointer