0Pricing
Mojo Academy · Lesson

Compile-Time Loop Unrolling

Expand loops with @parameter for.

Compile-Time Loop Unrolling is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Loops the Compiler Expands

Loop unrolling means the compiler writes out each iteration as straight-line code, removing the loop's counting and branching overhead. ⚙️

The @parameter for Decorator

Put @parameter on a for loop whose range is known at compile time, and Mojo unrolls it fully instead of running it at runtime.

@parameter
for i in range(4):
    print(i)

The Bound Must Be Static

The loop range has to be a compile-time value, so the compiler knows exactly how many copies of the body to generate.

alias N = 8

@parameter
for i in range(N):
    process(i)

The Index Becomes a Constant

In an unrolled loop the index is a true parameter in each copy, so you can use it where only compile-time values are allowed.

@parameter
for i in range(4):
    arr[i] = i * i

Why Unroll at All?

Unrolling drops the per-iteration branch and counter work, giving the CPU a clean run of instructions it can pipeline efficiently.

It Pairs With SIMD

Unrolled bodies often feed SIMD operations, letting each generated copy handle one vector lane group for maximum throughput.

Indexing SIMD Lanes

A compile-time index lets you reach individual lanes of a SIMD vector, something a runtime loop variable cannot always do.

@parameter
for i in range(4):
    total += vec[i]

Bigger Code, Faster Runs

Unrolling trades a larger binary for fewer instructions executed, a worthwhile deal in tight hot loops that run millions of times.

Don't Unroll Everything

Reserve unrolling for small, hot inner loops. Unrolling huge ranges bloats code and can hurt instruction-cache performance.

A Compile-Time Tool

@parameter for is pure metaprogramming: the loop never exists at runtime because the compiler already expanded it into flat code. 🚀

Pair It With Constants

Define the bound with an alias so one named constant drives the unroll count, keeping the code clear and easy to retune later.

alias TILE = 4

@parameter
for i in range(TILE):
    work(i)

Quick Check

Recall what @parameter does to a for loop.

Recap

@parameter for unrolls statically-bounded loops at compile time: the index becomes a constant, overhead vanishes, and hot loops run faster. 🎯

Frequently asked questions

Is the “Compile-Time Loop Unrolling” lesson free?

Yes — the full text of “Compile-Time Loop Unrolling” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Compile-Time Loop Unrolling”?

Expand loops with @parameter for. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Compile-Time Loop Unrolling” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Parametric Algorithms
  2. Compile-Time Loop Unrolling
  3. Conditional Compilation
  4. Constraints and Static Checks
← Back to Mojo Academy