Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

RPi Zero - Garbage Collection in adafruit-bitmap-font
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

RPi Zero - Garbage Collection in adafruit-bitmap-font

by ronfischler on Wed Sep 16, 2020 4:41 pm

I have been using a 28-point bitmap font on my 2" 240x320 TFT, which I load using the load_glyphs() BDF class method, located in bdf.py. My font has 97 glyphs, and it takes a long time to load: nearly 11 seconds. I thought this was quite long for my RPi Zero, and began investigating. I noticed on line 128 of bdf.py, that garbage is collected after each glyph has been loaded into the font's dictionary. This seems overly cautious to me, and I commented out that line: gc.collect(). Doing this on my Rpi Zero cut my font loading time from 11 down to about 5 seconds, a considerable savings when starting my code.

I do not have the knowledge to claim that changing how GC is done in bdf.py would be a good idea for all Adafruit boards, but I suggest that such aggressive garbage collection might be unnecessary. I found that as each glyph is loaded it adds approximately 11 first-generation references, reported by gc_count(). If automatic GC is running, is this an issue? At worst, maybe have load_glyphs() call for garbage collection after loading every ten glyphs? If adding only one glyph to the dictionary, then skip garbage collection altogether?

Loading my font leaves about 1,000 first-generation references to be collected. Automatic garbage collection, on my RPi Zero is set to run at 750.

If I do not call load_glyphs() explicitly before using my font, then the time it takes to get the TFT display going is noticeably worse.

ronfischler
 
Posts: 27
Joined: Thu Jul 23, 2020 3:15 pm

Re: RPi Zero - Garbage Collection in adafruit-bitmap-font

by tannewt on Thu Sep 17, 2020 1:40 pm

Hi Ron, fragmentation is a big issue on CircuitPython, that is why we collect every glyph. Hopefully, it leads to less fragmentation.

Are you loading all glyphs at once? Finding a glyph is a linear search through the BDF text which can make it very slow.

tannewt
 
Posts: 1837
Joined: Thu Oct 06, 2016 8:48 pm

Re: RPi Zero - Garbage Collection in adafruit-bitmap-font

by ronfischler on Thu Sep 17, 2020 2:26 pm

tannewt wrote:Hi Ron, fragmentation is a big issue on CircuitPython, that is why we collect every glyph. Hopefully, it leads to less fragmentation.

Are you loading all glyphs at once? Finding a glyph is a linear search through the BDF text which can make it very slow.


Ah! - I did not understand the issue of memory fragmentation. I load all glyphs at once. So, even when doing this, it is not sufficient to wait to do garbage collection until the end of the load? Fragmentation would increase?

ronfischler
 
Posts: 27
Joined: Thu Jul 23, 2020 3:15 pm

Re: RPi Zero - Garbage Collection in adafruit-bitmap-font

by tannewt on Fri Sep 18, 2020 1:47 pm

Yup, you actually want to call collect *before* you allocate the final object because that is where it will stay. By calling collect in the middle, it help keep the final allocations closer together.

(I have a deep dive on CircuitPython heap allocations here: https://www.youtube.com/watch?v=baa5ILZ ... A&index=25 )

tannewt
 
Posts: 1837
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.