From 66c8bb05b222372a1b3b281bcf8e82cb85c8f26a Mon Sep 17 00:00:00 2001 From: Herman Semenov Date: Mon, 26 Aug 2024 02:19:18 +0300 Subject: [PATCH] common,train,examples: using constexpr string and strlen for microoptimizations --- common/common.cpp | 4 ++-- common/train.cpp | 4 ++-- .../convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp | 4 ++-- examples/cvector-generator/cvector-generator.cpp | 8 ++++---- examples/gguf-hash/gguf-hash.cpp | 4 ++-- examples/gguf-split/gguf-split.cpp | 4 ++-- examples/llama-bench/llama-bench.cpp | 4 ++-- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index 59e829660..9ca7ded38 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -280,12 +280,12 @@ void gpt_params_handle_model_default(gpt_params & params) { bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) { bool invalid_param = false; std::string arg; - const std::string arg_prefix = "--"; + static constexpr auto arg_prefix = "--"; llama_sampling_params & sparams = params.sparams; for (int i = 1; i < argc; i++) { arg = argv[i]; - if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { + if (arg.compare(0, std::char_traits::length(arg_prefix), arg_prefix) == 0) { std::replace(arg.begin(), arg.end(), '_', '-'); } if (!gpt_params_find_arg(argc, argv, arg, params, i, invalid_param)) { diff --git a/common/train.cpp b/common/train.cpp index fef1e57c9..d59e8617b 100644 --- a/common/train.cpp +++ b/common/train.cpp @@ -1147,8 +1147,8 @@ bool consume_common_train_arg( ) { int& i = *idx; std::string arg = argv[i]; - const std::string arg_prefix = "--"; - if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { + static constexpr auto arg_prefix = "--"; + if (arg.compare(0, std::char_traits::length(arg_prefix), arg_prefix) == 0) { std::replace(arg.begin(), arg.end(), '_', '-'); } if (arg == "--train-data") { diff --git a/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp b/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp index 8ca9f8915..0d09c3043 100644 --- a/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp +++ b/examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp @@ -812,11 +812,11 @@ static bool params_parse(int argc, char ** argv, struct train_params * params) { bool reqd_param_found = false; std::string arg; struct train_params default_params = get_default_train_params(); - const std::string arg_prefix = "--"; + static constexpr auto arg_prefix = "--"; for (int i = 1; i < argc; i++) { arg = argv[i]; - if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { + if (arg.compare(0, std::char_traits::length(arg_prefix), arg_prefix) == 0) { std::replace(arg.begin(), arg.end(), '_', '-'); } diff --git a/examples/cvector-generator/cvector-generator.cpp b/examples/cvector-generator/cvector-generator.cpp index 8fa492571..c113626e0 100644 --- a/examples/cvector-generator/cvector-generator.cpp +++ b/examples/cvector-generator/cvector-generator.cpp @@ -349,10 +349,10 @@ static bool get_hidden_layers(llama_context * ctx, std::vector & to static void export_gguf(const std::vector & v_ctrl, const std::string fname, const std::string model_hint) { struct gguf_context * ctx = gguf_init_empty(); - const std::string arch = "controlvector"; - gguf_set_val_str(ctx, "general.architecture", arch.c_str()); - gguf_set_val_str(ctx, (arch + ".model_hint").c_str(), model_hint.c_str()); - gguf_set_val_i32(ctx, (arch + ".layer_count").c_str(), v_ctrl.size()); + static constexpr auto arch = "controlvector"; + gguf_set_val_str(ctx, "general.architecture", arch); + gguf_set_val_str(ctx, (std::string{arch} + ".model_hint").c_str(), model_hint.c_str()); + gguf_set_val_i32(ctx, (std::string{arch} + ".layer_count").c_str(), v_ctrl.size()); for (size_t i = 0; i < v_ctrl.size(); ++i) { gguf_add_tensor(ctx, v_ctrl[i]); diff --git a/examples/gguf-hash/gguf-hash.cpp b/examples/gguf-hash/gguf-hash.cpp index e96c75117..6f640f414 100644 --- a/examples/gguf-hash/gguf-hash.cpp +++ b/examples/gguf-hash/gguf-hash.cpp @@ -115,12 +115,12 @@ static void hash_print_usage(const char * executable) { static void hash_params_parse_ex(int argc, const char ** argv, hash_params & params) { std::string arg; bool invalid_param = false; - const std::string arg_prefix = "--"; + static constexpr auto arg_prefix = "--"; int arg_idx = 1; for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) { arg = argv[arg_idx]; - if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { + if (arg.compare(0, std::char_traits::length(arg_prefix), arg_prefix) == 0) { std::replace(arg.begin(), arg.end(), '_', '-'); } diff --git a/examples/gguf-split/gguf-split.cpp b/examples/gguf-split/gguf-split.cpp index 881f0451c..2333a4989 100644 --- a/examples/gguf-split/gguf-split.cpp +++ b/examples/gguf-split/gguf-split.cpp @@ -76,13 +76,13 @@ static size_t split_str_to_n_bytes(std::string str) { static void split_params_parse_ex(int argc, const char ** argv, split_params & params) { std::string arg; - const std::string arg_prefix = "--"; + static constexpr auto arg_prefix = "--"; bool invalid_param = false; int arg_idx = 1; for (; arg_idx < argc && strncmp(argv[arg_idx], "--", 2) == 0; arg_idx++) { arg = argv[arg_idx]; - if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { + if (arg.compare(0, std::char_traits::length(arg_prefix), arg_prefix) == 0) { std::replace(arg.begin(), arg.end(), '_', '-'); } diff --git a/examples/llama-bench/llama-bench.cpp b/examples/llama-bench/llama-bench.cpp index 42918bfc7..6064e58f2 100644 --- a/examples/llama-bench/llama-bench.cpp +++ b/examples/llama-bench/llama-bench.cpp @@ -330,7 +330,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) { cmd_params params; std::string arg; bool invalid_param = false; - const std::string arg_prefix = "--"; + static constexpr auto arg_prefix = "--"; const char split_delim = ','; params.verbose = cmd_params_defaults.verbose; @@ -341,7 +341,7 @@ static cmd_params parse_cmd_params(int argc, char ** argv) { for (int i = 1; i < argc; i++) { arg = argv[i]; - if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { + if (arg.compare(0, std::char_traits::length(arg_prefix), arg_prefix) == 0) { std::replace(arg.begin(), arg.end(), '_', '-'); }