Modern compilers are incredibly sophisticated, capable of transforming even the most inefficient code into highly
optimized machine instructions. Recursive algorithms, often seen as elegant yet potentially costly in terms of
performance, present a fascinating case study for these optimizations. From reducing function call overhead to
transforming recursion into iteration, compilers employ a range of techniques that balance developer productivity with
runtime efficiency.
In this article, we’ll explore how GCC optimizes recursive algorithms. We’ll examine key techniques such as tail-call
optimization, stack management, and inlining through a simple, easy to understand example. By the end, you’ll have a clearer
understanding of the interplay between recursive algorithms and compiler optimizations, equipping you to write code that
performs better while retaining clarity.
Factorial
The first example that we’ll look at is calculating a factorial.
This block of code is fairly simple. n is the factorial that we want to calculate with acc facilitating the
recursive processing that we’re looking to optimise.
-O0
First of all, we’ll compile this function with -O0 (no optimisation):
The compiler generates straightforward assembly that closely follows the original C code. No optimizations are applied
to reduce function call overhead or improve performance. You would use this level of optimisation (or lack thereof) in
situations where you might be debugging; and a straight-forward translation of your code is useful.
Stack operations (push, mov, sub, etc.) are explicitly performed for each recursive call. This results in the
largest amount of assembly code and higher function call overhead.
-O1
Next, we’ll re-compile this function at -O1 which will give us basic optimisations:
The first thing to notice here is the stack management at the start of the function.
-O0:
The stack frame is explicitly set up and torn down for every function call, regardless of whether it is needed. This
includes saving the base pointer and reserving 16 bytes of stack space.
We then have slower execution due to redundant stack operations and higher memory overhead.
-O1:
The stack frame is more compact, reducing overhead. The base pointer (%rbp) is no longer saved, as it’s not strictly
necessary. This give us reduced stack usage and faster function calls
Next up, we see optimisations around tail-call optimisation (TCO).
-O0:
Recursive calls are handled traditionally, with each call creating a new stack frame.
-O1:
While -O1 still retains recursion, it simplifies the process by preparing for tail-call optimization. Unnecessary
operations before and after the call are eliminated.
We also see some arithmetic simplification between the optimisation levels:
-O0:
Arithmetic operations explicitly load and store intermediate results in memory, reflecting a direct translation of the
high-level code.
-O1:
Intermediate results are kept in registers (%eax, %edi), avoiding unnecessary memory access.
There’s also some instruction elimination between the optimisation levels:
-O0:
Each variable is explicitly loaded from the stack and moved between registers, leading to redundant instructions.
-O1:
The compiler identifies that some operations are unnecessary and eliminates them, reducing instruction count.
We finish off with a return path optimisation.
-O0:
Explicit leave and ret instructions are used to restore the stack and return from the function.
-O1:
The leave instruction is eliminated as it’s redundant when the stack frame is managed efficiently.
With reduced stack overhead and fewer instructions, the function executes faster and consumes less memory at -O1
compared to -O0. Now we’ll see if we can squeeze things even further.
-02
We re-compile the same function again, turning optimisations up to -O2. The resulting generated code is this:
First we see some instruction-level parallelism here.
-O2 introduces techniques that exploit CPU-level parallelism. This is visible in the addition of the lea (load
effective address) instruction and conditional branching.
-O1:
-O2:
At -O2, the compiler begins precomputing values and uses lea to reduce instruction latency. The conditional branch
(test and jne) avoids unnecessary function calls by explicitly checking the termination condition.
Next, we see the compiler partially does some loop unrolling
-O1 Recursion is preserved:
-O2 Loop structure replaces recursion:
The recursion is transformed into a loop-like structure that uses the jne (jump if not equal) instruction to iterate
until the base case is met. This eliminates much of the overhead associated with recursive function calls, such as
managing stack frames.
More redundant operations removed from the code. Redundant instructions like saving and restoring registers are
removed. This is particularly noticeable in how the return path is optimized.
-O1:
-O2:
-O2 eliminates the need for stack pointer adjustments because the compiler reduces the stack usage overall.
Finally, we see some more sophisticated conditional simplifications.
-O1:
-O2:
Instead of jumping to a label and performing additional instructions, -O2 jumps directly to the return sequence
(2e <factorial+0x2e>). This improves branch prediction and minimizes unnecessary operations.
These transformations further reduce the number of instructions executed per recursive call, optimizing runtime
efficiency while minimizing memory footprint.
-O3
When we re-compile this code for -O3, we notice that the output code is identical to -O2. This suggests that the
compiler found all of the performance opportunities in previous optimisation levels.
This highlights an important point: not all functions benefit from the most aggressive optimization level.
The factorial function is simple and compact, meaning that the optimizations applied at -O2 (tail-recursion
transformation, register usage, and instruction alignment) have already maximized its efficiency. -O3 doesn’t
introduce further changes because:
The function is too small to benefit from aggressive inlining.
There are no data-parallel computations that could take advantage of SIMD instructions.
Loop unrolling is unnecessary since the tail-recursion has already been transformed into a loop.
For more complex code, -O3 often shines by extracting additional performance through aggressive heuristics, but in
cases like this, the improvements plateau at -O2.
Conclusion
Recursive algorithms can often feel like a trade-off between simplicity and performance, but modern compilers
significantly narrow this gap. By employing advanced optimizations such as tail-call elimination, inline expansion, and
efficient stack management, compilers make it possible to write elegant, recursive solutions without sacrificing runtime
efficiency.
Through the examples in this article, we’ve seen how these optimizations work in practice, as well as their limitations.
Understanding these techniques not only helps you write better code but also deepens your appreciation for the compilers
that turn your ideas into reality. Whether you’re a developer crafting algorithms or just curious about the magic
happening behind the scenes, the insights from this exploration highlight the art and science of compiler design.
Visual effects like water droplets are mesmerizing, and they showcase how simple algorithms can produce complex, beautiful
animations. In this article, I’ll walk you through creating a water droplet effect using VGA mode 13h.
We’ll rely on some of the code that we developed in the VGA routines from Watcom C
article for VGA setup and utility functions, focusing on how to implement the effect itself.
The effect that we’re looking to produce should look something like this:
The Idea
The water droplet effect simulates circular ripples spreading out from random points on the screen. Here’s the high-level approach:
Drops: Represent each drop with a structure containing its position, energy, and ripple generation.
Drawing Ripples: Use trigonometry to create circular patterns for each ripple generation.
Blur Effect: Smooth the buffer to simulate water’s fluid motion.
Palette: Set a blue-themed palette to enhance the watery feel.
Setting the Water Palette
First, we set a blue-tinted gradient palette. Each color gradually transitions from dark blue to bright blue.
Representing Drops
Each drop is represented by a structure that tracks:
(x, y): The origin of the drop.
e: Energy, which fades with time.
g: Current ripple generation.
Creating and Advancing Drops
Drops are reset with random positions, maximum energy, and zero ripple generation:
Each frame, we reduce the drop’s energy and increment its generation. When energy is exhausted, the drop stops
producing ripples:
Drawing Ripples
Ripples are drawn using polar coordinates. We calculate x and y offsets using cosine and sine functions for each
angle and scale by the current generation.
The colour that is rendered to the buffer is additive. We take the current colour at the pixel position, and add to it
giving the droplets a sense of collision when they overlap.
Simulating Fluid Motion
A blur effect smooths the ripples, blending them into neighboring pixels for a more fluid appearance. This is done by
averaging surrounding pixels.
Main Loop
The main loop handles:
Adding new drops randomly.
Advancing and drawing existing drops.
Applying the blur effect.
Rendering the buffer to the VGA screen.
Conclusion
This water droplet effect combines simple algorithms with creative use of VGA mode 13h to create a visually stunning effect. By leveraging circular ripples, energy fading, and a blur filter, we replicate the mesmerizing motion of water.
You can find the complete code on GitHub as a gist.
Try it out, tweak the parameters, and share your own effects! There’s a lot of joy in creating beautiful visuals with minimal resources.
The VGA era was all about getting the most out of limited hardware. It required clever tricks to push pixels and make
things move. To make it easier to work with VGA and related concepts, I put together a library called freak.
This library includes tools for VGA handling, keyboard input, vector and matrix math, and
fixed-point math. In this post, I’ll go through each part of the library and explain how it works, with examples.
Be warned - this stuff is old! You’ll need a Watcom Compiler as well as a dos-like environment to be able to run any
of your code. I’ve previously written about getting Watcom up and running with DosBox. If you want to get this running
you can read the following:
The code that this article outlines is available here.
Video routines
First of all, we’re going to take care of shifting in and out of old mode 13.
Setting a video mode
To shift in and out of video modes we use the int 10h bios interrupt.
Passing the video mode into al and setting ah to 0 allows us to change modes.
We also need to define where we want to draw to. VGA maps to A000:0000 in real mode. Because we’re in protected mode
(thanks to DOS/4G) we set our pointer to 0xA0000.
Working with buffers
We defined the pointer freak_vga as a location in memory. From that point in memory for the next 64,000 bytes (we’re
using 320x200x8 which is 64k) are all of the pixels on the screen.
That means we can treat the screen like any old memory buffer. That also means that we can define virtual buffers as
long as we have 64k to spare; which we do.
You could imagine doing something like this pretty easily:
We could use memset and memcpy to work with these buffers; or we would write our own optimised implementations to
use instructions to move a double at a time (like movsd and stosd):
Before flipping a back buffer onto the vga surface, we wait for the vsync to complete. This removes any flicker.
Colours
In mode13, we are given 256 colour slots to where we can control the red, green, and blue component. Whilst the default
palette does provide a vast array of different colours; it kinda sucks.
In order to set the r, g, b components of a colour we first need to write the colour index out to port 0x3c8. We then
write the r, g, and b components sequentially out to 0x3c9.
To read the current r, g, b values of a colour slot, we write the colour index out to 0x3c7. Then we can sequentially
read the values from 0x3c9.
Keyboard
Working with the keyboard is another BIOS service in 16h that we can call on.
Simple re-implementations of getch and kbhit can be produced with a little assembly language:
Fixed point math
The fixed point article that I had previously written walks you through
the basic mechanics of the topic. The bit lengths of the whole and fractional parts are pretty small; and unusable. So
we’re going to use this technique, but scale it up.
Conversions
First of all, we need to be able to go from the “C type world” (the world of int and double, for instance) into the
“fixed point world”. We also need to make our way back:
These macros as just simple helpers to clean up our code when defining numbers.
Constants
Next up, we define some important constants:
Each of these comes in handy for different mathematical operations that we’ll soon walk through.
Trig
We need trigonometry to do fun stuff. To speed up our code, we pre-compute a sine and cosine table that is already in
our fixed-point number format:
Our trig tables are based around a nerd number of 1,024 making this a little easier to reason about and giving us an
acceptable level of precision between fractions of radians for what we need.
These are then nicely wrapped up in macros.
Operations
The fixed multiply is a very simple integer-based operation (by design):
Division is also quite similar:
Square roots come in two flavours. A quicker by less precise version (“fast”) or the longer iterative approach.
Finally, some helpers that are usages of existing code that we’ve written are squaring a number, and putting a number
under 1:
3D
There a primer on Basic 3D that I have done previously that goes into deeper
information around 3D mathematics, and more.
Vectors
A 3-space vector has an x, y, and z component.
\[\mathbf{v} = \begin{bmatrix} x \\ y \\ z \end{bmatrix}\]
For convenience, we define ths in a union so that it can be addressed
using the x, y, and z members or as an array through the v member:
Setting an zero’ing out one of these structures is really just a basic data-movement problem:
Basic arithmetic is achieved using the fixed math primitives defined earlier:
To rotate around an arbitrary axis \(\mathbf{a} = \begin{bmatrix} a_x \\ a_y \\ a_z \end{bmatrix}\) by an angle \(\theta\),
the rotation matrix is defined as:
The freak library is my attempt to distill the essence of classic VGA programming into a modern, accessible toolkit. By
combining essential building blocks like graphics handling, input, and math operations, it provides everything you need
to recreate the magic of the demoscene or explore retro-style programming.
I hope this article inspires you to dive into the world of low-level programming and experiment with the techniques that
defined a generation of creativity. Whether you’re building your first polygon renderer or optimizing an effect with
fixed-point math, freak is here to make the journey both rewarding and fun.
Let me know what you think or share what you build—there’s nothing quite like seeing new creations come to life with
tools like these!
Sometimes, you need to squeeze more performance out of your Python code, and one great way to do that is to offload some of your CPU-intensive tasks to an extension. Traditionally, you might use a language like C for this. I’ve covered this topic in a previous post.
In today’s post, we’ll use the Rust language to create an extension that can be called from Python. We’ll also explore the reverse: allowing your Rust code to call Python.
Setup
Start by creating a new project. You’ll need to switch to the nightly Rust compiler:
Next, ensure pyo3 is installed with the extension-module feature enabled. Update your Cargo.toml file:
Code
The project setup leaves you with a main.rs file in the src directory. Rename this to lib.rs.
Now, let’s write the code for the extension. In the src/lib.rs file, define the functions you want to expose and the module they will reside in.
First, set up the necessary imports:
Next, define the function to expose:
This function simply returns the string "Hello, world!".
The #[pyfunction] attribute macro exposes Rust functions to Python. The return type PyResult<T> is an alias for Result<T, PyErr>, which handles Python function call results.
Finally, define the module and add the function:
The #[pymodule] attribute macro defines the module. The add_wrapped method adds the wrapped function to the module.
Building
With the code in place, build the module:
Once built, install it as a Python package using maturin. First, set up a virtual environment and install maturin:
Now, build and install the module:
The develop command that we use here builds our extension, and automatically installs the result into our virtual
environment. This makes life easy for us during the development and testing stages.
Testing
After installation, test the module in Python:
Success! You’ve called a Rust extension from Python.
Python from Rust
To call Python from Rust, follow this example from the pyo3 homepage.
Create a new project:
Update Cargo.toml to include pyo3 with the auto-initialize feature:
Here is an example src/main.rs file:
Build and run the project:
You should see output similar to:
Conclusion
Rewriting critical pieces of your Python code in a lower-level language like Rust can significantly improve performance. With pyo3, the integration between Python and Rust becomes seamless, allowing you to harness the best of both worlds.
In a previous post we covered the basic setup on
drawing to a <canvas> object via WebAssembly (WASM). In today’s article, we’ll create animated graphics directly on a
HTML5 canvas.
We’ll break down the provided code into digestible segments and walk through each part to understand how it works. By
the end of this article, you’ll have a clear picture of how to:
Set up an HTML5 canvas and interact with it using Rust and WebAssembly.
Generate random visual effects with Rust’s rand crate.
Build an animation loop with requestAnimationFrame.
Use shared, mutable state with Rc and RefCell in Rust.
Let’s get started.
Walkthrough
I won’t cover the project setup and basics here. The previous post
has all of that information for you. I will cover some dependencies that you need for your project here:
There’s a number of features in use there from web-sys. These will become clearer as we go through the code. The
getrandom dependency has web assembly support so
we can use this to make our animations slightly generative.
Getting Browser Access
First thing we’ll do is to define some helper functions that will try and acquire different features in the browser.
We need to be able to access the browser’s window object.
This function requests the common window object from the Javascript environment. The expect will give us an error
context if it fails, telling us that no window exists.
The function being requested here is documented as the callback.
The window.requestAnimationFrame() method tells the browser you wish to perform an animation. It requests the browser to call a user-supplied callback function before the next repaint.
This will come in handy to do our repaints.
Now, in our run function, we can start to access parts of the HTML document that we’ll need references for. Sitting in
our HTML template, we have the <canvas> tag that we want access to:
We can get a handle to this <canvas> element, along with the 2d drawing context with the following:
Create our Double-Buffer
When we double-buffer graphics, we need to allocate the block of memory that will act as our “virtual screen”. We draw
to that virtual screen, and then “flip” or “blit” that virtual screen (piece of memory) onto video memory to give the
graphics movement.
The size of our buffer will be width * height * number_of_bytes_per_pixel. With a red, green, blue, and alpha channel
that makes 4 bytes.
Animation Loop
We can now setup our animation loop.
This approach allows the closure to reference itself so it can schedule the next frame, solving Rust’s strict ownership
and borrowing constraints.
This pattern is common in Rust for managing shared, mutable state when working with closures in scenarios where you need
to reference a value multiple times or recursively, such as with event loops or callback-based systems. Let me break it
down step-by-step:
The Components
Rc(Reference Counted Pointer):
Rc allows multiple ownership of the same data by creating a reference-counted pointer. When the last reference to the data is dropped, the data is cleaned up.
In this case, it enables both f and g to share ownership of the same RefCell.
RefCell(Interior Mutability):
RefCell allows mutable access to data even when it is inside an immutable container like Rc.
This is crucial because Rc itself does not allow mutable access to its contents by design (to prevent race conditions in a single-threaded context).
Closure:
A closure in Rust is a function-like construct that can capture variables from its surrounding scope.
In the given code, a Closure is being stored in the RefCell for later use.
What’s Happening Here?
Shared Ownership:
Rc is used to allow multiple references (f and g) to the same underlying RefCell. This is required because the closure may need to reference f while being stored in it, which is impossible without shared ownership.
Mutation with RefCell:
RefCell enables modifying the underlying data (None → Some(Closure)) despite Rc being immutable.
Setting the Closure:
The closure is created and stored in the RefCell via *g.borrow_mut().
This closure may reference f for recursive or repeated access.
We follow this particular pattern here because the closure needs access to itself in order to recursively schedule calls
to requestAnimationFrame. By storing the closure in the RefCell, the closure can call itself indirectly.
If we didn’t use this pattern, we’d have some lifetime/ownership issues. Referencing the closure while defining it
would create a circular reference problem that Rust wouldn’t allow.
Drawing
We’re going to find a random point on our virtual screen to draw, and we’re going to pick a random shade of grey. We’re
going to need a random number generator:
We get a random location in our virtual screen, and calculate the offset o to draw at using those values.
Now, it’s as simple as setting 4 bytes from that location:
Blitting
Blitting refers to copying pixel data from the backbuffer to the canvas in a single operation. This ensures the displayed
image updates smoothly
Now we need to blit that back buffer onto our canvas. We need to create an ImageData object in order to do this.
Passing in our backbuffer object, we can create one with the following:
We then use our 2d context to simply draw the image:
Conclusion
And there you have it—a complete walkthrough of creating dynamic canvas animations with Rust and WebAssembly! We covered
how to:
Set up the canvas and prepare a backbuffer for pixel manipulation.
Use Rust’s rand crate to generate random visual effects.
Manage mutable state with Rc and RefCell for animation loops.
Leverage requestAnimationFrame to achieve smooth, frame-based updates.
This approach combines Rust’s strengths with the accessibility of modern web technologies, allowing you to build fast,
interactive graphics directly in the browser.