Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize IntegerNode::find_first_local #7772

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/realm/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,11 @@ class Array : public Node, public ArrayParent {
template <class cond>
size_t find_first(int64_t value, size_t start = 0, size_t end = size_t(-1)) const
{
static cond c;
REALM_ASSERT(start <= m_size && (end <= m_size || end == size_t(-1)) && start <= end);
if (end - start == 1) {
return c(get(start), value) ? start : realm::not_found;
}
// todo, would be nice to avoid this in order to speed up find_first loops
QueryStateFindFirst state;
Finder finder = m_vtable->finder[cond::condition];
Expand Down
12 changes: 8 additions & 4 deletions src/realm/array_integer_tpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ bool ArrayIntNull::find_impl(value_type opt_value, size_t start, size_t end, Que
template <class cond>
size_t ArrayIntNull::find_first(value_type value, size_t start, size_t end) const
{
static cond c;
REALM_ASSERT(start <= m_size && (end <= m_size || end == size_t(-1)) && start <= end);
if (end - start == 1) {
std::optional<int64_t> opt_int = get(start);
return c(opt_int, value, !opt_int, !value) ? start : realm::not_found;
}

QueryStateFindFirst state;
find_impl<cond>(value, start, end, &state);

if (state.match_count() > 0)
return to_size_t(state.m_state);
else
return not_found;
return static_cast<size_t>(state.m_state);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: QueryStateFindFirst.m_state is already a size_t, so no cast is necessary here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate being caught in this - but it was copied from somewhere else :-)
I will fix this later

}

template <class cond>
Expand Down
5 changes: 0 additions & 5 deletions src/realm/query_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,11 +576,6 @@ class IntegerNode<LeafType, Equal> : public IntegerNodeBase<LeafType> {
else if (m_index_evaluator) {
return m_index_evaluator->do_search_index(BaseType::m_cluster, start, end);
}
else if (end - start == 1) {
if (this->m_leaf->get(start) == this->m_value) {
s = start;
}
}
else {
s = this->m_leaf->template find_first<Equal>(this->m_value, start, end);
}
Expand Down
9 changes: 5 additions & 4 deletions test/test_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,17 +706,18 @@ TEST(Query_NextGenSyntaxMonkey0)
}
}

TEST(Query_NextGenSyntaxMonkey)
TEST_TYPES(Query_NextGenSyntaxMonkey, std::true_type, std::false_type)
{
static const bool nullable = TEST_TYPE::value;
Random random(random_int<unsigned long>()); // Seed from slow global generator
for (int iter = 1; iter < 5 * (TEST_DURATION * TEST_DURATION * TEST_DURATION + 1); iter++) {
// Set 'rows' to at least '* 20' else some tests will give 0 matches and bad coverage
const size_t rows = 1 + random.draw_int_mod<size_t>(REALM_MAX_BPNODE_SIZE * 20 *
(TEST_DURATION * TEST_DURATION * TEST_DURATION + 1));
Table table;
auto col_int0 = table.add_column(type_Int, "first");
auto col_int1 = table.add_column(type_Int, "second");
auto col_int2 = table.add_column(type_Int, "third");
auto col_int0 = table.add_column(type_Int, "first", nullable);
auto col_int1 = table.add_column(type_Int, "second", nullable);
auto col_int2 = table.add_column(type_Int, "third", nullable);

for (size_t r = 0; r < rows; r++) {
Obj obj = table.create_object();
Expand Down
2 changes: 1 addition & 1 deletion test/util/test_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ bool initialize_test_path(int argc, const char* argv[])
return false;
}
g_resource_path = File::resolve("resources", directory) + "/";
g_path_prefix = directory;
g_path_prefix = std::string(directory) + "/";
#endif

if (argc > 0) {
Expand Down
Loading