LLMs are great at deoptimizing code for readability - Example with Lua

As I recently wrote, I'm enjoying the Replicube PC/macOS videogame in more than one way, especially since it allows you to code using Lua. I had the initial idea of storing my solutions in a GitHub repository, but then it grew as a place to also store custom creations, like my 8-bit Zelda character [1]:

8-Bit Link from Zelda

I then decided to write down the (few) Lua optimization techniques I use to reduce the voxel render instructions count and/or cycles, in a simple markdown, inside the same repository.

Today I thought: LLMs are superb both at applying instructions and explaining existing code... So why don't I create a Cursor rule to deoptimize on demand any of my puzzle solutions into human-readable code? After an initial (AI-generated) version and a few revision rounds, I think that the resulting rule, replicube-deoptimization-rule.mdc, is good enough:

Run the @replicube-deoptimization-rule.mdc rule with the @5-little-chair.lua file

Converts from:

-- less code carving out (from last block) than drawing
return y > -1 and z > -1 and 0
    or x == 0 and y ~= -1 and y ~= 2 and 0
  or y < -1 and z == 0 and 0
  or x*x < 2 and y < 3 and z*z < 2 and 8

To:

-- This code uses carving approach - removing voxels from a solid block
-- rather than drawing individual parts

-- Carve out the top and front area (make it empty)
if y > -1 and z > -1 then
  return EMPTY -- Remove top-front area
end

-- Carve out the back support (except at specific heights)
if x == 0 and y ~= -1 and y ~= 2 then
  return EMPTY -- Remove back support middle section
end

-- Carve out the seat area from below
if y < -1 and z == 0 then
  return EMPTY -- Remove under-seat area
end

-- Keep the chair structure (legs, seat, backrest)
local x_abs = abs(x)
local z_abs = abs(z)

if x_abs < 2 and y < 3 and z_abs < 2 then
  return ORANGE -- Chair material
end

return EMPTY -- Everything else is empty space

Way easier to see whether the chair is correctly built or not, right? 😅

Replicube chair

I tried generating few deoptimized challenge solutions and running them through Replicube, and all of them work perfectly. I committed a few of them, you can look for the -readable suffixed Lua files in the repository.

Conclusion

As exemplified, LLMs excel at explaining existing code. They do not seem to hallucinate, and while this was a simple example, I've done way more complex experiments of both summarizing and unravelling source code with great success.

In this case, the solution is not perfect, as Cursor tends to ignore the directive of not extracting to a variable when there is a single occurrence. However, we're theoretically optimizing for readability, so I'll let the agent win this time. Probably iterating on the rule contents and/or adding some directive priorities would help steer it better.

Notes

[1]: At least for now, my trick to "draw" complex 3D images is to use a multidimensional Lua table to lay out the voxels one "slice" at a time, then simply calculate the table position to render (based on the coordinates) and return it.

Tags: Development Game Dev Lua ML & AI Tools Videogames

LLMs are great at deoptimizing code for readability - Example with Lua article, written by Kartones. Published on