mpprint: Add %R (print object) & %K (print exception). #17583
+18
−26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
printMP_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
%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 bePRINT_REPR
.%R
, as nowmp_printf
will be on the stack too.