Skip to content

Commit

Permalink
Further optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
jedelbo committed Aug 16, 2019
1 parent cec3647 commit 20b8ee8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
6 changes: 4 additions & 2 deletions src/realm/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2221,10 +2221,12 @@ bool Array::find_optimized(int64_t value, size_t start, size_t end, size_t basei
if (nullable_array) {
// We were called by find() of a nullable array. So skip first entry, take nulls in count, etc, etc. Fixme:
// Huge speed optimizations are possible here! This is a very simple generic method.
auto null_value = get(0);
for (; start2 < end; start2++) {
int64_t v = get<bitwidth>(start2 + 1);
if (c(v, value, v == get(0), find_null)) {
util::Optional<int64_t> v2(v == get(0) ? util::none : util::make_optional(v));
bool value_is_null = (v == null_value);
if (c(v, value, value_is_null, find_null)) {
util::Optional<int64_t> v2(value_is_null ? util::none : util::make_optional(v));
if (!find_action<action, Callback>(start2 + baseindex, v2, state, callback))
return false; // tell caller to stop aggregating/search
}
Expand Down
42 changes: 17 additions & 25 deletions src/realm/query_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,9 +759,6 @@ size_t TimestampNode<GreaterEqual>::find_first_local(size_t start, size_t end)
{
REALM_ASSERT(this->m_table);

if (this->m_value.is_null()) {
return not_found;
}
while (start < end) {
size_t ret = this->find_first_local_seconds<GreaterEqual>(start, end);

Expand All @@ -770,6 +767,9 @@ size_t TimestampNode<GreaterEqual>::find_first_local(size_t start, size_t end)

util::Optional<int64_t> seconds = get_seconds_and_cache(ret);
if (!seconds) { // null equality
if (this->m_value.is_null()) {
return ret;
}
start = ret + 1;
continue;
}
Expand All @@ -793,9 +793,6 @@ size_t TimestampNode<LessEqual>::find_first_local(size_t start, size_t end)
{
REALM_ASSERT(this->m_table);

if (this->m_value.is_null()) {
return not_found;
}
while (start < end) {
size_t ret = this->find_first_local_seconds<LessEqual>(start, end);

Expand All @@ -804,6 +801,9 @@ size_t TimestampNode<LessEqual>::find_first_local(size_t start, size_t end)

util::Optional<int64_t> seconds = get_seconds_and_cache(ret);
if (!seconds) { // null equality
if (this->m_value.is_null()) {
return ret;
}
start = ret + 1;
continue;
}
Expand Down Expand Up @@ -857,17 +857,6 @@ size_t TimestampNode<NotEqual>::find_first_local(size_t start, size_t end)
{
REALM_ASSERT(this->m_table);

// in many scenarios it is likely that the first item is not equal do a quick first check
if (start < end) {
util::Optional<int64_t> seconds = get_seconds_and_cache(start);
if (seconds != m_needle_seconds
|| (seconds && this->get_nanoseconds_and_cache(start) != m_value.get_nanoseconds())) {
return start;
}
}

++start;

if (m_value.is_null()) {
if (REALM_UNLIKELY(!m_condition_column_is_nullable)) {
return not_found;
Expand All @@ -878,14 +867,17 @@ size_t TimestampNode<NotEqual>::find_first_local(size_t start, size_t end)
int64_t needle_seconds = m_value.get_seconds();
while (start < end) {
util::Optional<int64_t> seconds = get_seconds_and_cache(start);
if (!seconds || *seconds != needle_seconds) {
return start;
}
// We now know that neither m_value nor current value is null and that seconds part equals
// We are just missing to compare nanoseconds part
int32_t nanos = this->get_nanoseconds_and_cache(start);
if (nanos != m_value.get_nanoseconds()) {
return start;
// Null value does not match
if (seconds) {
if (*seconds != needle_seconds) {
return start;
}
// We now know that neither m_value nor current value is null and that seconds part equals
// We are just missing to compare nanoseconds part
int32_t nanos = this->get_nanoseconds_and_cache(start);
if (nanos != m_value.get_nanoseconds()) {
return start;
}
}
++start;
}
Expand Down
2 changes: 2 additions & 0 deletions test/test_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10703,6 +10703,8 @@ TEST(Query_TimestampCount)
CHECK_EQUAL((timestamps >= Timestamp(0, 3)).count(), 6);
CHECK_EQUAL((timestamps < Timestamp(1, 3)).count(), 6);
CHECK_EQUAL((timestamps <= Timestamp(1, 3)).count(), 7);
CHECK_EQUAL((timestamps == Timestamp(0, 2)).count(), 1);
CHECK_EQUAL((timestamps != Timestamp(0, 2)).count(), 8);
CHECK_EQUAL((timestamps == Timestamp()).count(), 1);
CHECK_EQUAL((timestamps != Timestamp()).count(), 9);
}
Expand Down

0 comments on commit 20b8ee8

Please sign in to comment.