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 94146de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
* Converting local realm to sync'd realm crashes if an embedded object is null ([#6294](https://github.com/realm/realm-core/issues/6294), since v11.9.0)
* Fixed performance degradation on SubQueries ([#6327](https://github.com/realm/realm-core/issues/6327), since v6.0.0)

### Breaking changes
* None.
Expand Down
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 94146de

Please sign in to comment.