Logo

This Week in Mojo 2023-07-28

Playground Release

Full changelog hereopen in new window

⭐️ New

Types that define both __getitem__ and __setitem__ (i.e. where sub-scripting instances creates computed LValues) can now be indexed in parameter expressions. Unroll decorator for loops with constant bounds and steps:

  • @unroll: Fully unroll a loop.
  • @unroll(n): Unroll a loop by factor of n, where n is a positive integer.

Unroll decorator requires loop bounds and iteration step to be compiler time constant value, otherwise unrolling will fail with compilation error. This also doesn’t make loop induction variable a parameter.

Fully unroll the loop:

@unroll
for i in range(5):
    print(i)

Unroll the loop by a factor of 4 (with remainder iterations of 2):

@unroll(4)
for i in range(10):
    print(i)

The Mojo REPL now prints the values of variables defined in the REPL. There is full support for scalars and structs. Non-scalar SIMD vectors are not supported at this time.

🛠️ Fixed

Community Content

Mojo Team Answers

Loop Unrolling

These are two loop decorators to tell the compiler to unroll a loop, see wikipedia loop unrollingopen in new window. This doesn't impact the functionality of the loops, but potentially can help for better performance since it opens possibility for further compiler optimizations.

Fully unroll the loop's 10 iterations into 10 do_something calls and remove the for-loop:

@unroll 
for i in range(10):
  do_something(i)

Unroll every 2 iterations and loop over 5 times:

@unroll(2)
for i in range (10):
  do_something(i)

This decorator can be attached to while statement too.

Note that currently the compiler can only unroll a loop:

  • Its lower bound, upper bound and induction variable step every iteration are compile time constants
  • There is no early exits in the loop body that makes the loop trip count dynamic during runtime.

Otherwise, Compilation fails if a loop is decorated with @unroll

Here is a brief description of these two decorators in Mojo changelog on 2023-07-26open in new window.

Functional.unrollopen in new window performs the same loop unrolling functionality as library functions. There are a few differences between using library function of unroll and decorator @unroll are:

  • Library function call requires the induction variable to be a parameter while the decorator uses the induction variable as a dynamic variable.
  • Library function call unroll the loop so that the program the compiler starts to compile is with unrolled code. The can potentially increase the amount of code to compile depends on the amount to unroll.
  • Decorator unrolling happens at later stage of compilation which prevents program explosion too early.

-2023-07-28 Github Weiwei Chenopen in new window

JSON Parser

JSON is super important, but right now we are tracking more basic things, e.g. getting core data structures in place. Higher level libraries like this should likely be part of the broader package ecosystem, and we need packages before we plan that 😃