Stress-testing Céu with SDL
For a glimpse at the runtime costs of Céu, I created a simple application with SDL that animates 10000 rectangles on the screen.
My hypothesis is that lines of execution in Céu (a.k.a trails) are cheap and should be used carelessly, even for simple activities such as waiting for a single event and terminating. This way, the testing application in Céu has a peek of 40000 trails active at the same time, which are all visited by the scheduler on every frame.
I compare the implementation in Céu with a simpler application in C to serve as a benchmark.
The application in raw C is damn simple:
- Declares a vector of 10000 rectangles.
- Initializes each to 10×10 width/height, a random (x,y) position, and a random x velocity.
- Animates them continuously, restarting from left when reaching the right side of the screen.
- Achieved FPS = 120 .
- The program has 5 lines of execution:
- On SDL_QUIT event: terminates the application.
- Every frame: clears the screen.
- Every 40ms: creates 30 new Rect organisms.
- after 30s: kills all of them at once and restart the process
- Every frame: updates the screen.
- Every second: calculates the FPS.
(technically they are 7 lines of execution because (3) and (5) are split in two)
- The class Rect has 4 lines of execution:
- Every 200ms: varies the rectangle width, height, r, g, b (with a random epsilon).
- Every frame: animates the rectangle respecting its velocity, terminating when it reaches the end of the screen.
- Every frame: redraws the rectangle on screen.
- On termination: runs a simple finalizer (which maintains the number of organisms alive).
- Achieved FPS = 107 . (when 10000 rectangles are on screen)
The main differences between the two implementations are marked in bold above and are summarized as follows:
- The rectangles in Céu are allocated dynamically: in the peek, we have 750 rectangles being allocated (in the left side) and reclaimed (in the right side) every second.
- Each rectangle on screen (10000 at most) has 4 active trails, all of which are visited by the scheduler every frame (4 x 10000 = 40000 lightweight threads active at the same time).
- Finally, every 30 seconds the program restarts the spawning loop, killing all 10000 active organisms at once, running the associated finalizers, and reclaiming all memory.
Céu is a source-to-source compiler that generates single-threaded ansi-C code. This way, the generated code for the test application, although incomprehensible, can be compiled with gcc and tested in any computer that has SDL2:
% gcc download_code.c -lSDL2 .
The simple SDL application in C is used as a benchmark to compare to a more complex application written in Céu, which shows a decrease around 10% in FPS.
Although the application in C could be re-implemented to achieve the same functionality (for a more strict comparison), the initial numbers are already satisfactory and make the point that the trail-oriented programming style of Céu is realistic, even in a highly dynamic scenario.