Cube Library Code - Reduce Memory Usage

The "Cube4" is a 4x4x4 RGB LED cube with an Arduino Leonardo compatible onboard controller. [Product page]
Post Reply
neographophobic
Posts: 9
Joined: Mon Sep 28, 2015 6:36 am

Cube Library Code - Reduce Memory Usage

Post by neographophobic » Mon Sep 28, 2015 11:17 am

I've just started playing with my cube, and I particularly like the Weather Cube example that has been posted at viewtopic.php?f=32&t=6277. It gave me some ideas on how I would like to extend it, but I started to get concerned about the memory usage and how it may limit what I want to do. So while I wait for some parts to arrive, I tried to find ways I could reduce the memory usage.

This review resulted in 6 separate changes that I have made to the cube library (primarily to the serial command line parser). These changes are as follows:-
  • skipWhiteSpace
    Optimise the calls to skipWhiteSpace so that they happen within the parseAxis, parseDirection, parsePosition, parseOffset and parseRGB instead of being called before each function. As each call to those functions had a preceding call to skipWhiteSpace this can be done with no impact to functionality, and saves 244 bytes of program storage space.
  • Lowercase
    Convert the message to lowercase, and then only check against lowercase characters, instead of both upper and lowercase characters. This has the added benefit of now allowing commands as well as items to be in any case (so 'ALL BLUE;' and 'all blue;' both now work). This saves 52 bytes of program storage space.
  • Direction Minimisation
    Combine two if statements that set the same results into a single statement with or, saving 24 bytes of program storage space.
  • Remove middle letters
    When testing for the predefined characters, cheat and only look for the first and last character of the colour as these are unique. This saves 182 bytes of program storage space.
  • F Macro
    Use the F() macro for static strings that are outputted via the serial interface. This does use an 74 bytes of program storage space, but reduces the global variables from using 1,633 bytes (63%) to 729 bytes (28%) of dynamic memory. This makes an extra 904 bytes or 35% of dynamic memory available for local variables.
  • No help text
    Add an option (disabled by default) to remove the help text from the serial console when the user types 'help;'. If enabled all the user will only see the link to the website, and program storage space will be reduced by 1024 bytes.
A summary of the memory changes are as follows (all tested with the TestPattern Sketch):-
  • Original Library - 16,108 bytes (Program Storage Space) and 1,633 bytes (Dynamic Memory)
  • skipWhiteSpace, Lowercase, Direction Minimisation, Remove middle letters, and F Macro - 15,680 bytes (Program Storage Space) and 729 bytes (Dynamic Memory). This is a saving of 428 bytes (2%) Program Storage Space and a massive 904 bytes (35%) of Dynamic Memory.
  • No help text - if enabled, it saves an extra 1,024 bytes (3%) of Program Storage Space.
I've posted the changes to my clone of the Cube4 library on Github at https://github.com/neographophobic/Cube ... mory_usage, specifically to the reduce_memory_usage branch. Each of the changes outlined above were made as individual commits if you want to see what was done.

I've tested the above with the TestPattern, but would love to know if it works for you or if these changes introduce errors that I'm not aware of. I haven't written much Arduino code, so any improvements, comments, bugs, or suggestions are welcomed (including pull requests), but please bear with me as it may take me a while to incorporate any changes.

P.S. I've also posted a combined version of these changes, and the changes I made to add user defined function support to the serial command line parser at https://github.com/neographophobic/Cube ... ary_master. Please see the post Cube Library Code - User Defined Function for more information on this.

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Cube Library Code - Reduce Memory Usage

Post by andrew » Sat Dec 05, 2015 10:13 am

Well done, thank you for sharing that with us.

Post Reply