Skip to content

py: clean up calls to gc_alloc(), reorder setting of FTB, use mp_obj_malloc() to allocate array object - #19707

Merged
dpgeorge merged 5 commits into
micropython:masterfrom
dpgeorge:py-clean-up-obj-alloc
Sep 30, 2026
Merged

dpgeorge merged 5 commits into
micropython:masterfrom
dpgeorge:py-clean-up-obj-alloc

Conversation

@dpgeorge

Copy link
Copy Markdown
Member

Summary

A few small changes that came up while reviewing #19367:

  • Make sure flags are passed to gc_alloc instead of a bool (the API of gc_alloc() was changed long ago in commit 5ed578e to take flags instead of a bool).
  • Set FTB bit while still inside the GC lock.
  • Use mp_obj_malloc to allocate array object.

Testing

To be tested by CI.

Generative AI

I did not use generative AI tools when creating this PR.

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Sep 16, 2026
@codecov

codecov Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.59%. Comparing base (5b5f06c) to head (b4263a7).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #19707      +/-   ##
==========================================
+ Coverage   98.55%   98.59%   +0.03%     
==========================================
  Files         182      182              
  Lines       23335    23344       +9     
  Branches        5        5              
==========================================
+ Hits        22998    23015      +17     
+ Misses        336      328       -8     
  Partials        1        1              
Flag Coverage Δ
unix-coverage-32bit 98.59% <100.00%> (+0.03%) ⬆️
unix-coverage-64bit 98.52% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Code size report:

Reference:  mpy-cross/Makefile: Let mpy-cross build in EIM environments. [5b5f06c]
Comparison: unix/coverage: Add coverage test for realloc of GC block with finaliser. [merge of b4263a7]
  mpy-cross:   -16 -0.004% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +8 +0.001% standard
      stm32:   -16 -0.004% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    -8 -0.002% TEENSY40
        rp2:   -16 -0.002% RPI_PICO_W
       samd:    -8 -0.003% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   -10 -0.002% VIRT_RV32

@projectgus projectgus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cleanups look good to me!

The API of `gc_alloc()` was changed long ago in commit
5ed578e to take flags instead of a bool.
Update the remaining instances to use flags.

Signed-off-by: Damien George <[email protected]>
Should be equivalent logic, and prevents needing to reacquire the GC lock.

Signed-off-by: Damien George <[email protected]>
Makes it easier to track which allocations are for Python objects.

Signed-off-by: Damien George <[email protected]>
@dpgeorge
dpgeorge force-pushed the py-clean-up-obj-alloc branch from 1f6ba49 to 6dc11be Compare September 30, 2026 01:52
@dpgeorge

Copy link
Copy Markdown
Member Author

I just realised that the CI coverage test here is telling the truth: coverage decreased due to this PR. That's because there's no test for a relocation of a GC block that has the finaliser bit set (that feature is never used by the codebase).

I've now added a coverage test for that, including a new function gc_meta_bits(ptr) which returns the ATB/FTB/WTB bits for the given ptr. I think it's worth adding such a function because it helps a lot to test things are working correctly. It's not guarded by any #if but shouldn't be included in builds that don't use it due to gc-sections.

@dpgeorge
dpgeorge requested a review from projectgus September 30, 2026 03:14

@projectgus projectgus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, yes! Good catch by the coverage test.

@dpgeorge
dpgeorge merged commit b4263a7 into micropython:master Sep 30, 2026
102 of 103 checks passed
@dpgeorge
dpgeorge deleted the py-clean-up-obj-alloc branch September 30, 2026 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants