<p>I am very poor at explaining things and left people a little bit confused sorry! <img alt="

" class="bbc_emoticon" src="http://www.computercraft.info/forums2/public/style_emoticons/default/unsure.png" title="

" /> Until I can update the guide in a few hours please read through this of good points brought up by another player. A lot either I didn't explain well at all or misunderstood the reason why and he does know a bit more on instructions than me so I will fix those accordingly</p>
<p> </p>
<div>
Quote
</div>
<div>WEAK:avoid upvalues //partially true, upvalues are usually a good approach and in most cases can not be superseded by any other means. Users should not 'avoid' upvalues since doing so will probably render the resultant design inefficient. </div>
<div>
</div>
<div>I put that this one was only partially true depending on case. This is considered a micro optimization and why i left it as WEAK</div>
<div> </div>
<div>
Quote
</div>
<div>MAJOR: no variables in functions //false, very very false. The code itself is a function, which is why loadstring returns a function, and the bytecode accepts the lowest level as a function!! Code execution cannot occur outside of a function, therefore all your variables are indeed in functions!</div>
<div>
</div>
<div>whenever possible especially in conjunction with strings lua attempts to keep only 1 point of reference for each variable and instead relies on pointers for any further references to it. This is especially true in terms of tables. When you place a variable definition inside of a function the bytecode has to define that variable every time the function runs again instead of just using its pointer. Allocation for it to create and dump a variable over and over again is more taxing on speed than merely telling it to hold onto that variable.</div>
<div> </div>
<div>
Quote
</div>
<div>MAJOR: pre-index tables (please everyone read this one especially) //true, should be stated that the best way to preindexing is filling with nil values, since such a preindex can then execute in a single instruction</div>
<div>
</div>
<div>nil isnt the best way to do this because the handle can also lower its own array size. This is a very misunderstood technique and people just started assuming making a lot of nils would be better because its fewer instructions. When you first create this table it will be of the size you want it to be however as soon as you add your first item to this table it will check its size and lower to match that single item putting even more work into it than had you just not preindexed at all. while Lua doesnt check for adding nil into a table it will check the number of items soon as a non nil is added.</div>
<div> </div>
<div>
Quote
</div>
<div>avoid loadstring //false, there is no other way to natively compile Lua source code. Correct name for this advice would be something like 'Avoid frequent code compilation'.</div>
<div>
</div>
<div>not false similar to avoid upvalues. This is considered a micro-optimization technique where you sacrifice some readability for speed. There are ways occasionally to get around loadstring it is just most often preferred.</div>
<div> </div>
<div>
Quote
</div>
<div>MAJOR:use table.concat to concat strings //false, complete hoax! the native concatenation operator is faster and can concatenate multiple strings at once (using a single instruction), while saving the overhead of a function call AND a global variable lookup (table) AND the table lookup (concat) !!!</div>
<div>EDIT: Here is some evidence, taken from the document linked below:</div>
<div>
</div>
<div>>local a = "foo".."bar".."baz"</div>
<div>; function [0] definition (level 1)</div>
<div>; 0 upvalues, 0 params, 3 stacks</div>
<div>.function 0 0 2 3</div>
<div>.local "a" ; 0</div>
<div>.const "foo" ; 0</div>
<div>.const "bar" ; 1</div>
<div>.const "baz" ; 2</div>
<div>[1] loadk 0 0 ; "foo"</div>
<div>[2] loadk 1 1 ; "bar"</div>
<div>[3] loadk 2 2 ; "baz"</div>
<div>[4] concat 0 0 2</div>
<div>[5] return 0 1</div>
<div>; end of function</div>
<div>
</div>
<div>In the second example, three strings are concatenated together. Note that there is no string</div>
<div>constant folding. Lines [1] through [3] loads the three constants in the correct order for</div>
<div>concatenation; the CONCAT on line [4] performs the concatenation itself and assigns the</div>
<div>result to local a.</div>
<div>// end of evidence</div>
<div>
</div>
<div>This issue with this one is all of my benchmark tests continually show again and again table.concat is faster than string concats. every Benchmark test ive seen anyone in Lua do has shown string being slower. Every Lua site suggests using table instead of string. some of the official Lua pages mention using table instead of string. And again ive not seen a single benchmark show either being the same speed or string being faster. Your evidence shows maybe i dont understand why its faster but it has nothing to do with the fact it still is faster.</div>
<div> </div>
<div>
Quote
</div>
<div>use sequential numeric indexes in table //true, arrays are faster than tables</div>
<div>
</div>
<div> </div>
<div>
Quote
</div>
<div>MAJOR: don't put constants in loops //false, local variables belong to the parent function, Lua VM has a special structure that stores information about when a local variable goes out of the scope. This does not affect loops in any way!</div>
<div>
</div>
<div>this is again like the function one. Benchmark and semi-official/official websites all state differently. If I am mistake on why this method is slower and its not defining the function every single iteration forgive me on that regardless the speed tests and other users dont lie.</div>
<div> </div>
<div>
Quote
</div>
<div>multiply dont divide //not tested, but this definitely depends on the implementation, so cannot be applied to all Lua platforms</div>
<div>
</div>
<div>tested thoroughly on Computercraft LuaJ implementation. valid enough?</div>
<div> </div>
<div>
Quote
</div>
<div>put all non variable math first //true, but not because the reason given. The Lua compiler does limited constant folding for numbers, so such an expression is evaluated at compile time and saved as the resultant constant, has nothing to do with lookups (which are pretty much instantaneous in case of local variables and constants btw)v</div>
<div> </div>
<div>
</div>
<div>I stated I wasnt sure why this is true it is just backed up by the numbers. I will use your explanation on it as to why</div>
<div> </div>
<div>
Quote
</div>
<div>use factorization //idk about this one</div>
<div>
</div>
<div>simpler equations are better basically its a common suggestion for any language because it holds true for any</div>
<div> </div>
<div>
Quote
</div>
<div>WEAK: use for instead of while //false, this was only true for Lua 4.x and no longer applies to Lua 5. A 'while true do ... end' loop will translate into a single unconditional jump instruction, so is very very efficient, much better than for loops actually (but then again, fors are more efficient in other scenarios)</div>
<div> </div>
<div>
</div>
<div>NORMALLY you are right yes. however with the implementation of Computercraft for and while loops both execute at extremely similar speeds. A for statement tested over repeated tests taking place over a week completed on average about 5% faster than a while statement. That being said both are very efficient and fast. This is considered micro-optimization and listed as WEAK</div>
<div> </div>
<div>
Quote
</div>
<div>avoid table.insert //partially true, but table.insert is more readable and easier to use if you aren't appending to the end of the array (and table.insert could be implemented Java/C/C++/Whatever-side, so it actually could be faster, but generally isn't)</div>
<div>
</div>
<div>micro-optimization tip. Some optimization takes speed in favor of readability.</div>
<div> </div>
<div>
Quote
</div>
<div>string metatables //true, but fastest would be #"asdf"</div>
<div>
</div>
<div>I believe you are right on that one and honestly didnt even think of that method. I will test it later and add that as an option if it does prove to be faster(i dont see why it wouldnt be with no function overhead)</div>
<div> </div>
<div>
Quote
</div>
<div>avoid using the assert function //false, you apparently misunderstood what assert does. Assert errors if the condition is not true, and is used e.g. for checking correct types of arguments.</div>
<div> </div>
<div>
</div>
<div>assert evaluates everything inside of it. Even if it doesnt continue and error the error message is still generated and evaluated. a simple if block only evaluates the if condition first and if that doesnt pass then it skips the code inside altogether. I do understand asserts an if statement is just faster</div>
<div> </div>
<div>
Quote
</div>
<div>MAJOR: avoid unpack //false, yes, it is better to avoid a function call if you know how many elements your table has, but unpack is pretty efficient (esp. when localized) and can be used on arrays of any length. Avoiding unpack can lead to hard-to-find bugs e.g. when you change the length of your array </div>
<div> </div>
<div>
</div>
<div>while being micro-optimization so i might remove the MAJOR in it this still is very true. In LuaJ according to speed tests unpack is extremely slow compared to not using it. consistently not using unpack pulls out a very massive speed DIFFERENCE. now both methods can be considered efficient because lua itself is very efficient. while the speed difference is radical between these since the speed in the first place is extremely low the noticeable speeds arent quite as radical </div>
<div> </div>
<div>
Quote
</div>
<div>Don't use math.max or math.min //true, due to extra function calls</div>
<div>WEAK: use or when handling nil evaluation //true, should be stated that</div>
<div>
</div>
<div>local x = a==nil and b or c</div>
<div>
</div>
<div>can also be used (will return b if a is nil, or c if b is nil too. If a is different from nil, will return c)</div>
<div>use multiplication instead of exponents //true</div>
<div>
</div>
<div>I will add your part to the suggestions on nil handling :3</div>
<div> </div>
<div>
Quote
</div>
<div>MAJOR: localize arguments(all readers should also focus on this one) //true</div>
<div>
</div>
<div>IRONICALLY i just tested this one again in the latest version(most of my tests are from one version back) as of the current computercraft version this tip is no longer true at all. Not even remotely true anymore and i need to update this very quickly. While in previous versions of computercraft this held true and in C++ versions of lua it still seems to be the current one sometimes saving the value as a local can increase the speed because of the time it took to save those values as locals and since theres no speed difference between passing a global or local through a function argument.</div>
<div> </div>
<div>
Quote
</div>
<div>MAJOR: avoid pairs() and ipairs() //false, why?? They're native and pairs can not be replaced, since it will also iterate on non-array elements</div>
<div>
</div>
<div>pairs() is faster than ipairs() and for i=1,x is faster than both. both use several functions behind the scenes especially if i remember right the next function. This function is written very inefficiently and iterates through the entire table each time it is run and it is run for each item in a table. In other words the larger your table is the slower pairs() and ipairs() become yet having very little effect on i=1,x</div>
<div> </div>
<div>
Quote
</div>
<div>buffer nested tables //true, while not very useful. Avoid nested tables if possible, to prevent the need for buffering</div>
<div>use a table for comparisons //true</div>
<div>
</div>
<div> </div>
<div>
Quote
</div>
<div>MAJOR: use internal buffers instead of iterators //eeh.. probably true but iterators aren't that bad. If you are having trouble, change your design. Thats all.</div>
<div>
</div>
<div>this is essentially a very minor version of memoization that im suggestiong. While iterators handle what they do very well memoization is a lot lot better and is considered very good coding practice and should be at least attempted for an understand at.</div>
<div> </div>
<div>
Quote
</div>
<div>I don't have time now but I will post more info on the topic when I do. While I was writing my message replied to the topic. He's a pro, listen to him.</div>
<div>
</div>
<div>It was actually SquidDev who told me how to get a very accurate profiler test set up for computercraft and first got me into running all of this.</div>
<div>
Quote
</div>
<div>There is no such thing as a GETDOTTED instruction. GETGLOBAL has two arguments, so none of the bytecode posted would ever work.</div>
<div> </div>
<div>
</div>
<div>wont argue on this one i was just using what i found on lua-users since you are right I am sure on the bytecode end of Lua you know more than I do</div>