From: Zdenek Kabelac Date: Sun, 12 May 2024 13:39:18 +0000 (+0200) Subject: command: refactor code for simplier lookup X-Git-Tag: v2_03_24~34 X-Git-Url: https://sourceware.org/git/?a=commitdiff_plain;h=5840f90e828632f96fdce36a529fc2df1e743ba2;p=lvm2.git 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'. --- diff --git a/tools/command.c b/tools/command.c index 477604c75..712a5d161 100644 --- a/tools/command.c +++ b/tools/command.c @@ -377,7 +377,7 @@ static uint64_t _lv_to_bits(struct command *cmd, char *name) 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; int first = 0, last, middle; @@ -408,28 +408,17 @@ const struct command_name *find_command_name(const char *name) else if (i > 0) last = middle - 1; else - return &command_names[middle]; + return middle; } - return NULL; -} - -static const struct command_name *_find_command_name(const char *name) -{ - if (!islower(name[0])) - return NULL; /* Commands starts with lower-case */ - - return find_command_name(name); + return -1; } -static const char *_is_command_name(char *str) +const struct command_name *find_command_name(const char *name) { - const struct command_name *c; - - if ((c = _find_command_name(str))) - return c->name; + int r = _find_lvm_command_enum(name); - return NULL; + return (r < LVM_COMMAND_COUNT) ? &command_names[r] : NULL; } 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 skip = 0; int i; + int lvm_command_enum; memset(&commands, 0, sizeof(commands)); @@ -1328,21 +1318,21 @@ int define_commands(struct cmd_context *cmdtool, const char *run_name) continue; /* New cmd def begins: command_name */ - if ((name = _is_command_name(line_argv[0]))) { - if (cmd_count >= COMMAND_COUNT) { + if (islower(line_argv[0][0]) && /* All commands are lower-case only */ + ((lvm_command_enum = _find_lvm_command_enum(line_argv[0])) < LVM_COMMAND_COUNT)) { + if (cmd_count >= COMMAND_COUNT) return 0; - } /* * FIXME: when running one specific command name, * we can optimize by not parsing command defs * that don't start with that command name. */ - cmd = &commands[cmd_count]; cmd->command_index = 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)) { 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) { - const struct command_name *cname = _find_command_name(cmd->name); - const struct command_name_args *cna = (cname) ? &command_names_args[cname->lvm_command_enum] : NULL; + const struct command_name *cname = &command_names[cmd->lvm_command_enum]; + 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 include_extents = 0; int ro, rp, oo, op, opt_enum, first; diff --git a/tools/command.h b/tools/command.h index b0492a316..ef7a5d77c 100644 --- a/tools/command.h +++ b/tools/command.h @@ -193,6 +193,7 @@ struct command { command_fn fn; /* old style */ unsigned int cmd_flags; /* CMD_FLAG_ */ + uint16_t lvm_command_enum; /* position in commands[] */ /* definitions of opt/pos args */ diff --git a/tools/man-generator.c b/tools/man-generator.c index 32fbd9982..bc2b1770b 100644 --- a/tools/man-generator.c +++ b/tools/man-generator.c @@ -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) { - const struct command_name *cname; - const struct command_name_args *cna; + const struct command_name *cname = &command_names[cmd->lvm_command_enum]; + 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 sep, ro, rp, oo, op, opt_enum; 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; _was_hyphen = 0; - if (!(cname = _find_command_name(cmd->name))) - return; - - cna = &command_names_args[cname->lvm_command_enum]; - printf("\\fB%s\\fP", lvmname); if (!any_req) @@ -712,12 +707,9 @@ out: 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; - if (!(cname = _find_command_name(cmd->name))) - return; - printf("Common options for lvm:\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) { - const struct command_name *cname; - const struct command_name_args *cna; + const struct command_name *cname = &command_names[cmd->lvm_command_enum]; + const struct command_name_args *cna = &command_names_args[cmd->lvm_command_enum]; int i, sep, oo, opt_enum; 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) return; @@ -1369,7 +1356,7 @@ static int _print_man(char *name, char *des_file, int secondary) name += 4; } - cname = _find_command_name(name); + cname = find_command_name(name); printf(".TH %s 8 \"LVM TOOLS #VERSION#\" \"Red Hat, Inc.\"\n", _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"); prev_cmd = cmd; - if (!(cname = _find_command_name(cmd->name))) - return 0; - - cna = &command_names_args[cname->lvm_command_enum]; + cname = &command_names[cmd->lvm_command_enum]; + cna = &command_names_args[cmd->lvm_command_enum]; if (cna->variant_has_ro && cna->variant_has_rp) printf("\\fB%s\\fP \\fIoption_args\\fP \\fIposition_args\\fP\n", lvmname);