Skip to content
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

Reducing memory footprint by using _naming for purely internal constants #37

Merged
merged 1 commit into from
Jan 8, 2022
Merged

Reducing memory footprint by using _naming for purely internal constants #37

merged 1 commit into from
Jan 8, 2022

Conversation

fivesixzero
Copy link

While preparing some potential fixes for review I realized that most prospective fixes will increase memory usage a bit.

After some discussions in the circuitpython-dev Discord channel with danh I tried applying leading-underscore naming to the slew of purely internal constants in the main driver file.

To see if this actually made a difference, I did some basic memory usage testing using a script I quickly put together. Its definitely not scientific - there are a bunch of other things that could be impacting memory usage - but at the very least it should provide some rough indication of whether this fix is worth the effort of submitting a PR. 😄

import board
import gc

i2c = board.I2C()

mem_pre_import, mem_post_import, mem_pre_instantiate, mem_post_instantiate = (0, 0, 0, 0)

gc.collect()
mem_pre_import = gc.mem_free()
from adafruit_apds9960.apds9960 import APDS9960
gc.collect()
mem_post_import = gc.mem_free()

gc.collect()
mem_pre_instantiate = gc.mem_free()
apds = APDS9960(i2c)
gc.collect()
mem_post_instantiate = gc.mem_free()
print("MEM Pre-import       | mem_free: {:7}".format(mem_pre_import))
print("MEM Post-import      | mem_free: {:7} | change: {:7}".format(mem_post_import, mem_post_import - mem_pre_import))
print("MEM Post-instantiate | mem_free: {:7} | change: {:7}".format(mem_post_instantiate, mem_post_instantiate - mem_pre_instantiate))

The table below shows the increase in memory usage from tests with four different versions of the library.

post-import post-instantiate mpy size
bundle 20211114 mpy 8,416 144 3,839
previous mpy 8,544 160 3,948
current mpy 8,464 144 3,794
constant_fix mpy (this PR) 7,856 144 3,364

The compiled file size dropped by more than 10%, which could be handy on some more limited boards. The memory usage savings weren't as strong but a 5% memory footprint reduction for just adding some underscores ain't bad.

@fivesixzero
Copy link
Author

The commit in this PR has been included with #38 which is currently in draft status.

I'm leaving it open for now since its ready for merge and is a lot less work to review for merge than that the large refactor in the other PR.

@fivesixzero
Copy link
Author

I also included the commit in this PR within #39, which has superseded #38.

Since that one is so big I'll leave this as a seprate PR in case some or all of the changes that PR don't work out.

@FoamyGuy FoamyGuy merged commit 424e72c into adafruit:main Jan 8, 2022
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.

2 participants