From d629986df743bbe030475902cf6e68fc377edbc1 Mon Sep 17 00:00:00 2001 From: tanruixiang <819464715@qq.com> Date: Sun, 13 Nov 2022 11:47:54 +0800 Subject: [PATCH] Fix performance-inefficient-vector-operation warning reported by clang-tidy --- .clang-tidy | 2 +- src/commands/redis_cmd.cc | 4 ++++ src/types/redis_string.cc | 1 + src/types/redis_zset.cc | 1 + 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 4f70482cacc..db994ff2bf0 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,7 +1,7 @@ # refer to https://clang.llvm.org/extra/clang-tidy/checks/list.html Checks: -*, clang-analyzer-core.*, clang-analyzer-cplusplus.*, clang-analyzer-deadcode.*, clang-analyzer-nullability.*, clang-analyzer-security.*, clang-analyzer-unix.*, clang-analyzer-valist.*, cppcoreguidelines-init-variables, cppcoreguidelines-macro-usage, cppcoreguidelines-interfaces-global-init, cppcoreguidelines-narrowing-conversions, cppcoreguidelines-no-malloc, cppcoreguidelines-prefer-member-initializer, cppcoreguidelines-special-member-functions, cppcoreguidelines-slicing, google-build-explicit-make-pair, google-default-arguments, google-explicit-constructor, modernize-avoid-bind, modernize-loop-convert, modernize-macro-to-enum, modernize-make-shared, modernize-make-unique, modernize-pass-by-value, modernize-redundant-void-arg, modernize-return-braced-init-list, modernize-use-auto, modernize-use-bool-literals, modernize-use-emplace, modernize-use-equals-default, modernize-use-equals-delete, modernize-use-nullptr, modernize-use-override, modernize-use-using, performance-faster-string-find, performance-for-range-copy, performance-implicit-conversion-in-loop, performance-inefficient-algorithm, performance-inefficient-vector-operation, performance-move-const-arg, performance-move-constructor-init, performance-no-automatic-move, performance-trivially-destructible, performance-type-promotion-in-math-fn, performance-unnecessary-copy-initialization, performance-unnecessary-value-param -WarningsAsErrors: clang-analyzer-*, -clang-analyzer-security.insecureAPI.rand, cppcoreguidelines-interfaces-global-init, cppcoreguidelines-no-malloc, cppcoreguidelines-slicing, google-*, modernize-use-emplace, modernize-use-equals-default, modernize-use-equals-delete, performance-implicit-conversion-in-loop, performance-inefficient-algorithm, performance-move-constructor-init, performance-no-automatic-move, performance-trivially-destructible, performance-type-promotion-in-math-fn, performance-unnecessary-copy-initialization +WarningsAsErrors: clang-analyzer-*, -clang-analyzer-security.insecureAPI.rand, cppcoreguidelines-interfaces-global-init, cppcoreguidelines-no-malloc, cppcoreguidelines-slicing, google-*, modernize-use-emplace, modernize-use-equals-default, modernize-use-equals-delete, performance-implicit-conversion-in-loop, performance-inefficient-algorithm, performance-move-constructor-init, performance-no-automatic-move, performance-trivially-destructible, performance-type-promotion-in-math-fn, performance-unnecessary-copy-initialization, performance-inefficient-vector-operation CheckOptions: - key: cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor diff --git a/src/commands/redis_cmd.cc b/src/commands/redis_cmd.cc index 941ac4cc94f..79da321f137 100644 --- a/src/commands/redis_cmd.cc +++ b/src/commands/redis_cmd.cc @@ -1458,6 +1458,7 @@ class CommandHKeys : public Commander { return Status(Status::RedisExecErr, s.ToString()); } std::vector keys; + keys.reserve(field_values.size()); for (const auto &fv : field_values) { keys.emplace_back(fv.field); } @@ -1476,6 +1477,7 @@ class CommandHVals : public Commander { return Status(Status::RedisExecErr, s.ToString()); } std::vector values; + values.reserve(field_values.size()); for (const auto &p : field_values) { values.emplace_back(p.value); } @@ -4275,6 +4277,7 @@ class CommandCommand : public Commander { return Status::OK(); } std::vector keys; + keys.reserve(keys_indexes.size()); for (const auto &key_index : keys_indexes) { keys.emplace_back(args_[key_index + 2]); } @@ -4558,6 +4561,7 @@ class CommandZScan : public CommandSubkeyScanBase { return Status(Status::RedisExecErr, s.ToString()); } std::vector score_strings; + score_strings.reserve(scores.size()); for (const auto &score : scores) { score_strings.emplace_back(Util::Float2String(score)); } diff --git a/src/types/redis_string.cc b/src/types/redis_string.cc index eefb766af97..aaffdfb57f0 100644 --- a/src/types/redis_string.cc +++ b/src/types/redis_string.cc @@ -398,6 +398,7 @@ rocksdb::Status String::MSetNX(const std::vector &pairs, int ttl, in int exists; std::vector keys; + keys.reserve(pairs.size()); for (StringPair pair : pairs) { keys.emplace_back(pair.key); } diff --git a/src/types/redis_zset.cc b/src/types/redis_zset.cc index 558f25ab27b..95a0509d71c 100644 --- a/src/types/redis_zset.cc +++ b/src/types/redis_zset.cc @@ -751,6 +751,7 @@ rocksdb::Status ZSet::UnionStore(const Slice &dst, const std::vector } if (!dst_zset.empty()) { std::vector mscores; + mscores.reserve(dst_zset.size()); for (const auto &iter : dst_zset) { mscores.emplace_back(MemberScore{iter.first, iter.second}); }