Skip to content

Commit

Permalink
Optimize rounding distances, avoid promoting to double (milvus-io#22846)
Browse files Browse the repository at this point in the history
Signed-off-by: yah01 <yang.cen@zilliz.com>
  • Loading branch information
yah01 authored Mar 20, 2023
1 parent 8cf748a commit 3202eb0
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Checks: >
-*, clang-diagnostic-*, -clang-diagnostic-error,
clang-analyzer-*, -clang-analyzer-alpha*,
google-*, -google-runtime-references, -google-readability-todo,
modernize-*, -modernize-use-trailing-return-type,
modernize-*, -modernize-use-trailing-return-type, -modernize-use-nodiscard,
performance-*,
bugprone-bool-pointer-implicit-conversion,
bugprone-branch-clone,
Expand Down
2 changes: 1 addition & 1 deletion internal/core/src/index/VectorDiskIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ VectorDiskAnnIndex<T>::Query(const DatasetPtr dataset,
if (round_decimal != -1) {
const float multiplier = pow(10.0, round_decimal);
for (int i = 0; i < total_num; i++) {
distances[i] = round(distances[i] * multiplier) / multiplier;
distances[i] = std::round(distances[i] * multiplier) / multiplier;
}
}
auto result = std::make_unique<SearchResult>();
Expand Down
4 changes: 3 additions & 1 deletion internal/core/src/index/VectorMemIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// limitations under the License.

#include "index/VectorMemIndex.h"

#include <cmath>
#include "index/Meta.h"
#include "index/Utils.h"
#include "exceptions/EasyAssert.h"
Expand Down Expand Up @@ -129,7 +131,7 @@ VectorMemIndex::Query(const DatasetPtr dataset,
if (round_decimal != -1) {
const float multiplier = pow(10.0, round_decimal);
for (int i = 0; i < total_num; i++) {
distances[i] = round(distances[i] * multiplier) / multiplier;
distances[i] = std::round(distances[i] * multiplier) / multiplier;
}
}
auto result = std::make_unique<SearchResult>();
Expand Down
2 changes: 1 addition & 1 deletion internal/core/src/query/SearchOnSealed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ SearchOnSealedIndex(const Schema& schema,
if (round_decimal != -1) {
const float multiplier = pow(10.0, round_decimal);
for (int i = 0; i < total_num; i++) {
distances[i] = round(distances[i] * multiplier) / multiplier;
distances[i] = std::round(distances[i] * multiplier) / multiplier;
}
}
result.seg_offsets_.resize(total_num);
Expand Down
5 changes: 2 additions & 3 deletions internal/core/src/query/SubSearchResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ SubSearchResult::round_values() {
if (round_decimal_ == -1)
return;
const float multiplier = pow(10.0, round_decimal_);
for (auto it = this->distances_.begin(); it != this->distances_.end();
it++) {
*it = round(*it * multiplier) / multiplier;
for (float& distance : this->distances_) {
distance = std::round(distance * multiplier) / multiplier;
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/core/src/query/SubSearchResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class SubSearchResult {
distances_(num_queries * topk, init_value(metric_type)) {
}

SubSearchResult(SubSearchResult&& other)
SubSearchResult(SubSearchResult&& other) noexcept
: num_queries_(other.num_queries_),
topk_(other.topk_),
round_decimal_(other.round_decimal_),
metric_type_(other.metric_type_),
metric_type_(std::move(other.metric_type_)),
seg_offsets_(std::move(other.seg_offsets_)),
distances_(std::move(other.distances_)) {
}
Expand Down
5 changes: 3 additions & 2 deletions internal/core/src/segcore/ReduceStructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#pragma once

#include <limits>
#include <utility>

#include "common/Consts.h"
#include "common/Types.h"
Expand All @@ -33,7 +34,7 @@ struct SearchResultPair {
int64_t index,
int64_t lb,
int64_t rb)
: primary_key_(primary_key),
: primary_key_(std::move(primary_key)),
distance_(distance),
search_result_(result),
segment_index_(index),
Expand All @@ -43,7 +44,7 @@ struct SearchResultPair {

bool
operator>(const SearchResultPair& other) const {
if (fabs(distance_ - other.distance_) < 0.000001f) {
if (std::fabs(distance_ - other.distance_) < 0.000001f) {
return primary_key_ < other.primary_key_;
}
return distance_ > other.distance_;
Expand Down

0 comments on commit 3202eb0

Please sign in to comment.