Skip to content

Commit

Permalink
Merge pull request #33299 from vespa-engine/toregge/extend-shared-str…
Browse files Browse the repository at this point in the history
…ing-repo-handles-push-back-choices

Extend vespalib::SharedStringRepo::Handles with a push_back member fu…
  • Loading branch information
toregge authored Feb 11, 2025
2 parents 25a7e92 + 149eea2 commit 6782a12
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
7 changes: 3 additions & 4 deletions searchlib/src/vespa/searchlib/features/elementwise_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ElementwiseOutput::build_helper(const vespalib::hash_map<uint32_t, double>& scor
auto& cells = std::get<std::vector<CT>>(_cells);
cells.clear();
for (auto& elem : scores) {
_labels->push_back(SharedStringRepo::Handle::handle_from_number(elem.first).id());
_labels.push_back(SharedStringRepo::Handle::handle_from_number(elem.first));
cells.emplace_back(static_cast<CT>(elem.second));
}
return TypedCells(cells);
Expand All @@ -58,10 +58,9 @@ ElementwiseOutput::build(const vespalib::hash_map<uint32_t, double>& scores)
if (scores.empty()) {
return _empty_output;
}
_labels.reset();
_labels.emplace();
_labels.clear();
auto cells = typify_invoke<1, TypifyCellType, CallBuilderHelper>(_empty_output.type().cell_type(), *this, scores);
_output = std::make_unique<FastValueView>(_empty_output.type(), _labels->view(), cells, 1, (size_t)cells.size);
_output = std::make_unique<FastValueView>(_empty_output.type(), _labels.view(), cells, 1, (size_t)cells.size);
return *_output;
}

Expand Down
3 changes: 1 addition & 2 deletions searchlib/src/vespa/searchlib/features/elementwise_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <vespa/eval/eval/value.h>
#include <vespa/vespalib/stllike/hash_map.h>
#include <vespa/vespalib/util/shared_string_repo.h>
#include <optional>
#include <variant>

namespace search::features {
Expand All @@ -18,7 +17,7 @@ namespace search::features {
class ElementwiseOutput {
struct CallBuilderHelper;
friend struct CallBuilderHelper;
std::optional<vespalib::SharedStringRepo::Handles> _labels;
vespalib::SharedStringRepo::Handles _labels;
std::variant<std::monostate, std::vector<double>, std::vector<float>, std::vector<vespalib::BFloat16>, std::vector<vespalib::eval::Int8Float>> _cells;
const vespalib::eval::Value& _empty_output;
std::unique_ptr<vespalib::eval::Value> _output;
Expand Down
11 changes: 11 additions & 0 deletions vespalib/src/vespa/vespalib/util/shared_string_repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,15 @@ SharedStringRepo::Handles::~Handles()
}
}

void
SharedStringRepo::Handles::clear()
{
if (should_reclaim) {
for (string_id handle : _handles) {
_repo.reclaim(handle);
}
}
_handles.clear();
}

}
11 changes: 11 additions & 0 deletions vespalib/src/vespa/vespalib/util/shared_string_repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class SharedStringRepo {
void reclaim(string_id id);

static SharedStringRepo _repo;

struct access_token { };
public:
static bool will_reclaim() { return should_reclaim; }
static Stats stats();
Expand Down Expand Up @@ -168,6 +170,11 @@ class SharedStringRepo {
rhs._id = string_id();
return *this;
}
string_id release(access_token) noexcept {
auto res = _id;
_id = string_id();
return res;
}
// NB: not lexical sorting order, but can be used in maps
bool operator<(const Handle &rhs) const noexcept { return (_id < rhs._id); }
bool operator==(const Handle &rhs) const noexcept { return (_id == rhs._id); }
Expand Down Expand Up @@ -197,6 +204,7 @@ class SharedStringRepo {
Handles &operator=(const Handles &) = delete;
Handles &operator=(Handles &&) = delete;
~Handles();
void clear();
string_id add(std::string_view str) {
string_id id = _repo.resolve(str);
_handles.push_back(id);
Expand All @@ -207,6 +215,9 @@ class SharedStringRepo {
string_id id = _repo.copy(handle);
_handles.push_back(id);
}
void push_back(Handle&& handle) {
_handles.push_back(handle.release(access_token()));
}
[[nodiscard]] const StringIdVector &view() const { return _handles; }
};

Expand Down

0 comments on commit 6782a12

Please sign in to comment.