diff --git a/internal/core/src/common/CGoHelper.h b/internal/core/src/common/CGoHelper.h index 021f21355f..8f8ce80032 100644 --- a/internal/core/src/common/CGoHelper.h +++ b/internal/core/src/common/CGoHelper.h @@ -29,8 +29,8 @@ SuccessCStatus() { } inline CStatus -FailureCStatus(ErrorCode error_code, const std::string& str) { - auto str_dup = strdup(str.c_str()); +FailureCStatus(ErrorCode error_code, const std::string_view str) { + auto str_dup = strdup(str.data()); return CStatus{error_code, str_dup}; } diff --git a/internal/core/src/common/RangeSearchHelper.cpp b/internal/core/src/common/RangeSearchHelper.cpp index d72102ae39..58ece0c179 100644 --- a/internal/core/src/common/RangeSearchHelper.cpp +++ b/internal/core/src/common/RangeSearchHelper.cpp @@ -25,7 +25,7 @@ DatasetPtr SortRangeSearchResult(DatasetPtr data_set, int64_t topk, int64_t nq, - std::string metric_type) { + const std::string_view metric_type) { /** * nq: number of queries; * lims: the size of lims is nq + 1, lims[i+1] - lims[i] refers to the size of RangeSearch result queries[i] @@ -104,7 +104,7 @@ SortRangeSearchResult(DatasetPtr data_set, void CheckRangeSearchParam(float radius, float range_filter, - std::string metric_type) { + const std::string_view metric_type) { /* * IP: 1.0 range_filter radius * |------------+---------------| min_heap descending_order diff --git a/internal/core/src/common/RangeSearchHelper.h b/internal/core/src/common/RangeSearchHelper.h index 921c1a6138..b401be04c3 100644 --- a/internal/core/src/common/RangeSearchHelper.h +++ b/internal/core/src/common/RangeSearchHelper.h @@ -20,10 +20,10 @@ DatasetPtr SortRangeSearchResult(DatasetPtr data_set, int64_t topk, int64_t nq, - std::string metric_type); + const std::string_view metric_type); void CheckRangeSearchParam(float radius, float range_filter, - std::string metric_type); + const std::string_view metric_type); } // namespace milvus diff --git a/internal/core/src/common/Types.h b/internal/core/src/common/Types.h index 6a343a6292..fbbb20292b 100644 --- a/internal/core/src/common/Types.h +++ b/internal/core/src/common/Types.h @@ -16,27 +16,28 @@ #pragma once -#include -#include -#include -#include -#include -#include #include #include + +#include #include #include #include -#include +#include +#include +#include +#include +#include #include +#include -#include "nlohmann/json.hpp" -#include "knowhere/comp/index_param.h" #include "knowhere/binaryset.h" +#include "knowhere/comp/index_param.h" #include "knowhere/dataset.h" +#include "nlohmann/json.hpp" +#include "pb/plan.pb.h" #include "pb/schema.pb.h" #include "pb/segcore.pb.h" -#include "pb/plan.pb.h" namespace milvus { @@ -132,4 +133,23 @@ using IndexType = knowhere::IndexType; // TODO :: type define milvus index mode, add transfer func from milvus index mode to knowhere index mode using IndexMode = knowhere::IndexMode; +// Plus 1 because we can't use greater(>) symbol +constexpr size_t REF_SIZE_THRESHOLD = 16 + 1; +template +using MayRef = std::conditional_t || + sizeof(T) >= REF_SIZE_THRESHOLD, + T&, + T>; +template +using Parameter = std:: + conditional_t, std::string_view, MayRef>; + +static_assert(std::is_same_v>); +static_assert(std::is_same_v>); + +struct LargeType { + int64_t x, y, z; +}; +static_assert(std::is_same_v>); + } // namespace milvus diff --git a/internal/core/src/common/Utils.h b/internal/core/src/common/Utils.h index bc6d14b8fd..f14d665284 100644 --- a/internal/core/src/common/Utils.h +++ b/internal/core/src/common/Utils.h @@ -91,13 +91,13 @@ GenResultDataset(const int64_t nq, } inline bool -PostfixMatch(const std::string_view str, const std::string& postfix) { +PostfixMatch(const std::string_view str, const std::string_view postfix) { if (postfix.length() > str.length()) { return false; } int offset = str.length() - postfix.length(); - auto ret = strncmp(str.data() + offset, postfix.c_str(), postfix.length()); + auto ret = strncmp(str.data() + offset, postfix.data(), postfix.length()); if (ret != 0) { return false; } @@ -127,8 +127,9 @@ upper_div(int64_t value, int64_t align) { } inline bool -IsMetricType(const std::string& str, const knowhere::MetricType& metric_type) { - return !strcasecmp(str.c_str(), metric_type.c_str()); +IsMetricType(const std::string_view str, + const knowhere::MetricType& metric_type) { + return !strcasecmp(str.data(), metric_type.c_str()); } inline bool diff --git a/internal/core/src/config/ConfigChunkManager.cpp b/internal/core/src/config/ConfigChunkManager.cpp index 87de8a0f7f..ed5c2d5f8e 100644 --- a/internal/core/src/config/ConfigChunkManager.cpp +++ b/internal/core/src/config/ConfigChunkManager.cpp @@ -21,7 +21,7 @@ namespace milvus::ChunkMangerConfig { std::string LOCAL_ROOT_PATH = "/tmp/milvus"; // NOLINT void -SetLocalRootPath(const std::string& path_prefix) { +SetLocalRootPath(const std::string_view path_prefix) { LOCAL_ROOT_PATH = path_prefix; } diff --git a/internal/core/src/config/ConfigChunkManager.h b/internal/core/src/config/ConfigChunkManager.h index 40ccec2dee..f0cc57b858 100644 --- a/internal/core/src/config/ConfigChunkManager.h +++ b/internal/core/src/config/ConfigChunkManager.h @@ -21,7 +21,7 @@ namespace milvus::ChunkMangerConfig { void -SetLocalRootPath(const std::string& path_prefix); +SetLocalRootPath(const std::string_view path_prefix); std::string GetLocalRootPath(); diff --git a/internal/core/src/index/StringIndex.h b/internal/core/src/index/StringIndex.h index 266832e018..c46b7a126f 100644 --- a/internal/core/src/index/StringIndex.h +++ b/internal/core/src/index/StringIndex.h @@ -40,7 +40,7 @@ class StringIndex : public ScalarIndex { } virtual const TargetBitmapPtr - PrefixMatch(std::string prefix) = 0; + PrefixMatch(const std::string_view prefix) = 0; }; using StringIndexPtr = std::unique_ptr; } // namespace milvus::index diff --git a/internal/core/src/index/StringIndexMarisa.cpp b/internal/core/src/index/StringIndexMarisa.cpp index c6e1a9e4ed..026435decf 100644 --- a/internal/core/src/index/StringIndexMarisa.cpp +++ b/internal/core/src/index/StringIndexMarisa.cpp @@ -231,7 +231,7 @@ StringIndexMarisa::Range(std::string lower_bound_value, } const TargetBitmapPtr -StringIndexMarisa::PrefixMatch(std::string prefix) { +StringIndexMarisa::PrefixMatch(std::string_view prefix) { TargetBitmapPtr bitset = std::make_unique(str_ids_.size()); auto matched = prefix_match(prefix); for (const auto str_id : matched) { @@ -266,9 +266,9 @@ StringIndexMarisa::fill_offsets() { } size_t -StringIndexMarisa::lookup(const std::string& str) { +StringIndexMarisa::lookup(const std::string_view str) { marisa::Agent agent; - agent.set_query(str.c_str()); + agent.set_query(str.data()); if (trie_.lookup(agent)) { return agent.key().id(); } @@ -278,10 +278,10 @@ StringIndexMarisa::lookup(const std::string& str) { } std::vector -StringIndexMarisa::prefix_match(const std::string& prefix) { +StringIndexMarisa::prefix_match(const std::string_view prefix) { std::vector ret; marisa::Agent agent; - agent.set_query(prefix.c_str()); + agent.set_query(prefix.data()); while (trie_.predictive_search(agent)) { ret.push_back(agent.key().id()); } diff --git a/internal/core/src/index/StringIndexMarisa.h b/internal/core/src/index/StringIndexMarisa.h index 86d391170b..91cada0fb3 100644 --- a/internal/core/src/index/StringIndexMarisa.h +++ b/internal/core/src/index/StringIndexMarisa.h @@ -64,7 +64,7 @@ class StringIndexMarisa : public StringIndex { bool ub_inclusive) override; const TargetBitmapPtr - PrefixMatch(std::string prefix) override; + PrefixMatch(const std::string_view prefix) override; std::string Reverse_Lookup(size_t offset) const override; @@ -78,10 +78,10 @@ class StringIndexMarisa : public StringIndex { // get str_id by str, if str not found, -1 was returned. size_t - lookup(const std::string& str); + lookup(const std::string_view str); std::vector - prefix_match(const std::string& prefix); + prefix_match(const std::string_view prefix); private: Config config_; diff --git a/internal/core/src/index/Utils.cpp b/internal/core/src/index/Utils.cpp index 3da89612ac..33cac8e727 100644 --- a/internal/core/src/index/Utils.cpp +++ b/internal/core/src/index/Utils.cpp @@ -130,7 +130,7 @@ GetIndexModeFromConfig(const Config& config) { } IndexMode -GetIndexMode(const std::string index_mode) { +GetIndexMode(const std::string_view index_mode) { if (index_mode.compare("CPU") == 0 || index_mode.compare("cpu") == 0) { return IndexMode::MODE_CPU; } diff --git a/internal/core/src/index/Utils.h b/internal/core/src/index/Utils.h index 2d3153074d..8a8c84b026 100644 --- a/internal/core/src/index/Utils.h +++ b/internal/core/src/index/Utils.h @@ -112,7 +112,7 @@ IndexMode GetIndexModeFromConfig(const Config& config); IndexMode -GetIndexMode(const std::string index_mode); +GetIndexMode(const std::string_view index_mode); storage::FieldDataMeta GetFieldDataMetaFromConfig(const Config& config); diff --git a/internal/core/src/log/Log.cpp b/internal/core/src/log/Log.cpp index a9de58d3a3..7598284f8a 100644 --- a/internal/core/src/log/Log.cpp +++ b/internal/core/src/log/Log.cpp @@ -51,12 +51,12 @@ LogOut(const char* pattern, ...) { } void -SetThreadName(const std::string& name) { +SetThreadName(const std::string_view name) { // Note: the name cannot exceed 16 bytes #ifdef __APPLE__ - pthread_setname_np(name.c_str()); + pthread_setname_np(name.data()); #elif defined(__linux__) || defined(__MINGW64__) - pthread_setname_np(pthread_self(), name.c_str()); + pthread_setname_np(pthread_self(), name.data()); #else #error "Unsupported SetThreadName"; #endif diff --git a/internal/core/src/log/Log.h b/internal/core/src/log/Log.h index 2d087dd209..28e11f52cc 100644 --- a/internal/core/src/log/Log.h +++ b/internal/core/src/log/Log.h @@ -129,7 +129,7 @@ std::string LogOut(const char* pattern, ...); void -SetThreadName(const std::string& name); +SetThreadName(const std::string_view name); std::string GetThreadName(); diff --git a/internal/core/src/query/Parser.h b/internal/core/src/query/Parser.h index dea7ca8206..7ed4219d3a 100644 --- a/internal/core/src/query/Parser.h +++ b/internal/core/src/query/Parser.h @@ -28,7 +28,7 @@ namespace milvus::query { class Parser { public: friend std::unique_ptr - CreatePlan(const Schema& schema, const std::string& dsl_str); + CreatePlan(const Schema& schema, const std::string_view dsl_str); private: std::unique_ptr diff --git a/internal/core/src/query/Plan.cpp b/internal/core/src/query/Plan.cpp index b408a9b5dc..978243376d 100644 --- a/internal/core/src/query/Plan.cpp +++ b/internal/core/src/query/Plan.cpp @@ -24,10 +24,10 @@ namespace milvus::query { // deprecated std::unique_ptr ParsePlaceholderGroup(const Plan* plan, - const std::string& placeholder_group_blob) { + const std::string_view placeholder_group_blob) { return ParsePlaceholderGroup( plan, - reinterpret_cast(placeholder_group_blob.c_str()), + reinterpret_cast(placeholder_group_blob.data()), placeholder_group_blob.size()); } @@ -64,7 +64,7 @@ ParsePlaceholderGroup(const Plan* plan, } std::unique_ptr -CreatePlan(const Schema& schema, const std::string& dsl_str) { +CreatePlan(const Schema& schema, const std::string_view dsl_str) { Json dsl; dsl = json::parse(dsl_str); auto plan = Parser(schema).CreatePlanImpl(dsl); diff --git a/internal/core/src/query/Plan.h b/internal/core/src/query/Plan.h index fb13cc882f..cc5f65c476 100644 --- a/internal/core/src/query/Plan.h +++ b/internal/core/src/query/Plan.h @@ -27,7 +27,7 @@ struct PlaceholderGroup; struct RetrievePlan; std::unique_ptr -CreatePlan(const Schema& schema, const std::string& dsl); +CreatePlan(const Schema& schema, const std::string_view dsl); // Note: serialized_expr_plan is of binary format std::unique_ptr @@ -43,7 +43,7 @@ ParsePlaceholderGroup(const Plan* plan, // deprecated std::unique_ptr ParsePlaceholderGroup(const Plan* plan, - const std::string& placeholder_group_blob); + const std::string_view placeholder_group_blob); int64_t GetNumOfQueries(const PlaceholderGroup*); diff --git a/internal/core/src/query/Relational.h b/internal/core/src/query/Relational.h index da3bb80a6f..e96ec60b0b 100644 --- a/internal/core/src/query/Relational.h +++ b/internal/core/src/query/Relational.h @@ -11,15 +11,15 @@ #pragma once +#include +#include + +#include "common/Utils.h" #include "common/VectorTrait.h" #include "exceptions/EasyAssert.h" #include "query/Expr.h" -#include "common/Utils.h" #include "query/Utils.h" -#include -#include - namespace milvus::query { template bool diff --git a/internal/core/src/query/deprecated/ParserDeprecated.cpp b/internal/core/src/query/deprecated/ParserDeprecated.cpp index fa70edc0ae..149683f434 100644 --- a/internal/core/src/query/deprecated/ParserDeprecated.cpp +++ b/internal/core/src/query/deprecated/ParserDeprecated.cpp @@ -198,7 +198,7 @@ ProcessBooleanQueryJson(const milvus::json& query_json, Status DeserializeJsonToBoolQuery(const google::protobuf::RepeatedPtrField< ::milvus::grpc::VectorParam>& vector_params, - const std::string& dsl_string, + const std::string_view dsl_string, query_old::BooleanQueryPtr& boolean_query, query_old::QueryPtr& query_ptr) { #if 1 @@ -214,7 +214,7 @@ DeserializeJsonToBoolQuery(const google::protobuf::RepeatedPtrField< "DSL must include vector query"); } for (const auto& vector_param : vector_params) { - const std::string& vector_string = vector_param.json(); + const std::string_view vector_string = vector_param.json(); milvus::json vector_json = Json::parse(vector_string); milvus::json::iterator it = vector_json.begin(); std::string placeholder = it.key(); @@ -222,7 +222,7 @@ DeserializeJsonToBoolQuery(const google::protobuf::RepeatedPtrField< auto vector_query = std::make_shared(); milvus::json::iterator vector_param_it = it.value().begin(); if (vector_param_it != it.value().end()) { - const std::string& field_name = vector_param_it.key(); + const std::string_view field_name = vector_param_it.key(); vector_query->field_name = field_name; milvus::json param_json = vector_param_it.value(); int64_t topk = param_json["topk"]; diff --git a/internal/core/src/query/deprecated/ValidationUtil.cpp b/internal/core/src/query/deprecated/ValidationUtil.cpp index 94d1261a8d..4c10b36fdc 100644 --- a/internal/core/src/query/deprecated/ValidationUtil.cpp +++ b/internal/core/src/query/deprecated/ValidationUtil.cpp @@ -26,7 +26,7 @@ namespace { Status CheckParameterRange(const milvus::json& json_params, - const std::string& param_name, + const std::string_view param_name, int64_t min, int64_t max, bool min_close = true, @@ -63,7 +63,7 @@ CheckParameterRange(const milvus::json& json_params, Status CheckParameterExistence(const milvus::json& json_params, - const std::string& param_name) { + const std::string_view param_name) { if (json_params.find(param_name) == json_params.end()) { std::string msg = "Parameter list must contain: "; msg += param_name; @@ -92,7 +92,7 @@ CheckParameterExistence(const milvus::json& json_params, } // namespace Status -ValidateCollectionName(const std::string& collection_name) { +ValidateCollectionName(const std::string_view collection_name) { // Collection name shouldn't be empty. if (collection_name.empty()) { std::string msg = "Collection name should not be empty."; @@ -138,7 +138,7 @@ ValidateCollectionName(const std::string& collection_name) { } Status -ValidateFieldName(const std::string& field_name) { +ValidateFieldName(const std::string_view field_name) { // Field name shouldn't be empty. if (field_name.empty()) { std::string msg = "Field name should not be empty."; @@ -269,7 +269,7 @@ ValidateDimension(int64_t dim, bool is_binary) { Status ValidateIndexParams(const milvus::json& index_params, int64_t dimension, - const std::string& index_type) { + const std::string_view index_type) { if (engine::utils::IsFlatIndexType(index_type)) { return Status::OK(); } else if (index_type == knowhere::IndexEnum::INDEX_FAISS_IVFFLAT || @@ -362,8 +362,8 @@ ValidateSegmentRowCount(int64_t segment_row_count) { } Status -ValidateIndexMetricType(const std::string& metric_type, - const std::string& index_type) { +ValidateIndexMetricType(const std::string_view metric_type, + const std::string_view index_type) { if (engine::utils::IsFlatIndexType(index_type)) { // pass } else if (index_type == knowhere::IndexEnum::INDEX_FAISS_BIN_IVFFLAT) { @@ -391,7 +391,7 @@ ValidateIndexMetricType(const std::string& metric_type, } Status -ValidateSearchMetricType(const std::string& metric_type, bool is_binary) { +ValidateSearchMetricType(const std::string_view metric_type, bool is_binary) { if (is_binary) { // binary if (metric_type == knowhere::Metric::L2 || @@ -432,7 +432,7 @@ ValidateSearchTopk(int64_t top_k) { Status ValidatePartitionTags(const std::vector& partition_tags) { - for (const std::string& tag : partition_tags) { + for (const std::string_view tag : partition_tags) { // Partition nametag shouldn't be empty. if (tag.empty()) { std::string msg = "Partition tag should not be empty."; diff --git a/internal/core/src/query/deprecated/ValidationUtil.h b/internal/core/src/query/deprecated/ValidationUtil.h index f1b481dceb..ae0ae09be4 100644 --- a/internal/core/src/query/deprecated/ValidationUtil.h +++ b/internal/core/src/query/deprecated/ValidationUtil.h @@ -27,10 +27,10 @@ constexpr int64_t GPU_QUERY_MAX_TOPK = 2048; constexpr int64_t GPU_QUERY_MAX_NPROBE = 2048; extern Status -ValidateCollectionName(const std::string& collection_name); +ValidateCollectionName(const std::string_view collection_name); extern Status -ValidateFieldName(const std::string& field_name); +ValidateFieldName(const std::string_view field_name); extern Status ValidateDimension(int64_t dimension, bool is_binary); @@ -44,17 +44,17 @@ ValidateStructuredIndexType(std::string& index_type); extern Status ValidateIndexParams(const milvus::json& index_params, int64_t dimension, - const std::string& index_type); + const std::string_view index_type); extern Status ValidateSegmentRowCount(int64_t segment_row_count); extern Status -ValidateIndexMetricType(const std::string& metric_type, - const std::string& index_type); +ValidateIndexMetricType(const std::string_view metric_type, + const std::string_view index_type); extern Status -ValidateSearchMetricType(const std::string& metric_type, bool is_binary); +ValidateSearchMetricType(const std::string_view metric_type, bool is_binary); extern Status ValidateSearchTopk(int64_t top_k); diff --git a/internal/core/src/segcore/Collection.cpp b/internal/core/src/segcore/Collection.cpp index 944322bc7c..52cb7614dd 100644 --- a/internal/core/src/segcore/Collection.cpp +++ b/internal/core/src/segcore/Collection.cpp @@ -16,7 +16,7 @@ namespace milvus::segcore { -Collection::Collection(const std::string& collection_proto) +Collection::Collection(const std::string_view collection_proto) : schema_proto_(collection_proto) { parse(); } diff --git a/internal/core/src/segcore/Collection.h b/internal/core/src/segcore/Collection.h index c5e5f20e98..414922536e 100644 --- a/internal/core/src/segcore/Collection.h +++ b/internal/core/src/segcore/Collection.h @@ -20,7 +20,7 @@ namespace milvus::segcore { class Collection { public: - explicit Collection(const std::string& collection_proto); + explicit Collection(const std::string_view collection_proto); void parse(); @@ -31,7 +31,7 @@ class Collection { return schema_; } - const std::string& + const std::string_view get_collection_name() { return collection_name_; } diff --git a/internal/core/src/storage/LocalChunkManager.cpp b/internal/core/src/storage/LocalChunkManager.cpp index 6e25f8cae8..ebcf43ae82 100644 --- a/internal/core/src/storage/LocalChunkManager.cpp +++ b/internal/core/src/storage/LocalChunkManager.cpp @@ -79,7 +79,7 @@ LocalChunkManager::Read(const std::string& filepath, void* buf, uint64_t size) { std::ifstream infile; - infile.open(filepath, std::ios_base::binary); + infile.open(filepath.data(), std::ios_base::binary); if (infile.fail()) { std::stringstream err_msg; err_msg << "Error: open local file '" << filepath << " failed, " @@ -104,7 +104,7 @@ LocalChunkManager::Write(const std::string& absPathStr, void* buf, uint64_t size) { std::ofstream outfile; - outfile.open(absPathStr, std::ios_base::binary); + outfile.open(absPathStr.data(), std::ios_base::binary); if (outfile.fail()) { std::stringstream err_msg; err_msg << "Error: open local file '" << absPathStr << " failed, " @@ -126,7 +126,7 @@ LocalChunkManager::Write(const std::string& absPathStr, uint64_t size) { std::ofstream outfile; outfile.open( - absPathStr, + absPathStr.data(), std::ios_base::in | std::ios_base::out | std::ios_base::binary); if (outfile.fail()) { std::stringstream err_msg; diff --git a/internal/core/src/storage/MinioChunkManager.cpp b/internal/core/src/storage/MinioChunkManager.cpp index 3987e0bf4a..7f3fd1fd8a 100644 --- a/internal/core/src/storage/MinioChunkManager.cpp +++ b/internal/core/src/storage/MinioChunkManager.cpp @@ -14,20 +14,22 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include "storage/MinioChunkManager.h" + #include #include #include #include #include -#include #include +#include +#include #include #include -#include #include -#include "storage/MinioChunkManager.h" +#include + #include "exceptions/EasyAssert.h" #include "log/Log.h" diff --git a/internal/core/src/storage/parquet_c.cpp b/internal/core/src/storage/parquet_c.cpp index 9b5a3de655..4f13e9bc75 100644 --- a/internal/core/src/storage/parquet_c.cpp +++ b/internal/core/src/storage/parquet_c.cpp @@ -39,11 +39,11 @@ ReleaseArrowUnused() { } static const char* -ErrorMsg(const std::string& msg) { +ErrorMsg(const std::string_view msg) { if (msg.empty()) return nullptr; auto ret = (char*)malloc(msg.size() + 1); - std::memcpy(ret, msg.c_str(), msg.size()); + std::memcpy(ret, msg.data(), msg.size()); ret[msg.size()] = '\0'; return ret; } diff --git a/internal/core/src/utils/Status.cpp b/internal/core/src/utils/Status.cpp index 0fa2a2fea6..332fe0d987 100644 --- a/internal/core/src/utils/Status.cpp +++ b/internal/core/src/utils/Status.cpp @@ -18,7 +18,7 @@ namespace milvus { constexpr int CODE_WIDTH = sizeof(StatusCode); -Status::Status(StatusCode code, const std::string& msg) { +Status::Status(StatusCode code, const std::string_view msg) { // 4 bytes store code // 4 bytes store message length // the left bytes store message string diff --git a/internal/core/src/utils/Status.h b/internal/core/src/utils/Status.h index 56948aea1a..73bc395861 100644 --- a/internal/core/src/utils/Status.h +++ b/internal/core/src/utils/Status.h @@ -30,7 +30,7 @@ using StatusCode = ErrorCode; class Status { public: - Status(StatusCode code, const std::string& msg); + Status(StatusCode code, const std::string_view msg); Status() = default; virtual ~Status();