Commit 674d78f
committed
py/mpprint: Use a padding buffer on the stack.
This reworks `mp_print_strn` to use a stack-allocated padding buffer
rather than special-cased hardcoded ROM strings in order to reduce
code size and improve string formatting performance.
Note that this is actually just as performant, even for zeroes and
spaces! On my RP2350 Cortex M33 hardware, spaces are about 1% faster
for short-padding cases, and 3.4% faster for long-padding cases.
I've done some cursory tests for alternate values of `PAD_BUF_SIZE`, but
the results definitely won't generalize to other architectures, and
probably not even to other implementations of the same architecture.
The buffer size of 20 is chosen as the smallest size that easily admits
a later implementation of #18092 to support padding with grouping
characters, to avoid pessimizing the short-padding cases any more than
required.
I've also explored alternatives involving using `alloca` for the padding
buffer, but the conditionals and fallback logic needed to bound stack
usage for the pathological cases end up pessimizing code size beyond
what's reasonable for the very marginal additional speed gains.
Signed-off-by: Anson Mansfield <[email protected]>1 parent 718b28a commit 674d78f
1 file changed
+38
-46
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
44 | | - | |
45 | 43 | | |
46 | 44 | | |
47 | 45 | | |
| |||
58 | 56 | | |
59 | 57 | | |
60 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
61 | 73 | | |
62 | 74 | | |
63 | 75 | | |
64 | 76 | | |
65 | | - | |
66 | 77 | | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | 78 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
90 | 96 | | |
91 | | - | |
| 97 | + | |
92 | 98 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | 99 | | |
102 | | - | |
| 100 | + | |
| 101 | + | |
103 | 102 | | |
104 | 103 | | |
105 | 104 | | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
117 | 109 | | |
118 | 110 | | |
119 | 111 | | |
| |||
0 commit comments