Skip to content

Firmware size savings #3236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 7, 2020
Merged

Conversation

jepler
Copy link

@jepler jepler commented Aug 1, 2020

I ended up finding a range of firmware size (flash utilization) improvements. One item is a performance trade-off, and one item is a feature removal for the most constrainted boards.

A small amount of RAM is also saved.

Baseline: c394af4 (origin/main) Merge pull request #3241 from jepler/translation-percent-space-fixes

Binary: trinket_m0 en_US

Flash free: +1404 bytes
RAM free: +76 bytes
Total translation strings: -17

@jepler jepler force-pushed the firmware-size-gemma-trinket branch from c1f97b5 to 930000e Compare August 1, 2020 18:56
@jepler
Copy link
Author

jepler commented Aug 1, 2020

@jepler jepler closed this Aug 1, 2020
@jepler jepler reopened this Aug 1, 2020
@tannewt
Copy link
Member

tannewt commented Aug 3, 2020

Many of these strings are already in the qstr pool. Do you want to switch these to %q (non-standard but useful) and use the qstr instead? That should save storing the pointer and any literal duplication of the string.

jepler added 3 commits August 4, 2020 13:34
This is a slight trade-off with code size, in places where a "_varg"
mp_raise variant is now used.  The net savings on trinket_m0 is
just 32 bytes.

It also means that the translation will include the original English
text, and cannot be translated.  These are usually names of Python
types such as int, set, or dict or special values such as "inf" or
"Nan".
This saves a very small amount of flash, 8 bytes on trinket_m0
@jepler jepler changed the title Save 40 more bytes firmware size on trinket_m0 Firmware size savings Aug 4, 2020
@jepler jepler force-pushed the firmware-size-gemma-trinket branch from 63bfaec to d3fb6ff Compare August 4, 2020 19:31
@jepler
Copy link
Author

jepler commented Aug 4, 2020

I've heavily revised this PR and now it contains a couple of substantial size savers.

jepler added 12 commits August 4, 2020 14:45
This removes runtime allocations of the cstring version of the qstring.

It is not a size improvement
This saves nearly 200 bytes.  Curiously, it also saves RAM.
The missing second "const" made these mutable arrays pointing to
const string data.
This function computes the remainder of a value `x` modulo pi/2, to high
precision.

It does this by dividing the flotaing point values into several ranges
by magnitude, and applies successively slower but more accurate algorithms.

The last two steps, one covering values up to around 2^7 * pi/2
(called "medium size") and a final one covering all possible float values,
require big tables.

By eliminating the "medium size" case, a table and some code are removed
from the binary.  This makes some cases take longer, but saves hundreds
of bytes.  It does _NOT_ affect the result, only the speed.

```
[desktop python]
>>> sum(math.sin(2.**i) for i in range(21))
1.4206898748939305

[trinket m0, before change to ef_rem_pio2.c]
>>> sum(math.sin(2.**i) for i in range(21))
1.42069

[trinket m0, after change to ef_rem_pio2.c]
>>> sum(math.sin(2.**i) for i in range(21))
1.42069
```
This array was of 32-bit values, but the entries were only ever
in the 0-255 range.  Convert to uint8_t.

Testing performed: The result of the sum-of-sin was unchanged
>>> import math; sum(math.sin(2.**i) for i in range(21))
1.42069
@jepler jepler force-pushed the firmware-size-gemma-trinket branch from d3fb6ff to 6669ced Compare August 4, 2020 19:45
@jepler
Copy link
Author

jepler commented Aug 4, 2020

Free Flash Change Free RAM Change Commit
1772   22716   c394af4 Merge pull request #3241 from jepler/translation-percent-space-fixes
1804 32 22716 0 dddd25a Combine similar strings to reduce size of translations
1812 8 22716 0 67eb93f py: introduce, use mp_raise_msg_vlist
1804 -8 22716 0 92917b8 fix exception type for pop from empty set
1808 4 22716 0 d60cace Use qstrs to save an additional 4 bytes
1804 -4 22716 0 67c8173 various: Use mp_obj_get_type_qstr more widely
1856 52 22716 0 549632a Combine 'index out of range' messages
1868 12 22716 0 c046bd7 Combine some "safe mode" messages
1868 0 22716 0 0517c3e Combine some "can't convert" messages
2064 196 22752 36 f65d1a0 main: Drop "double extension" detection if not FULL_BUILD
2064 0 22792 40 e138b02 main: Allow these arrays to reside in ROM
2096 32 22792 0 1c53f91 safe_mode: Exclude NORDIC_SOFT_DEVICE_ASSERT str if possible
2544 448 22792 0 5c32756 libm: ef_rem_pio2.c: Save ROM-tables at the expense of speed
3152 608 22792 0 a3566ad libm: rem_pio2: Reduce size of static array
3176 24 22792 0 0f8084b py: mp_obj_get_type_qstr as macro saves 24 bytes
3176 0 22792 0 d3fb6ff make translate

dhalbert
dhalbert previously approved these changes Aug 4, 2020
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! Excellent analysis, particularly on the math routines.

Saves 12 bytes code on trinket m0
@jepler jepler requested a review from tannewt August 5, 2020 01:32
@dhalbert
Copy link
Collaborator

dhalbert commented Aug 5, 2020

@jepler I'm confused, did you remove the #3245 changes from this PR?

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@jepler jepler merged commit d8cc479 into adafruit:main Aug 7, 2020
@jepler
Copy link
Author

jepler commented Aug 7, 2020

@dhalbert #3245 is an additional size savings, but at the expense of mathematical correctness. I split it out so it could be considered separately.

@jepler jepler deleted the firmware-size-gemma-trinket branch November 3, 2021 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants