Script optimization
|
|
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
|
12-10-2008 08:04
From: Argent Stonecutter It depends on the level of optimization and the concurrency support defined by the language. For example, in a simple stack compiler a global variable may be loaded with:
LD R1,#address_of_global LDI R2,R1
Whereas a local would need to be loaded with:
LD R1,#offset_of_local ADD SP,R1 LDI R2,R1
With more complex language constructs (such as displays or continuations) there may need to be several steps to locate the approriate context for the local.
On the other hand, with an optimizing compiler the local variable can be kept in a register all the time, and the global has to go to memory.
But neither version of LSL (LSL2 or Mono) is an optimizing compiler. Most compilers, even without optimization, only modify the stack pointer once for all the local variables required for a function's stack frame. Not only that, but most non-trivial functions will require SOME (at least temporary) value on the function's stack frame. So I disagree. Unless the compiler is a completely amateur one written overnight (and perhaps the LSL qualifier DOES qualify  ) a local variable is not likely to result in any more instructions than the global variable.
|
|
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
|
12-10-2008 08:46
From: Lear Cale Hewee is correct about multithreaded, compiled languages, but overgeneralizing for single-threaded. In my career, I've done a lot of work where I wrote in high level compiled language and debugged in machine code (hopefully disassembled, but often not), and in virtually all cases, globals were either more efficient to access or the same, and didn't have to be "optimized" to be the same as locals, *unless the locals are optimized by keeping them in registers*. Without this optimiztion, locals have to be accessed indirectly through a register ("frame pointer"  , whereas globals can be accessed directly. Of course, globals always take space Right. The point is that in ("purely"  single-threaded environments, a global might only have to be accessed the first time it is read and/or the last time it is written in a function (oh, there's one more spot that doesn't apply to locals: where another function is called), not at every access or thread synchronization point. So in essence they CAN be about as efficient as local variables in a single-threaded environment. As I understand it, most modern architectures do register-indexed (or possibly just stack-indexed depending on how specialized the processor) memory access just as quickly as direct access. Both only require one memory access, as opposed to true indirection (accessing memory referenced by memory rather than a register) which requires two accesses (though it is still often optimized to be faster than two full instructions to fetch and then read).
|
|
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
|
12-10-2008 09:24
From: Hewee Zetkin Most compilers, even without optimization, only modify the stack pointer once for all the local variables required for a function's stack frame. I didn't modify the stack pointer in that code sample, I modified R1. R1 <- offset R1 <- R1 + SP R2 <- mem(R1) Some instruction sets make part of that a single operation with complex addressing modes, but internally the CPU is performing the same operations. EG, for a classic CISC like the PDP-11, if the offset is small enough, you can do: MOV @offset(sp),r2 From: someone Not only that, but most non-trivial functions will require SOME (at least temporary) value on the function's stack frame. But that code doesn't generally involve loading an offset as well as the stack. Even for a non-optimizing compiler: either the compiler is doing a straightforward stack based expression evaluator, or they're generating register code and keeping intermediate values in registers. And, again, the LSL compiler performs NO optimization. You can verify that by examining indra.l, indra.y, lscript_tree.cpp, and so on in the open source client.
|
|
Alyssa Bijoux
Jeweler
Join date: 7 Dec 2004
Posts: 394
|
12-10-2008 14:35
No optimization? Darn. Has anyone tested whether there's still a difference between:
integer index; integer length = llGetListLength(myList); for (index = 0; index < length; index++) {}
and
integer index; for (index = 0; index < llGetListLength(myList); index++) {}
I was rather hoping to see some simple optimizations that cope with situations like this
/esc
|
|
Lear Cale
wordy bugger
Join date: 22 Aug 2007
Posts: 3,569
|
12-10-2008 15:31
From: Hewee Zetkin Most compilers, even without optimization, only modify the stack pointer once for all the local variables required for a function's stack frame. Not only that, but most non-trivial functions will require SOME (at least temporary) value on the function's stack frame. So I disagree. Unless the compiler is a completely amateur one written overnight (and perhaps the LSL qualifier DOES qualify  ) a local variable is not likely to result in any more instructions than the global variable. Fully object-oriented languages don't just reserve stack space, they create the object. Interpreted languages can work completely differently. For example, in Python, the variable is created and its name is added to the scope's dictionary. Neither of these is a case of an amateur compiler, and there are more cases. As I said above, it doesn't make sense to generalize. There are so many different possibilities.
|
|
Jeredin Denimore
Romani Ite Domum
Join date: 5 Jul 2008
Posts: 95
|
12-11-2008 06:42
From: Lear Cale Fully object-oriented languages don't just reserve stack space, they create the object.
Interpreted languages can work completely differently. For example, in Python, the variable is created and its name is added to the scope's dictionary.
Neither of these is a case of an amateur compiler, and there are more cases.
"Languages" don't do any of that... compilers/interpreters do. The nightmare that is Javascript comes to mind... or C/C++. Heck even LSL has multiple interpreters that produce different results based on the same "language" input. Yeah, I know... semantics nitpicking, but I couldn't resist with... From: Lear Cale As I said above, it doesn't make sense to generalize. 
|
|
Escort DeFarge
Together
Join date: 18 Nov 2004
Posts: 681
|
12-12-2008 18:21
From: Jeredin Denimore The nightmare that is Javascript comes to mind... For sure I understand this sentiment - but before you write off javascript entirely -- take a look at the work of/google tech talks by john resnik (inventor of JQuery). It may give you pause about JS -- and dynamically typed languages in general -- did for me  /esc
_____________________
http://slurl.com/secondlife/Together
|