1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-02 01:18:26 +03:00

command: refactor code for simplier lookup

We can more efficiently use command_name struct to
lookup for  lvm_command_enum and avoid many repeated
command name searches since we already know
the enum index that is now stored in 'struct command'.
This commit is contained in:
Zdenek Kabelac 2024-05-12 15:39:18 +02:00
parent 82617852a4
commit 5840f90e82
3 changed files with 23 additions and 47 deletions

View File

@ -377,7 +377,7 @@ static uint64_t _lv_to_bits(struct command *cmd, char *name)
return lvt_bits; return lvt_bits;
} }
const struct command_name *find_command_name(const char *name) static unsigned _find_lvm_command_enum(const char *name)
{ {
static int _command_names_count = -1; static int _command_names_count = -1;
int first = 0, last, middle; int first = 0, last, middle;
@ -408,28 +408,17 @@ const struct command_name *find_command_name(const char *name)
else if (i > 0) else if (i > 0)
last = middle - 1; last = middle - 1;
else else
return &command_names[middle]; return middle;
} }
return NULL; return -1;
} }
static const struct command_name *_find_command_name(const char *name) const struct command_name *find_command_name(const char *name)
{ {
if (!islower(name[0])) int r = _find_lvm_command_enum(name);
return NULL; /* Commands starts with lower-case */
return find_command_name(name); return (r < LVM_COMMAND_COUNT) ? &command_names[r] : NULL;
}
static const char *_is_command_name(char *str)
{
const struct command_name *c;
if ((c = _find_command_name(str)))
return c->name;
return NULL;
} }
static int _is_opt_name(char *str) static int _is_opt_name(char *str)
@ -1306,6 +1295,7 @@ int define_commands(struct cmd_context *cmdtool, const char *run_name)
int copy_pos = 0; int copy_pos = 0;
int skip = 0; int skip = 0;
int i; int i;
int lvm_command_enum;
memset(&commands, 0, sizeof(commands)); memset(&commands, 0, sizeof(commands));
@ -1328,21 +1318,21 @@ int define_commands(struct cmd_context *cmdtool, const char *run_name)
continue; continue;
/* New cmd def begins: command_name <required opt/pos args> */ /* New cmd def begins: command_name <required opt/pos args> */
if ((name = _is_command_name(line_argv[0]))) { if (islower(line_argv[0][0]) && /* All commands are lower-case only */
if (cmd_count >= COMMAND_COUNT) { ((lvm_command_enum = _find_lvm_command_enum(line_argv[0])) < LVM_COMMAND_COUNT)) {
if (cmd_count >= COMMAND_COUNT)
return 0; return 0;
}
/* /*
* FIXME: when running one specific command name, * FIXME: when running one specific command name,
* we can optimize by not parsing command defs * we can optimize by not parsing command defs
* that don't start with that command name. * that don't start with that command name.
*/ */
cmd = &commands[cmd_count]; cmd = &commands[cmd_count];
cmd->command_index = cmd_count; cmd->command_index = cmd_count;
cmd_count++; cmd_count++;
cmd->name = name; cmd->lvm_command_enum = lvm_command_enum;
cmd->name = name = command_names[lvm_command_enum].name;
if (run_name && strcmp(run_name, name)) { if (run_name && strcmp(run_name, name)) {
skip = 1; skip = 1;
@ -1724,8 +1714,8 @@ static void _print_usage_def(struct command *cmd, int opt_enum, struct arg_def *
void print_usage(struct command *cmd, int longhelp, int desc_first) void print_usage(struct command *cmd, int longhelp, int desc_first)
{ {
const struct command_name *cname = _find_command_name(cmd->name); const struct command_name *cname = &command_names[cmd->lvm_command_enum];
const struct command_name_args *cna = (cname) ? &command_names_args[cname->lvm_command_enum] : NULL; const struct command_name_args *cna = &command_names_args[cname->lvm_command_enum];
int any_req = (cmd->cmd_flags & CMD_FLAG_ANY_REQUIRED_OPT) ? 1 : 0; int any_req = (cmd->cmd_flags & CMD_FLAG_ANY_REQUIRED_OPT) ? 1 : 0;
int include_extents = 0; int include_extents = 0;
int ro, rp, oo, op, opt_enum, first; int ro, rp, oo, op, opt_enum, first;

View File

@ -193,6 +193,7 @@ struct command {
command_fn fn; /* old style */ command_fn fn; /* old style */
unsigned int cmd_flags; /* CMD_FLAG_ */ unsigned int cmd_flags; /* CMD_FLAG_ */
uint16_t lvm_command_enum; /* position in commands[] */
/* definitions of opt/pos args */ /* definitions of opt/pos args */

View File

@ -346,8 +346,8 @@ static const char *_man_long_opt_name(const char *cmdname, int opt_enum)
static void _print_man_usage(char *lvmname, struct command *cmd) static void _print_man_usage(char *lvmname, struct command *cmd)
{ {
const struct command_name *cname; const struct command_name *cname = &command_names[cmd->lvm_command_enum];
const struct command_name_args *cna; const struct command_name_args *cna = &command_names_args[cmd->lvm_command_enum];
int any_req = (cmd->cmd_flags & CMD_FLAG_ANY_REQUIRED_OPT) ? 1 : 0; int any_req = (cmd->cmd_flags & CMD_FLAG_ANY_REQUIRED_OPT) ? 1 : 0;
int sep, ro, rp, oo, op, opt_enum; int sep, ro, rp, oo, op, opt_enum;
int need_ro_indent_end = 0; int need_ro_indent_end = 0;
@ -356,11 +356,6 @@ static void _print_man_usage(char *lvmname, struct command *cmd)
uint64_t lv_type_bits = 0; uint64_t lv_type_bits = 0;
_was_hyphen = 0; _was_hyphen = 0;
if (!(cname = _find_command_name(cmd->name)))
return;
cna = &command_names_args[cname->lvm_command_enum];
printf("\\fB%s\\fP", lvmname); printf("\\fB%s\\fP", lvmname);
if (!any_req) if (!any_req)
@ -712,12 +707,9 @@ out:
static void _print_man_usage_common_lvm(struct command *cmd) static void _print_man_usage_common_lvm(struct command *cmd)
{ {
const struct command_name *cname; const struct command_name *cname = &command_names[cmd->lvm_command_enum];
int i, sep, oo, opt_enum; int i, sep, oo, opt_enum;
if (!(cname = _find_command_name(cmd->name)))
return;
printf("Common options for lvm:\n"); printf("Common options for lvm:\n");
printf(".\n"); printf(".\n");
@ -796,16 +788,11 @@ static void _print_man_usage_common_lvm(struct command *cmd)
static void _print_man_usage_common_cmd(struct command *cmd) static void _print_man_usage_common_cmd(struct command *cmd)
{ {
const struct command_name *cname; const struct command_name *cname = &command_names[cmd->lvm_command_enum];
const struct command_name_args *cna; const struct command_name_args *cna = &command_names_args[cmd->lvm_command_enum];
int i, sep, oo, opt_enum; int i, sep, oo, opt_enum;
int found_common_command = 0; int found_common_command = 0;
if (!(cname = _find_command_name(cmd->name)))
return;
cna = &command_names_args[cname->lvm_command_enum];
if (cna->variants < 2) if (cna->variants < 2)
return; return;
@ -1369,7 +1356,7 @@ static int _print_man(char *name, char *des_file, int secondary)
name += 4; name += 4;
} }
cname = _find_command_name(name); cname = find_command_name(name);
printf(".TH %s 8 \"LVM TOOLS #VERSION#\" \"Red Hat, Inc.\"\n", printf(".TH %s 8 \"LVM TOOLS #VERSION#\" \"Red Hat, Inc.\"\n",
_upper_command_name(lvmname)); _upper_command_name(lvmname));
@ -1409,10 +1396,8 @@ static int _print_man(char *name, char *des_file, int secondary)
printf(".\n.SH SYNOPSIS\n.\n"); printf(".\n.SH SYNOPSIS\n.\n");
prev_cmd = cmd; prev_cmd = cmd;
if (!(cname = _find_command_name(cmd->name))) cname = &command_names[cmd->lvm_command_enum];
return 0; cna = &command_names_args[cmd->lvm_command_enum];
cna = &command_names_args[cname->lvm_command_enum];
if (cna->variant_has_ro && cna->variant_has_rp) if (cna->variant_has_ro && cna->variant_has_rp)
printf("\\fB%s\\fP \\fIoption_args\\fP \\fIposition_args\\fP\n", lvmname); printf("\\fB%s\\fP \\fIoption_args\\fP \\fIposition_args\\fP\n", lvmname);