Programming for Robotics with Arduino C++ 9, Challenge 1 solution (predict the RAM) Idea: from the array sizes in grid.h, x1.cpp computes the RAM each variant of the size-test sketch should use, and the same figure from sizeof of the real classes. The second pair is the compiler's own report for the four variants, the third is avr-nm's listing of the biggest RAM symbols of variant 2. CODE: // x1.cpp -- Challenge 1: predict the RAM of a planner before compiling it (host build). // From the sizes of the arrays in grid.h (BitGrid: ceil(cells/8) bytes; Bfs: seen bits + 2 bits per cell + 2 bytes per queue entry + 4 bytes of counters; // AStar: closed bits + 1 byte per cell + 2 bits per cell + 4 bytes per heap entry + 6 bytes of counters), predict the "Global variables use" figure of the four variants of the size-test sketch // s_plan.ino: the baseline sketch alone is 188 bytes (Serial's buffers and the timer), the path buffer is 100 cells x 2 bytes, the 30 x 20 map is read from flash (0 bytes of RAM), // the 16 x 16 map is a BitGrid in RAM. Then the same numbers from the real classes with sizeof. #include #include "grid.h" int main() { const unsigned base = 188, path = 200; auto bits = [](unsigned cells) { return (cells + 7) / 8; }; auto quarter = [](unsigned cells) { return (cells + 3) / 4; }; unsigned v1 = base + bits(600) + quarter(600) + 2 * 40 + 4 + path; unsigned v2 = base + bits(600) + 600 + quarter(600) + 4 * 192 + 6 + path; unsigned v3 = base + bits(256) + 256 + quarter(256) + 4 * 97 + 6 + bits(256) + path; std::printf("variant predicted RAM from sizeof (same arrays)\n"); std::printf(" 0 no planner %13u %25u\n", base, base); std::printf(" 1 BFS 30 x 20, queue 40, map in flash %13u %25u\n", v1, base + (unsigned)sizeof(Bfs<30, 20, 40>) + path); std::printf(" 2 A* 30 x 20, heap 192, map in flash %13u %25u\n", v2, base + (unsigned)sizeof(AStar<30, 20, 192>) + path); std::printf(" 3 A* 16 x 16, heap 97, map in RAM %13u %25u\n", v3, base + (unsigned)sizeof(AStar<16, 16, 97>) + (unsigned)sizeof(BitGrid<16, 16>) + path); std::printf("the Uno has 2048 bytes: variant 2 leaves %u for the stack and everything else\n", 2048 - v2); return 0; } END CODE OUTPUT: variant predicted RAM from sizeof (same arrays) 0 no planner 188 188 1 BFS 30 x 20, queue 40, map in flash 697 698 2 A* 30 x 20, heap 192, map in flash 1987 1988 3 A* 16 x 16, heap 97, map in RAM 1166 1166 the Uno has 2048 bytes: variant 2 leaves 61 for the stack and everything else END OUTPUT CODE: for v in 0 1 2 3; do arduino-cli compile --fqbn arduino:avr:uno --warnings all --build-path b_p$v --build-property "compiler.cpp.extra_flags=-DVARIANT=$v" s_plan done # VARIANT 0: no planner; 1: BFS 30 x 20, queue 40, map in flash; 2: A* 30 x 20, heap 192, map in flash; 3: A* 16 x 16, heap 97, map in RAM END CODE OUTPUT: == VARIANT=0 Sketch uses 1710 bytes (5%) of program storage space. Maximum is 32256 bytes. Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes. == VARIANT=1 Sketch uses 2990 bytes (9%) of program storage space. Maximum is 32256 bytes. Global variables use 697 bytes (34%) of dynamic memory, leaving 1351 bytes for local variables. Maximum is 2048 bytes. == VARIANT=2 Sketch uses 3582 bytes (11%) of program storage space. Maximum is 32256 bytes. Global variables use 1987 bytes (97%) of dynamic memory, leaving 61 bytes for local variables. Maximum is 2048 bytes. == VARIANT=3 Sketch uses 3470 bytes (10%) of program storage space. Maximum is 32256 bytes. Global variables use 1166 bytes (56%) of dynamic memory, leaving 882 bytes for local variables. Maximum is 2048 bytes. END OUTPUT CODE: avr-nm --size-sort -S -C b_p2/s_plan.ino.elf | grep -E " [bBdD] " | tail -6 # variant 2 END CODE OUTPUT: avr-nm --size-sort -S -C b_p2/s_plan.ino.elf (variant 2: A* on 30 x 20, map in flash), RAM symbols (b = zero-initialised, d = initialised data), size in hex bytes 008001df 00000004 b timer0_millis 008001e3 00000004 b timer0_overflow_count 00800100 00000012 d vtable for HardwareSerial 008001e7 0000009d b Serial 00800116 000000c8 b path 00800284 0000063f b planner END OUTPUT Reading the output. The prediction is 188, 697, 1987 and 1166 bytes, and the compiler reported 188, 697, 1987 and 1166: exact. BFS on 30 x 20 with a queue of 40 is 75 + 150 + 80 + 4 = 309 bytes, plus the 200-byte path buffer, plus the 188 bytes that Serial and the timer take with no planner at all. A* on 30 x 20 with a heap of 192 is 75 + 600 + 150 + 768 + 6 = 1599 bytes, and avr-nm confirms it: the symbol 'planner' is 0x63F = 1599 bytes, the path buffer 0xC8 = 200. Two details. First, the 'from sizeof' column is 1 byte more than the prediction for variants 1 and 2 (698, 1988). That is the PC: its compiler pads a class to an even size because the heap entries hold 16-bit numbers, and the AVR does not pad. So sizeof on the PC is a good prediction to within a byte, not an exact one. Second, the first build of this sketch printed 705, 1995 and 1174, 8 more than the prediction in every variant; avr-nm showed two 4-byte tables (the neighbour offsets) in RAM, because on an AVR a const array lives in RAM and is initialised from flash at start-up. Replacing the tables by two small functions gave the predicted figures. What went wrong: I assumed const meant flash. How to avoid it: on an AVR, put constant tables in PROGMEM or write them as code, and check the symbol sizes with avr-nm when a prediction is off by a few bytes. Stack: variant 2 leaves 2048 - 1987 = 61 bytes for the stack, the Serial calls and everything else; variant 1 leaves 1351, variant 3 leaves 882. The compiler does not know the stack depth and reported variant 2 as a success. I did not measure how deep the stack gets, so 61 bytes is not shown to be too little; it is far less than I would be willing to bet on. Check on method: the prediction is arithmetic, and the sizes are the compiler's for arduino:avr 1.8.8; a different core (Serial buffer sizes) changes the 188.