print_buffer: optimize & shrink
PATCH 1/1 print_buffer: optimize & shrink
Applying a little creative format string allows us to shrink the initial data read & display loop by only calling printf once. Re-using the local data buffer to generate the string we want to display then allows us to output everything with just one printf call instead of multiple calls to the putc function.
The local stack buffer needs increasing by 1 byte, but the resulting code shrink and speed up is worth it I think.
--- lib/display_options.c | 27 +++++++++++++-------------- 1 files changed, 13 insertions(+), 14 deletions(-)
lib/display_options.c +13 -14
@@ -101,7 +101,7 @@ void print_size(unsigned long long size, const char *s)#define DEFAULT_LINE_LENGTH_BYTES (16)int print_buffer (ulong addr,void * data,uint width,uint count,uint linelen) { -uint8_t linebuf[MAX_LINE_LENGTH_BYTES ]; +uint8_t linebuf[MAX_LINE_LENGTH_BYTES +1 ];uint32_t *uip = (void *)linebuf;uint16_t *usp = (void *)linebuf;uint8_t *ucp = (void *)linebuf;
@@ -121,24 +121,23 @@ int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen)/* Copy from memory into linebuf and print hex values */ for (i =0 ; i < linelen; i++) { -if (width ==4 ) { - uip[i] = *(volatile uint32_t *)data; -printf (" %08x" , uip[i]); - }else if (width ==2 ) { - usp[i] = *(volatile uint16_t *)data; -printf (" %04x" , usp[i]); - }else { - ucp[i] = *(volatile uint8_t *)data; -printf (" %02x" , ucp[i]); - } +uint32_t x; +if (width ==4 ) + x = uip[i] = *(volatile uint32_t *)data; +else if (width ==2 ) + x = usp[i] = *(volatile uint16_t *)data; +else + x = ucp[i] = *(volatile uint8_t *)data; +printf (" %0*x" , width *2 , x); data += width; }/* Print data in ASCII characters */ -puts (" " );for (i =0 ; i < linelen * width; i++) -putc (isprint (ucp[i]) && (ucp[i] <0x80 ) ? ucp[i] :'.' ); -putc ('\n' ); +if (!isprint (ucp[i]) || ucp[i] >=0x80 ) + ucp[i] ='.' ; + ucp[i] ='\0' ; +printf (" %s\n" , ucp);/* update references */ addr += linelen * width;
Signature · 1 line
1.7.2
Wolfgang Denk
AppliedPATCH 1/1 print_buffer: optimize & shrink
Dear Mike Frysinger,
Quoted 6 lines · In message <[email protected]> you wrote:
Applying a little creative format string allows us to shrink the initial data read & display loop by only calling printf once. Re-using the local data buffer to generate the string we want to display then allows us to output everything with just one printf call instead of multiple calls to the putc function.
Quoted 7 lines
The local stack buffer needs increasing by 1 byte, but the resulting code shrink and speed up is worth it I think.
Signed-off-by Mike Frysinger <[email protected]>--- lib/display_options.c | 27 +++++++++++++-------------- 1 files changed, 13 insertions(+), 14 deletions(-)
Applied, thanks.
Best regards,
Wolfgang Denk
Signature · 5 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de "The number of Unix installations has grown to 10, with more expected." - The Unix Programmer's Manual, 2nd Edition, June, 1972