Skip to content

Commit e957e16

Browse files
no1wudimasayuki2009
authored andcommitted
nsh: Improve performance of help by line buffer
This change reduce the usage of `printf`, which will improve both CPU usage and IO performance. Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
1 parent 21bc466 commit e957e16

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

nshlib/nsh_command.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,8 @@ static inline void help_cmdlist(FAR struct nsh_vtbl_s *vtbl)
615615
unsigned int i;
616616
unsigned int j;
617617
unsigned int k;
618+
unsigned int offset;
619+
char line[HELP_LINELEN];
618620

619621
/* Pick an optimal column width */
620622

@@ -650,22 +652,32 @@ static inline void help_cmdlist(FAR struct nsh_vtbl_s *vtbl)
650652

651653
for (i = 0; i < ncmdrows; i++)
652654
{
653-
nsh_output(vtbl, " ");
655+
/* Tab before a new line */
656+
657+
offset = 4;
658+
memset(line, ' ', offset);
659+
654660
for (j = 0, k = i;
655661
j < cmdsperline && k < NUM_CMDS;
656662
j++, k += ncmdrows)
657663
{
658-
nsh_output(vtbl, "%s", g_cmdmap[k].cmd);
664+
/* Copy the cmd name to line buffer */
665+
666+
offset += strlcpy(line + offset, g_cmdmap[k].cmd,
667+
sizeof(line) - offset);
668+
669+
/* Add space between commands */
659670

660671
for (cmdwidth = strlen(g_cmdmap[k].cmd);
661672
cmdwidth < colwidth;
662673
cmdwidth++)
663674
{
664-
nsh_output(vtbl, " ");
675+
line[offset++] = ' ';
665676
}
666677
}
667678

668-
nsh_output(vtbl, "\n");
679+
line[offset++] = '\n';
680+
nsh_write(vtbl, line, offset);
669681
}
670682
}
671683
#endif
@@ -796,6 +808,9 @@ static inline void help_builtins(FAR struct nsh_vtbl_s *vtbl)
796808
unsigned int i;
797809
unsigned int j;
798810
unsigned int k;
811+
unsigned int offset;
812+
char line[HELP_LINELEN];
813+
static const char *g_builtin_prompt = "\nBuiltin Apps:\n";
799814

800815
/* Count the number of built-in commands and get the optimal column width */
801816

@@ -845,10 +860,12 @@ static inline void help_builtins(FAR struct nsh_vtbl_s *vtbl)
845860

846861
/* List the set of available built-in commands */
847862

848-
nsh_output(vtbl, "\nBuiltin Apps:\n");
863+
nsh_write(vtbl, g_builtin_prompt, strlen(g_builtin_prompt));
849864
for (i = 0; i < num_builtin_rows; i++)
850865
{
851-
nsh_output(vtbl, " ");
866+
offset = 4;
867+
memset(line, ' ', offset);
868+
852869
for (j = 0, k = i;
853870
j < builtins_per_line &&
854871
(builtin = builtin_for_index(k));
@@ -859,17 +876,19 @@ static inline void help_builtins(FAR struct nsh_vtbl_s *vtbl)
859876
continue;
860877
}
861878

862-
nsh_output(vtbl, "%s", builtin->name);
879+
offset += strlcpy(line + offset, builtin->name,
880+
sizeof(line) - offset);
863881

864882
for (builtin_width = strlen(builtin->name);
865883
builtin_width < column_width;
866884
builtin_width++)
867885
{
868-
nsh_output(vtbl, " ");
886+
line[offset++] = ' ';
869887
}
870888
}
871889

872-
nsh_output(vtbl, "\n");
890+
line[offset++] = '\n';
891+
nsh_write(vtbl, line, offset);
873892
}
874893
#endif
875894
}

0 commit comments

Comments
 (0)