# fillTriangle() results huge binary size?

So i’m on an optimization hunt, since my game is getting near the limit, while i have discovered this weird thing to me.

I have a draw function wich draws 6 triangles which i use as an animated background.
When i remove the `arduboy.fillTriangle()` method the filesize drops one hole KB! From 27416 bytes to 26324 bytes.

This can’t be normal and if it is pls explain. Is it the same for the other draw methods? i want to learn to avoid mistakes like this and safe file size.
Here’s my code:

``````struct ABPoint{
int x;
int y;
};

static inline ABPoint ABPointMake (int x, int y){
return {x, y};
}

void drawTriangles(ABPoint offset, byte animator){
animator = animator%3;

ABPoint origin = ABPointMake(24 + offset.x, 16 + offset.y);
ABPoint a, b;

// first triangle
a = ABPointMake(8 + offset.x + (animator*8), 0 + offset.y);
b = ABPointMake(24 + offset.x + (animator*8), 0 + offset.y);

//arduboy.fillTriangle(origin.x, origin.y, a.x, a.y, b.x, b.y, WHITE);

// 2nd triangle
a = ABPointMake(32 + offset.x + (animator*8), 0 + offset.y);
b = ABPointMake(48 + offset.x + (animator*8), 0 + offset.y);

//arduboy.fillTriangle(origin.x, origin.y, a.x, a.y, b.x, b.y, WHITE);

// 3rd triangle
a = ABPointMake(64 + offset.x, 0 + offset.y + (animator*8));
b = ABPointMake(64 + offset.x, 16 + offset.y + (animator*8));

//arduboy.fillTriangle(origin.x, origin.y, a.x, a.y, b.x, b.y, WHITE);

// 4th triangle
a = ABPointMake(48 + offset.x - (animator*8), 32 + offset.y);
b = ABPointMake(64 + offset.x - (animator*8), 32 + offset.y);

//arduboy.fillTriangle(origin.x, origin.y, a.x, a.y, b.x, b.y, WHITE);

// 5th triangle
a = ABPointMake(16 + offset.x - (animator*8), 32 + offset.y);
b = ABPointMake(32 + offset.x - (animator*8), 32 + offset.y);

//arduboy.fillTriangle(origin.x, origin.y, a.x, a.y, b.x, b.y, WHITE);

// 6th triangle
a = ABPointMake(0 + offset.x, 16 + offset.y - (animator*8));
b = ABPointMake(0 + offset.x, 32 + offset.y - (animator*8));

//arduboy.fillTriangle(origin.x, origin.y, a.x, a.y, b.x, b.y, WHITE);
}
``````

##Update:
If i use the method only once (uncomment the first occurrence) it already jumps up to 27100 bytes…

Using this minimal sketch (which doesn’t do anything useful except for testing):

``````#include <Arduboy.h>

Arduboy arduboy;

void setup() {
arduboy.begin();
}

void loop() {
arduboy.fillTriangle(0,0, 10,0, 15,5, WHITE);
arduboy.fillTriangle(10,10, 20,10, 25,15, WHITE);
}
``````

Commenting out both fillTriangle() calls results in a size of 10452.

Then, uncommenting only the first fillTriangle() results in size 11292
which is an extra 840 bytes.
This makes sense since it’s the first time the fillTriangle() function has been called, so it has to pull in and compile it, along with any functions that it uses that haven’t previously been used.

Uncommenting the second fillTriangle() results in size 11328
which is an extra 36 bytes. This would be the code to set up the 7 parameters to be passed and call the fillTriangle() function. This test case is passing constants to the function. Doing calculations to determine the values of the parameters passed, and using parameters more complex than constants, would increase the amount of code needed perform the function.

2 Likes

Wow this is heavy. Didn’t know that it costs that much!
Really hard to optimize for small filesize…

1 Like

fillTriangle() does a lot of things to make it work in a number of situations. For instance, it does testing and calculations so it can accept the three vertices in any order.

If the triangles you’re drawing are limited, and the parameters can be controlled, for example you always pass the highest vertex first, followed by the next highest, you may be able to write your own fillTriangle() function that uses less code, by not having to perform some of the checks and calculations that a generic fillTriangle() requires.

There’s also the possibility that the library fillTriangle(), and other functions, could be optimized to produce smaller code. Feel free to submit any library optimizations that you come up with.

1 Like