General language techniques - arbuxsrc/replicube-techniques GitHub Wiki

There are a number of techniques that help reduce size and/or speed for your solutions.

Comments cost nothing for speed or size

Complex logic / explanations can be included in comments in the solution without impacting speed or size.

-- Single line comment

--[[
     Multi-line comment
]]

Debugging

You can get debug information by using the print function:

print(x*y)

and if you want info on a specific cell, you can get this using a basic if statement

if x==0 and y==0 and z==0 then print("whatever you want") end

Removing If Then End tokens

Not particularly great for speed, but Lua has a short cut

if x > 0 and y < 1 then return 2 else return 3 end -- tokens: 15, speed: 4.429

is equivalent to:

return x > 0 and y < 1 and 2 or 3 -- tokens: 12, speed: 4.673

Short Circuit And and Or evaluation

Lua has short circuit logic for and and or so will only evaluate the necessary parts of the equation. Changing the order of the x any y checks above keeps the same speed, but speeds up the evaluation:

if y < 1 and x > 0 then return 2 else return 3 end -- tokens: 15, speed: 4.571

To get best outcomes speed wise, without changing size:

  • Use the most likely failure conditions first for any And operators
  • Use the most likely passing conditions first for any Or operators

If you must then elseif short circuit

Elseif is a built in short circuit operator for steps that shouldn't overlap.

In the following code, x==-y will only be evaluated if x==y is not evaluated. The speed is 4.980 for this versioon

if x==y then
	return 1
elseif x==-y then
	return 2
end

Note that the following is slower (speed:5.122. This is because there is one extra command to be processed "-" in "x==-y" in each pass.

if x==-y then
	return 2
elseif x==y then
	return 1
end

Working with nil

The preference is not to.

  • Returning nil from the code is the same as returning 0
  • nil can't have math functions performed on them so require additional steps (performance and size issue) to check and convert
  • nil breaks the and or functionality

Working with infinities

With copious amounts of 0-value coordinates, get to know how lua deals within infinite values

  • Wrapping a +inf/-inf in a sign() function will return 1 for +inf and -1 for -inf so will change the shape from blanks (where the infinities are calculated) to values again (1,-1)

Get the LUA version

While not exposed, standard Lua functionality, including a default import of the math library, is available in the interface.

For example, the following script prints out the Lua version in the debug window

if x==0 and y==0 and z==0 then print (_VERSION) end
CleanShot 2025-07-18 at 21 31 20@2x

Using functions

Functions are relatively expensive to use. The minimum size for a function declaration is 6 and a minimum size for calling of 2. Function declarations and function calls also cause a slowdown of speed.

That said, repeated calculations may offset the size rather than a copy and paste.

function a(_x,_y,_z) return _x*_y*_z end
return a(z,y,x);

For Replicube, use local in front of the function declaration for slightly faster performance, but with 1 code size tradeoff

local function a() return 1 end
return a()

They're all just 64-bit numbers

You can use binary and shift operators on values. Just be wary that you will get a code-size hit if you use numbers > 16 in your code.

It's still worth knowing binary operators, but be careful using them on -ve numbers

Use Floor Division

The following enables rounding to the floor (smaller integer) for division:

return x//2

Use modulus to get alternating patterns

return x%2

What's cool: the % sign doesn't care about the sign of the left hand value. The sign of the result will be based on the right hand side (which can be negative or positive)

Use xor to flick between two numbers

--Alternates between two green colours
return  10 ~ (x % 2)
CleanShot 2025-07-18 at 23 05 18@2x
-- Alternates between colours 10 (light green) and 14 (light blue)
return  10 ~ 4*(x % 2)
CleanShot 2025-07-18 at 23 06 39@2x
⚠️ **GitHub.com Fallback** ⚠️