zilliztech / knowhere

Knowhere is an open-source vector search engine, integrating FAISS, HNSW, etc.
Apache License 2.0
181 stars 77 forks source link

Fix: use std::visit to replace std::get_if #630

Closed Light-City closed 2 weeks ago

Light-City commented 5 months ago

The destructor of dataset is not well written to release memory. We can use visit to simplify the code.

before:

{
                auto ptr = std::get_if<0>(&x.second);
                if (ptr != nullptr) {
                    delete[] * ptr;
                }
            }
            {
                auto ptr = std::get_if<1>(&x.second);
                if (ptr != nullptr) {
                    delete[] * ptr;
                }
            }
            {
                auto ptr = std::get_if<2>(&x.second);
                if (ptr != nullptr) {
                    delete[] * ptr;
                }
            }
            {
                auto ptr = std::get_if<3>(&x.second);
                if (ptr != nullptr) {
                    if (is_sparse) {
                        delete[](sparse::SparseRow<float>*)(*ptr);
                    } else {
                        delete[](char*)(*ptr);
                    }
                }
            }

after:

for (auto&& x : this->data_) {
            std::visit(
                [](auto&& arg) {
                    using T = std::decay_t<decltype(arg)>;
                    if constexpr (std::is_same_v<T, const float*> || std::is_same_v<T, const size_t*> ||
                                  std::is_same_v<T, const int64_t*>) {
                        delete[] arg;
                    } else if constexpr (std::is_same_v<T, const void*>) {
                        delete[] (char*)(arg);
                    }
                },
                x.second);
github-actions[bot] commented 4 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Rotten issues close after 30d of inactivity. Reopen the issue with /reopen.

Light-City commented 4 months ago

/reopen

github-actions[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Rotten issues close after 30d of inactivity. Reopen the issue with /reopen.

github-actions[bot] commented 1 month ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Rotten issues close after 30d of inactivity. Reopen the issue with /reopen.

github-actions[bot] commented 3 weeks ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Rotten issues close after 30d of inactivity. Reopen the issue with /reopen.