Skip to content

Commit e625f5f

Browse files
committed
optimize more
1 parent eb7d8f8 commit e625f5f

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

common/common.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params, std::vecto
373373
}
374374
} catch (std::exception & e) {
375375
throw std::invalid_argument(format(
376-
"error while handling environment variable \"%s\": %s\n\n", opt.env.c_str(), e.what()));
376+
"error while handling environment variable \"%s\": %s\n\n", opt.env, e.what()));
377377
}
378378
}
379379
}
@@ -395,7 +395,7 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params, std::vecto
395395
}
396396
auto opt = *arg_to_options[arg];
397397
if (opt.has_value_from_env()) {
398-
fprintf(stderr, "warn: %s environment variable is set, but will be overwritten by command line argument %s\n", opt.env.c_str(), arg.c_str());
398+
fprintf(stderr, "warn: %s environment variable is set, but will be overwritten by command line argument %s\n", opt.env, arg.c_str());
399399
}
400400
try {
401401
if (opt.handler_void) {
@@ -595,15 +595,19 @@ std::string llama_arg::to_string() {
595595
std::string leading_spaces(n_leading_spaces, ' ');
596596

597597
std::ostringstream ss;
598-
for (const auto & arg : args) {
598+
for (const auto arg : args) {
599599
if (arg == args.front()) {
600-
ss << (args.size() == 1 ? arg : format("%-7s", (arg + ",").c_str()));
600+
if (args.size() == 1) {
601+
ss << arg;
602+
} else {
603+
ss << format("%-7s", arg) << ", ";
604+
}
601605
} else {
602606
ss << arg << (arg != args.back() ? ", " : "");
603607
}
604608
}
605-
if (!value_hint.empty()) ss << " " << value_hint;
606-
if (!value_hint_2.empty()) ss << " " << value_hint_2;
609+
if (value_hint) ss << " " << value_hint;
610+
if (value_hint_2) ss << " " << value_hint_2;
607611
if (ss.tellp() > n_leading_spaces - 3) {
608612
// current line is too long, add new line
609613
ss << "\n" << leading_spaces;
@@ -675,7 +679,7 @@ std::vector<llama_arg> gpt_params_parser_init(gpt_params & params, llama_example
675679
if (seen_args.find(a) == seen_args.end()) {
676680
seen_args.insert(a);
677681
} else {
678-
throw std::runtime_error(format("found duplicated argument in source code: %s", a.c_str()));
682+
throw std::runtime_error(format("found duplicated argument in source code: %s", a));
679683
}
680684
}
681685
options.push_back(std::move(arg));
@@ -693,7 +697,7 @@ std::vector<llama_arg> gpt_params_parser_init(gpt_params & params, llama_example
693697
add_opt(llama_arg(
694698
{"--version"},
695699
"show version and build info",
696-
[](gpt_params & params) {
700+
[](gpt_params &) {
697701
fprintf(stderr, "version: %d (%s)\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT);
698702
fprintf(stderr, "built with %s for %s\n", LLAMA_COMPILER, LLAMA_BUILD_TARGET);
699703
exit(0);
@@ -2248,32 +2252,32 @@ std::vector<llama_arg> gpt_params_parser_init(gpt_params & params, llama_example
22482252
add_opt(llama_arg(
22492253
{"--log-test"},
22502254
"Log test",
2251-
[](gpt_params & params) { log_param_single_parse("--log-test"); }
2255+
[](gpt_params &) { log_param_single_parse("--log-test"); }
22522256
));
22532257
add_opt(llama_arg(
22542258
{"--log-disable"},
22552259
"Log disable",
2256-
[](gpt_params & params) { log_param_single_parse("--log-disable"); }
2260+
[](gpt_params &) { log_param_single_parse("--log-disable"); }
22572261
));
22582262
add_opt(llama_arg(
22592263
{"--log-enable"},
22602264
"Log enable",
2261-
[](gpt_params & params) { log_param_single_parse("--log-enable"); }
2265+
[](gpt_params &) { log_param_single_parse("--log-enable"); }
22622266
));
22632267
add_opt(llama_arg(
22642268
{"--log-new"},
22652269
"Log new",
2266-
[](gpt_params & params) { log_param_single_parse("--log-new"); }
2270+
[](gpt_params &) { log_param_single_parse("--log-new"); }
22672271
));
22682272
add_opt(llama_arg(
22692273
{"--log-append"},
22702274
"Log append",
2271-
[](gpt_params & params) { log_param_single_parse("--log-append"); }
2275+
[](gpt_params &) { log_param_single_parse("--log-append"); }
22722276
));
22732277
add_opt(llama_arg(
22742278
{"--log-file"}, "FNAME",
22752279
"Log file",
2276-
[](gpt_params & params, const std::string & value) { log_param_pair_parse(false, "--log-file", value); }
2280+
[](gpt_params &, const std::string & value) { log_param_pair_parse(false, "--log-file", value); }
22772281
));
22782282
#endif // LOG_DISABLE_LOGS
22792283

common/common.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -305,53 +305,53 @@ struct gpt_params {
305305

306306
struct llama_arg {
307307
std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON};
308-
std::vector<std::string> args;
309-
std::string value_hint; // help text or example for arg value
310-
std::string value_hint_2; // for second arg value
311-
std::string env;
308+
std::vector<const char *> args;
309+
const char * value_hint = nullptr; // help text or example for arg value
310+
const char * value_hint_2 = nullptr; // for second arg value
311+
const char * env = nullptr;
312312
std::string help;
313313
void (*handler_void) (gpt_params & params) = nullptr;
314314
void (*handler_string) (gpt_params & params, const std::string &) = nullptr;
315315
void (*handler_str_str)(gpt_params & params, const std::string &, const std::string &) = nullptr;
316316
void (*handler_int) (gpt_params & params, int) = nullptr;
317317

318318
llama_arg(
319-
const std::initializer_list<std::string> & args,
320-
const std::string & value_hint,
319+
const std::initializer_list<const char *> & args,
320+
const char * value_hint,
321321
const std::string & help,
322322
void (*handler)(gpt_params & params, const std::string &)
323323
) : args(args), value_hint(value_hint), help(help), handler_string(handler) {}
324324

325325
llama_arg(
326-
const std::initializer_list<std::string> & args,
327-
const std::string & value_hint,
326+
const std::initializer_list<const char *> & args,
327+
const char * value_hint,
328328
const std::string & help,
329329
void (*handler)(gpt_params & params, int)
330330
) : args(args), value_hint(value_hint), help(help), handler_int(handler) {}
331331

332332
llama_arg(
333-
const std::initializer_list<std::string> & args,
333+
const std::initializer_list<const char *> & args,
334334
const std::string & help,
335335
void (*handler)(gpt_params & params)
336336
) : args(args), help(help), handler_void(handler) {}
337337

338338
// support 2 values for arg
339339
llama_arg(
340-
const std::initializer_list<std::string> & args,
341-
const std::string & value_hint,
342-
const std::string & value_hint_2,
340+
const std::initializer_list<const char *> & args,
341+
const char * value_hint,
342+
const char * value_hint_2,
343343
const std::string & help,
344344
void (*handler)(gpt_params & params, const std::string &, const std::string &)
345345
) : args(args), value_hint(value_hint), value_hint_2(value_hint_2), help(help), handler_str_str(handler) {}
346346

347-
llama_arg & set_examples(std::set<enum llama_example> examples) {
347+
llama_arg & set_examples(std::initializer_list<enum llama_example> examples) {
348348
this->examples = std::move(examples);
349349
return *this;
350350
}
351351

352-
llama_arg & set_env(std::string env) {
352+
llama_arg & set_env(const char * env) {
353353
help = help + "\n(env: " + env + ")";
354-
this->env = std::move(env);
354+
this->env = env;
355355
return *this;
356356
}
357357

@@ -360,8 +360,8 @@ struct llama_arg {
360360
}
361361

362362
bool get_value_from_env(std::string & output) const {
363-
if (env.empty()) return false;
364-
char * value = std::getenv(env.c_str());
363+
if (env == nullptr) return false;
364+
char * value = std::getenv(env);
365365
if (value) {
366366
output = value;
367367
return true;
@@ -370,7 +370,7 @@ struct llama_arg {
370370
}
371371

372372
bool has_value_from_env() const {
373-
return std::getenv(env.c_str());
373+
return env != nullptr && std::getenv(env);
374374
}
375375

376376
std::string to_string();

examples/export-docs/export-docs.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ static void export_md(std::string fname, llama_example ex) {
2222
// args
2323
for (const auto & arg : opt.args) {
2424
if (arg == opt.args.front()) {
25-
file << (opt.args.size() == 1 ? arg : (arg + ", "));
25+
file << arg;
26+
if (opt.args.size() > 1) file << ", ";
2627
} else {
2728
file << arg << (arg != opt.args.back() ? ", " : "");
2829
}
2930
}
3031
// value hint
31-
if (!opt.value_hint.empty()) {
32+
if (opt.value_hint) {
3233
std::string md_value_hint(opt.value_hint);
3334
string_replace_all(md_value_hint, "|", "\\|");
3435
file << " " << md_value_hint;
3536
}
36-
if (!opt.value_hint_2.empty()) {
37+
if (opt.value_hint_2) {
3738
std::string md_value_hint_2(opt.value_hint_2);
3839
string_replace_all(md_value_hint_2, "|", "\\|");
3940
file << " " << md_value_hint_2;

0 commit comments

Comments
 (0)