Skip to content

mpprint: Add %R (print object) & %K (print exception). #17583

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jepler
Copy link
Contributor

@jepler jepler commented Jun 30, 2025

As well as adding some use sites to get an idea of whether this is saving code size.

This also makes that mp_obj_print_helper print MP_OBJ_NULL objects as (nil) (regardless of NDEBUG), which is otherwise written at several use sites.

With just a few sites modified to use %R & %K, it already saved around 28 bytes on MPS2_AN385 according to a quick local test.

Summary

I noticed that often there is an alternation between calling mp_printf & mp_print_obj_helper. I hypothesized that overall code size might be reduced by adding a %-format for whole objects to mp_printf.

Testing

I locally ran the unix coverage suite & built a qemu build for size comparison.

Trade-offs and Alternatives

  • Somewhat arbitrarily I chose %R for objects and %K for exceptions. I initially decided to pass in the print kind for %R but this may be unneeded as in the sites I converted so far, the print type is statically known to be PRINT_REPR.
  • Printing some deeply nested objects will terminate sooner when it goes through a path that uses %R, as now mp_printf will be on the stack too.
  • This would require updates to the printf format string checker in [RFC] Add compile-time checking of mp_printf format strings #17556, which I will do if this PR is accepted and that PR is deemed desirable.
  • There are probably more sites amenable to conversion. I only converted enough sites to show that there was a size savings to be had.

As well as adding some use sites to get an idea of whether this
is saving code size.

This also makes that `mp_obj_print_helper` print `MP_OBJ_NULL`
objects as `(nil)` (regardless of NDEBUG), which is otherwise
written at several use sites.

With just a few sites modified to use %R & %K, it already saved
around 28 bytes on MPS2_AN385 according to a quick local test.

Signed-off-by: Jeff Epler <[email protected]>
Copy link

codecov bot commented Jun 30, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.57%. Comparing base (09541b7) to head (6f86d23).
Report is 17 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17583   +/-   ##
=======================================
  Coverage   98.56%   98.57%           
=======================================
  Files         169      169           
  Lines       21950    21961   +11     
=======================================
+ Hits        21636    21649   +13     
+ Misses        314      312    -2     

☔ View full report in Codecov by Sentry.
📢 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.

Copy link

Code size report:

   bare-arm:   +48 +0.085% 
minimal x86:  +157 +0.084% 
   unix x64:   -72 -0.008% standard
      stm32:   -36 -0.009% PYBV10
     mimxrt:  -136 -0.036% TEENSY40
        rp2:   +32 +0.003% RPI_PICO_W
       samd:   -36 -0.013% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   +48 +0.011% VIRT_RV32

@jepler
Copy link
Contributor Author

jepler commented Jun 30, 2025

The code size is a bit all over the place. This is because the more minimal ports aren't benefitting from (m)any of the reductions, but are paying the full cost of the added format codes.

the cc3200 failure is interesting. It looks like mp_printf but not mp_obj_print_helper is built into the "bootmgr" program. So, support for these new format strings would need to be conditional.

Making it conditional would be nice, as some ports might not use %R; the plugin printf checker could probably consult the compiler flags to check if it's available when checking format strings, but that's a hypothetical at this point.

the nanbox failure is due to printing an object of type mp_obj_t via the %p format specifier. This mistake wasn't detected at compile time by #17556 because the plugin was only enabled for coverage builds, but the nanbox build is its own variant.

%K should likely be dropped, I'm guessing there are too few sites where it would be used.

This is feeling overly complicated for a small savings only on larger builds where clawing for those last few bytes doesn't make as much difference usually. Please feel free to close if you're feeling the same way.

@dpgeorge
Copy link
Member

dpgeorge commented Jul 1, 2025

I can see how this could be useful in decreasing code size. And it's definitely a lot cleaner to use %R than explicitly calling the function to print the object.

But, to merge it, the change can't increase size of the bare-arm build.

Note that unix and mimxrt are the ones with the biggest savings because they are the only ones on the list with MICROPY_ERROR_REPORTING_DETAILED enabled, which is where most of the use of %R is.

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