Skip to content

Commit fd143c7

Browse files
committed
use resize() instead for(...)
1 parent b86bfdb commit fd143c7

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

tools/server/utils.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,6 @@ static json format_response_rerank(
851851
bool is_tei_format,
852852
std::vector<std::string> & texts,
853853
int top_n) {
854-
json results;
855854
int32_t n_tokens = 0;
856855
bool return_text = is_tei_format && json_value(request, "return_text", false);
857856
std::vector<json> elements; // Temporary vector to hold unsorted elements
@@ -868,17 +867,14 @@ static json format_response_rerank(
868867
}
869868
elements.push_back(elem);
870869
}
871-
870+
872871
std::sort(elements.begin(), elements.end(), [score_label](const json& a, const json& b) {
873872
return json_value(a, score_label, 0.0) > json_value(b, score_label, 0.0);
874873
});
875874

876-
results = json::array();
877-
int count = 0;
878-
for (const auto & elem : elements) {
879-
if (++count > top_n) break;
880-
results.push_back(elem);
881-
}
875+
elements.resize(std::min(top_n, (int)elements.size()));
876+
json results = elements;
877+
882878
if (is_tei_format) return results;
883879

884880
json res = json{

0 commit comments

Comments
 (0)