Repository navigation
Conversation
|
Stack usage was also checked with |
|
casaroli
left a comment
There was a problem hiding this comment.
This makes lib_libvsprintf.o 218 bytes larger than master (arm-none-eabi-gcc 13.2: 2453 -> 2671 bytes of text); the "+48 bytes" table compares with e5a5f85^, not with master. verdin-mx8mp:rpmsg had 8 bytes of flash free, so arm-imx CI fails: "region `flash' overflowed by 208 bytes".
Fix: reduce the growth, or make room in boards/arm/mx8mp/verdin-mx8mp/configs/rpmsg/defconfig in this PR (for example disable CONFIG_TESTING_RAMTEST).
Commit bdc9adc "libc/stdio: Merge float format dispatch checks": tools/checkpatch.sh drops a first line that contains "Merge" (it takes it for a merge commit), so the check job fails with "Commit subject missing colon".
Fix: use another word in the subject, for example "Combine".
This PR contains the 6 commits of #21339 with the same SHAs, so both PRs ask a review for the same code.
Fix: rebase this PR on master after #21339 merges.
what's the benefit if no stack or code size reduce? @anchao |
The stack usage only increases in the worst-case path. For common cases that do not use floating-point formatting, the stack usage is reduced because the float-specific state is moved out of the main conversion path. |
a8bce62 to
c2010af
Compare
how many bytes will increase in the worst-case? |
The worst-case stack increase I measured is +16 bytes. The common non-float paths decrease: literal -96, string/integer about -112, char -16, wide string no change. The +16 case is the floating-point path, where vsprintf_internal drops by 192 bytes but the active helper chain (vsprintf_put_float + fixed/NaN helper + repeat) adds back slightly more than the old inline path. |
casaroli
left a comment
There was a problem hiding this comment.
arm-stm32h7 fails on this PR: weact-stm32h750:sdcard with clang gives "section '.data' will not fit in region 'flash': overflowed by 160 bytes" (run 41649784195). The same configuration links on other PRs in the same job.
Fix: reduce the growth, or remove CONFIG_ARCH_STACKDUMP from boards/arm/stm32h7/weact-stm32h750/configs/sdcard/defconfig in this PR, as aed8c30 did for two other boards.
|
@anchao please lets use my other proposed solution which:
|
|
@anchao please fix the conflit. |
Done |
@casaroli Thanks for checking this. I looked into this again, and I think the stack increase is related to the #define inline_function attribute((always_inline)) inline So using static inline_function here forces GCC to inline all of these split I tested this with GCC by removing inline_function from all of the split nuttx.elf text: lib_libvsprintf.o text: stack usage: So for this GCC configuration, removing forced inline is a net win: text size is I updated the PR to drop inline_function from these helpers. Other |
|
@anchao should you change the pr title or create a new pr? the change is different from the initial patch a lot. |
@xiaoxiang781216 @casaroli I have to say I’m quite confused by the recent Because of the frequent changes in this area, many of my local PR branches have become obsolete or very hard to rebase. What I do not understand is that I had already asked for my first |
|
A change similar to this is actually already included in my PR: |
|
@casaroli I find this behavior quite unreasonable. Please refrain from reviewing my commits if the review is going to proceed in this manner. |
Please review again |
casaroli
left a comment
There was a problem hiding this comment.
bb0df3617ef makes printf("abc%") print abc%% and return 5, and the next two commits keep it; bd2c87347ef makes it abc% again. Master already prints abc%: the literal loop ends with c == 0, and the branch for an unknown conversion or the end of the format prints the %. So "currently drops that trailing percent" is not true for master, and three commits with a wrong output land on master. On qemu-armv8a:knsh at 3429b61862b: abc% gives abc%%, % gives %%, %d% gives 5%%; at the base 4d0aabe2d6f and at the head: abc%, %, 5%.
Fix: drop bb0df3617ef; vsprintf_scan_literal() in bd2c87347ef already prints the %.
The image and the deepest stack are still larger than on master. nucleo-f072rb:nsh, Default profile, arm-none-eabi-gcc 15.3, the head against its base 4d0aabe2d6f, size nuttx and -fstack-usage:
| float off | float on | |
|---|---|---|
image text, defconfig (-O0) |
+520 | +536 |
image text, -Os |
+80 | +80 |
deepest stack from lib_vsprintf(), -O0 |
216 -> 272 | 400 -> 376 |
deepest stack from lib_vsprintf(), -Os |
128 -> 168 | 232 -> 240 |
At -Os only vsprintf_put_field() stays a function, and the deepest chain goes through it and vsprintf_repeat(). The base gives the numbers of Documentation/os/libs/libc/printf.rst (46,792 bytes, vsprintf_internal() frame 88), so that page is out of date after this PR.
Fix: no growth of image text and of the deepest stack, or update the "Flash and stack" tables of printf.rst in this PR.
The body lists changes that are not in the 6 commits (the floating point splits, the numbered-argument helper, the shared integer argument fetch, inline_function for the helpers), and the size table compares with the parent of the first commit of an older version of this PR, not with master.
Fix: describe the 6 commits, and measure against the base 4d0aabe2d6f.
#21595 (%n with LIBC_PRINTF_PERCENT_N off) is merged, and apache/nuttx-apps#3994 adds the printftest cases for the current printf options. Please wait until apache/nuttx-apps#3994 merges, then rebase this PR on master and run that printftest on it.
casaroli
left a comment
There was a problem hiding this comment.
Dropping bb0df3617ef conflicts with bd2c87347ef, which rewrites the same lines.
Fix: squash bb0df3617ef into bd2c87347ef (git rebase -i, move it after 3429b61862b, mark it fixup). The head stays the same, and no commit prints abc%%.
A format string that ends with a single '%' currently drops that trailing percent. The literal scanner consumes the terminating NUL as the next conversion character, then vsprintf_internal() treats it as end-of-format. Emit and count the pending '%' before returning end-of-format from both the direct string and CONFIG_ARCH_ROMGETC scanning paths. Signed-off-by: chao an <[email protected]>
Move the common string field padding and output sequence into vsprintf_put_field(). This keeps the conversion handlers focused on selecting the data to print while preserving the existing width and left-padding behavior. Signed-off-by: chao an <[email protected]>
Move character and locale wide-string output handling into dedicated helpers. The main conversion loop now delegates %c, %lc and %ls emission while keeping the same padding, precision and conversion-error behavior. Signed-off-by: chao an <[email protected]>
Move literal scanning and escaped percent output into a helper shared by the normal and CONFIG_ARCH_ROMGETC paths. Keep numbered-argument first pass handling intact and preserve trailing percent literals when the format ends after a single %. Signed-off-by: chao an <[email protected]>
Move CONFIG_LIBC_PRINT_EXTENSION handling for %pB, %pV, %pS and %ps into a separate helper. This keeps extension dispatch out of the normal integer conversion path while preserving va_format and allsyms behavior. Signed-off-by: chao an <[email protected]>
Move integer prefix, precision, padding and digit emission into vsprintf_put_integer(). Leave vsprintf_internal() responsible for reading and converting the argument, then delegate the formatted output to the helper. Signed-off-by: chao an <[email protected]>
Pass the field width by value to vsprintf_put_field() so the helper no longer needs to update the caller's width through a pointer. Keep the left-padding tail adjustment in the callers that still need it. This trims the default optimized printf path while preserving the helper split and avoids forcing the field helper inline. Signed-off-by: chao an <[email protected]>
|
@casaroli I also added a small follow-up optimization to reduce the field helper overhead without forcing it inline. In my local test, forcing vsprintf_put_field() inline increased text size, so the new version instead passes the width by value and keeps the left-padding tail adjustment in the callers that need it. I reran size/stack measurements on nucleo-f072rb:nsh with arm-none-eabi-gcc 13.3.1. For the default non-floating-point -Os path: For the floating-point -Os path with CONFIG_LIBM_TOOLCHAIN=y and CONFIG_LIBC_FLOATINGPOINT=y: I will update the PR description with the current test configuration and full result tables. I also understand your point about avoiding broken intermediate commits for the trailing % behavior. I will review the commit history before the final version so that each commit preserves the expected behavior. |
|
ping @casaroli |
casaroli
left a comment
There was a problem hiding this comment.
b-g431b-esc1:nsh and nucleo-g431kb:nsh do not link on this PR (flash overflowed by 28 and 84 bytes); the same configs link in other PRs' runs today.
Fix: no text growth, or make room on these two boards in this PR.
03e3a5a3c6e still makes printf("abc%") print abc%% until a later commit.
Fix: drop it or squash it into f31ab128cb3.
|
|
||
| pnt = buf; | ||
| ch = vsprintf_put_field(stream, pnt, size, *width, flags); | ||
| *width = FL_LPAD != 0 && *width > 0 && size < (size_t)*width ? |
There was a problem hiding this comment.
FL_LPAD != 0 tests the macro, not the flag, so it is true whenever LIBC_PRINTF_LEFT_JUSTIFY is on (the default). After a right-justified field, width keeps width - size, and tail: pads it again on the right: printf("[%5s]", "ab") prints [ ab ] and returns 10; master prints [ ab] and returns 7. The same for %5c, %5lc, %1$5s and the ? of %5a.
Fix: (flags & FL_LPAD) != 0 here and at line 2160.
| width = width >= size ? width - size : 0; | ||
| total_len += vsprintf_put_field(stream, pnt, size, width, | ||
| flags); | ||
| width = FL_LPAD != 0 && width > 0 && size < (size_t)width ? |
There was a problem hiding this comment.
The same as line 586.
Fix: (flags & FL_LPAD) != 0.
Summary
Refactor
lib_vsprintf()by splitting several conversion/output paths out ofvsprintf_internal()into smaller helpers.This series keeps the external printf behavior unchanged while making the main conversion loop easier to follow and reducing the amount of formatting logic kept directly inside
vsprintf_internal().The series currently contains:
%literals, e.g.printf("abc%");vsprintf_put_field();vsprintf_put_char();vsprintf_scan_literal();vsprintf_pointer_extension();vsprintf_put_integer();Rebase
Rebased on current
apache/master:Size and stack results
Test target:
nucleo-f072rb:nsharm-none-eabi-gcc 13.3.1Common test options:
Default non-floating-point printf path
For the default nucleo-f072rb:nsh configuration,
CONFIG_LIBM_NONE=y, so floating-point printf support is disabled.-O0
CONFIG_DEBUG_NOOPT=y,CONFIG_LIBC_FLOATINGPOINT=n-Os
CONFIG_DEBUG_FULLOPT=y,CONFIG_LIBC_FLOATINGPOINT=nFloating-point printf path
For the floating-point test, the math library was enabled explicitly:
-O0
CONFIG_DEBUG_NOOPT=y-Os
CONFIG_DEBUG_FULLOPT=yChecks
The following checks were run:
No issues were reported.
The following builds were run for
nucleo-f072rb:nsh:-O0,CONFIG_LIBC_FLOATINGPOINT=n-Os,CONFIG_LIBC_FLOATINGPOINT=n-O0,CONFIG_LIBM_TOOLCHAIN=y,CONFIG_LIBC_FLOATINGPOINT=y-Os,CONFIG_LIBM_TOOLCHAIN=y,CONFIG_LIBC_FLOATINGPOINT=y