Skip to content

Commit

Permalink
Initialize sub-query only once
Browse files Browse the repository at this point in the history
  • Loading branch information
jedelbo committed Feb 22, 2023
1 parent 25082db commit a48eb8b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/realm/query_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3866,7 +3866,10 @@ class SubQueryCount : public Subexpr2<Int> {
{
std::vector<ObjKey> links = m_link_map.get_links(index);
// std::sort(links.begin(), links.end());
m_query.init();
if (!m_initialized) {
m_query.init();
m_initialized = true;
}

size_t count = std::accumulate(links.begin(), links.end(), size_t(0), [this](size_t running_count, ObjKey k) {
const Obj obj = m_link_map.get_target_table()->get_object(k);
Expand All @@ -3893,9 +3896,17 @@ class SubQueryCount : public Subexpr2<Int> {
return make_subexpr<SubQueryCount>(*this);
}

SubQueryCount(const SubQueryCount& other)
: m_query(other.m_query)
, m_link_map(other.m_link_map)
, m_initialized(false)
{
}

private:
Query m_query;
LinkMap m_link_map;
bool m_initialized = false;
};

// The unused template parameter is a hack to avoid a circular dependency between table.hpp and query_expression.hpp.
Expand Down

0 comments on commit a48eb8b

Please sign in to comment.