tp: reduce per-lookup overhead in core/exec scans and Filter Trim structural per-call overhead in core/exec so point lookups and single-batch scans pay less fixed cost per execution: - Compact ParamValue from 120 B to 32 B by lazily allocating the text/regex/list/glob fields, and split string equality param casting from regex/glob casting so string equality avoids regex stack frames. - Replace Filter::Process's nested switches with a static kCompareTable[Op][StorageType] dispatch table and an out-of-line slow path, shrinking Filter::Process and removing its stack protector. - For dense nullable flat columns (such as ArgTable::key) under numeric comparisons or interned-id string equality/inequality, filter values contiguously first via FilterRange and drop nulls only from the surviving rows instead of materializing and scanning the null bitvector over the full batch. Zero BufferPool blocks once on allocation so null slots in reused chunks are initialized. - Replace RangeReader's descending index table with Selection::Reverse(), lend non-null Id spans directly in DataframeWindows::Gather, skip empty trailing Next() calls via RowBatch::last() / RowCursor::exhausted_ and Source::HasInPlace(), and move SortedSearches string boxing out of line.
diff --git a/src/trace_processor/core/exec/buffer_pool.cc b/src/trace_processor/core/exec/buffer_pool.cc index a56f6b7..86b627b 100644 --- a/src/trace_processor/core/exec/buffer_pool.cc +++ b/src/trace_processor/core/exec/buffer_pool.cc
@@ -17,6 +17,7 @@ #include "src/trace_processor/core/exec/buffer_pool.h" #include <cstdint> +#include <cstring> #include <memory> #include "perfetto/base/logging.h" @@ -43,6 +44,8 @@ block->width_class = width_class; uint64_t bytes = uint64_t{kMaxBatchRows} * (4u << width_class); block->chunk.storage = Slab<uint8_t>::Alloc(bytes); + // Zero once on allocation so null slots in reused chunks are initialized. + memset(block->chunk.storage.data(), 0, bytes); allocated_bytes_ += bytes; return block; }
diff --git a/src/trace_processor/core/exec/dataframe_scan_unittest.cc b/src/trace_processor/core/exec/dataframe_scan_unittest.cc index 9b05918..b50b762 100644 --- a/src/trace_processor/core/exec/dataframe_scan_unittest.cc +++ b/src/trace_processor/core/exec/dataframe_scan_unittest.cc
@@ -227,7 +227,8 @@ Context context; OperatorState* state = scan.MakeState(context); ParamValue param; - param.value = filter::CastFilterValueResult::Valid(kBig + 7); + param.validity = filter::CastFilterValueResult::kValid; + param.value = kBig + 7; context.SetParams(base::Span<const ParamValue>(¶m, ¶m + 1)); RowBatch batch;
diff --git a/src/trace_processor/core/exec/dataframe_windows.cc b/src/trace_processor/core/exec/dataframe_windows.cc index 80e7590..147a4b5 100644 --- a/src/trace_processor/core/exec/dataframe_windows.cc +++ b/src/trace_processor/core/exec/dataframe_windows.cc
@@ -16,13 +16,11 @@ #include "src/trace_processor/core/exec/dataframe_windows.h" -#include <array> #include <cstdint> #include <cstring> #include "perfetto/base/compiler.h" #include "perfetto/base/logging.h" -#include "perfetto/ext/base/no_destructor.h" #include "perfetto/ext/base/span.h" #include "src/trace_processor/containers/string_pool.h" #include "src/trace_processor/core/common/filter_kernels.h" @@ -90,6 +88,13 @@ const uint32_t* popcount = view.popcount(); const uint32_t* begin = rows.b; auto count = static_cast<uint32_t>(rows.size()); + if (!validity && !sparse && data) { + for (uint32_t i = 0; i < count; ++i) { + memcpy(values + uint64_t{i} * kWidth, data + uint64_t{begin[i]} * kWidth, + kWidth); + } + return nullptr; + } for (uint32_t i = 0; i < count; ++i) { uint32_t row = begin[i]; uint8_t* value = values + uint64_t{i} * kWidth; @@ -175,6 +180,13 @@ } for (uint32_t i = 0; i < count_; ++i) { const ColumnView& view = lent_[i]; + if (view.kind() == ColumnView::Kind::kSequence && !view.validity()) { + // A non-null Id column's gathered values are the row indices themselves; + // `rows` outlives the run, so lend it directly without copying. + gathered_[i] = ColumnView::Reference(StorageType{Uint32{}}, rows.b); + gathered_buffers_[i] = ColumnBuffer(); + continue; + } uint32_t width = ValueWidth(view.type()); // A buffer a batch still holds is left to it: a fresh one is taken. gathered_buffers_[i] = context.TakeBuffer(width); @@ -190,32 +202,6 @@ out.SetRowCount(static_cast<uint32_t>(rows.size())); } -void DataframeWindows::Fill(uint32_t from, uint32_t count, RowBatch& out) { - // Only the windows move: the views stay as they were lent. - for (uint32_t i = 0; i < count_; ++i) { - lent_[i].set_start(from); - } - out.SetColumns(lent_, buffers_, count_); - out.SetRowCount(count); -} - -namespace { - -// From kMaxBatchRows - 1 down to 0: the last `count` reverse a batch. -const uint32_t* DescendingIndices() { - static const base::NoDestructor<std::array<uint32_t, kMaxBatchRows>> kTable( - [] { - std::array<uint32_t, kMaxBatchRows> table; - for (uint32_t i = 0; i < kMaxBatchRows; ++i) { - table[i] = kMaxBatchRows - 1 - i; - } - return table; - }()); - return kTable.ref().data(); -} - -} // namespace - void RangeReader::Init(base::Span<const dataframe::Column* const> columns, OperatorState& owner) { windows_.Init(columns, owner); @@ -228,9 +214,8 @@ uint32_t count = std::min(kMaxBatchRows, next_ - range_.b); next_ -= count; windows_.Fill(next_, count, out); - const uint32_t* descending = DescendingIndices(); - out.mutable_selection().Reorder(base::Span<const uint32_t>( - descending + (kMaxBatchRows - count), descending + kMaxBatchRows)); + out.mutable_selection().Reverse(); + out.SetLast(next_ == range_.b); return true; }
diff --git a/src/trace_processor/core/exec/dataframe_windows.h b/src/trace_processor/core/exec/dataframe_windows.h index 637afb6..e3ab36e 100644 --- a/src/trace_processor/core/exec/dataframe_windows.h +++ b/src/trace_processor/core/exec/dataframe_windows.h
@@ -50,7 +50,16 @@ void Init(base::Span<const dataframe::Column* const>, OperatorState& owner); // Points `out` at rows [from, from + count), all kept. - void Fill(uint32_t from, uint32_t count, RowBatch& out); + PERFETTO_ALWAYS_INLINE void Fill(uint32_t from, + uint32_t count, + RowBatch& out) { + // Only the windows move: the views stay as they were lent. + for (uint32_t i = 0; i < count_; ++i) { + lent_[i].set_start(from); + } + out.SetColumns(lent_, buffers_, count_); + out.SetRowCount(count); + } // Fills `out` with copies of the values at `rows`, in buffers taken from // `context`. A column with no storage of its own becomes stored Uint32s, @@ -105,6 +114,7 @@ uint32_t count = std::min(kMaxBatchRows, range_.e - next_); windows_.Fill(next_, count, out); next_ += count; + out.SetLast(next_ == range_.e); return true; } @@ -146,6 +156,7 @@ windows_.Gather(base::Span<const uint32_t>(next_, next_ + count), *context_, out); next_ += count; + out.SetLast(next_ == end_); return true; }
diff --git a/src/trace_processor/core/exec/filter.cc b/src/trace_processor/core/exec/filter.cc index 57c8e87..b6d0f0f 100644 --- a/src/trace_processor/core/exec/filter.cc +++ b/src/trace_processor/core/exec/filter.cc
@@ -16,6 +16,7 @@ #include "src/trace_processor/core/exec/filter.h" +#include <array> #include <cstdint> #include <cstring> #include <functional> @@ -60,22 +61,30 @@ bool contiguous; }; +using CompareFn = Rows (*)(const ColumnView&, + const ParamValue&, + const StringPool&, + Rows); + // Writes contiguous rows out, for the steps which read them one by one. -void Materialize(Rows& r) { - if (r.contiguous) { - std::iota(r.rows, r.rows + r.count, 0u); - r.contiguous = false; - } +PERFETTO_NO_INLINE Rows Materialize(Rows r) { + std::iota(r.rows, r.rows + r.count, 0u); + r.contiguous = false; + return r; } // Keeps the rows which are null if `nulls`, or which are not otherwise. -void KeepNulls(const ColumnView& column, bool nulls, Rows& r) { +PERFETTO_NO_INLINE Rows KeepNulls(const ColumnView& column, + bool nulls, + Rows r) { const BitVector* validity = column.validity(); if (column.kind() != ColumnView::Kind::kVariant && !validity) { r.count = nulls ? 0 : r.count; - return; + return r; } - Materialize(r); + if (r.contiguous) { + r = Materialize(r); + } if (column.kind() == ColumnView::Kind::kVariant) { const auto* data = static_cast<const Variant*>(column.data()); uint32_t start = column.start(); @@ -89,7 +98,7 @@ } } r.count = kept; - return; + return r; } uint32_t start = column.start(); uint32_t* rows = r.rows; @@ -102,19 +111,27 @@ } } r.count = kept; + return r; } +// Forward declaration for Compare<String, Glob>. +PERFETTO_NO_INLINE Rows KeepGlobMatches(const ColumnView& column, + GlobMatch& glob, + const StringPool& pool, + Rows r); + // Keeps the rows of a column of storage type T which compare true by O with // the param. The kernels read each row's value at the column's data // plus the row, so the data passed starts where the window does. template <typename T, typename O> -void Compare(const ColumnView& column, +Rows Compare(const ColumnView& column, const ParamValue& cast, const StringPool& pool, - Rows& r) { - // The value was cast for the column's storage type, which picks its - // alternative. - PERFETTO_DCHECK(cast.value.value.index() == StorageType::GetTypeIndex<T>()); + Rows r) { + if constexpr (!std::is_same_v<T, String> || + (!std::is_same_v<O, Eq> && !std::is_same_v<O, Ne>)) { + PERFETTO_DCHECK(cast.value.index() == StorageType::GetTypeIndex<T>()); + } if (r.contiguous) { // The rows are a run, so the values are read in order with no list. if constexpr (std::is_same_v<T, String>) { @@ -128,28 +145,35 @@ cast.string_id.raw_id(), Cmp()) - r.rows); r.contiguous = false; - return; + return r; } } else { using M = StorageType::VariantTypeAtIndex<T, CastFilterValueResult::Value>; const auto* data = static_cast<const M*>(column.data()) + column.start(); // A local, not to be reloaded after every row written. - M value = base::unchecked_get<M>(cast.value.value); + M value = base::unchecked_get<M>(cast.value); r.count = static_cast<uint32_t>( filter::FilterRange( data, 0, r.count, r.rows, value, filter::comparators::IntegerOrDoubleComparator<M, O>()) - r.rows); r.contiguous = false; - return; + return r; } } - Materialize(r); + if (r.contiguous) { + r = Materialize(r); + } uint32_t* begin = r.rows; uint32_t* end = r.rows + r.count; uint32_t* kept; if constexpr (std::is_same_v<T, String>) { + if constexpr (std::is_same_v<O, Glob>) { + if (GlobMatch* glob = cast.glob()) { + return KeepGlobMatches(column, *glob, pool, r); + } + } const auto* data = static_cast<const StringPool::Id*>(column.data()) + column.start(); if constexpr (std::is_same_v<O, Eq> || std::is_same_v<O, Ne>) { @@ -160,99 +184,87 @@ kept = filter::Filter(reinterpret_cast<const uint32_t*>(data), begin, end, begin, cast.string_id.raw_id(), Cmp()); } else if constexpr (std::is_same_v<O, Regex>) { - kept = filter::StringFilterRegex(&pool, data, *cast.regex, begin, end, + kept = filter::StringFilterRegex(&pool, data, cast.regex(), begin, end, begin); } else { kept = filter::FilterStringCompare<O>(&pool, data, begin, end, begin, - cast.text.c_str()); + cast.text().c_str()); } } else { using M = StorageType::VariantTypeAtIndex<T, CastFilterValueResult::Value>; const auto* data = static_cast<const M*>(column.data()) + column.start(); - M value = base::unchecked_get<M>(cast.value.value); + M value = base::unchecked_get<M>(cast.value); kept = filter::Filter(data, begin, end, begin, value, filter::comparators::IntegerOrDoubleComparator<M, O>()); } r.count = static_cast<uint32_t>(kept - begin); + return r; } // Keeps the rows of a column of storage type T whose value is in `list`. template <typename T> -void KeepIn(const ColumnView& column, - const CastFilterValueListResult& list, - Rows& r) { +Rows KeepIn(const ColumnView& column, + const ParamValue& cast, + const StringPool&, + Rows r) { using M = StorageType::VariantTypeAtIndex<T, CastFilterValueListResult::Value>; const auto* data = static_cast<const M*>(column.data()) + column.start(); - Materialize(r); + if (r.contiguous) { + r = Materialize(r); + } base::Span<uint32_t> rows(r.rows, r.rows + r.count); base::Span<uint32_t> dest = rows; - filter::NonIndexedFilterInScan<T>(list, data, column.start(), rows, dest); + filter::NonIndexedFilterInScan<T>(cast.list(), data, column.start(), rows, + dest); r.count = static_cast<uint32_t>(dest.size()); + return r; } +using CompareTable = + std::array<std::array<CompareFn, StorageType::kSize>, Op::kSize>; + template <typename T> -void CompareByOp(const Op& op, - const ColumnView& column, - const ParamValue& cast, - const StringPool& pool, - Rows& r) { - switch (op.index()) { - case Op::GetTypeIndex<Eq>(): - return Compare<T, Eq>(column, cast, pool, r); - case Op::GetTypeIndex<Ne>(): - return Compare<T, Ne>(column, cast, pool, r); - case Op::GetTypeIndex<Lt>(): - return Compare<T, Lt>(column, cast, pool, r); - case Op::GetTypeIndex<Le>(): - return Compare<T, Le>(column, cast, pool, r); - case Op::GetTypeIndex<Gt>(): - return Compare<T, Gt>(column, cast, pool, r); - case Op::GetTypeIndex<Ge>(): - return Compare<T, Ge>(column, cast, pool, r); - case Op::GetTypeIndex<In>(): - return KeepIn<T>(column, *cast.list, r); - case Op::GetTypeIndex<Glob>(): - if constexpr (std::is_same_v<T, String>) { - return Compare<T, Glob>(column, cast, pool, r); - } - break; - case Op::GetTypeIndex<Regex>(): - if constexpr (std::is_same_v<T, String>) { - return Compare<T, Regex>(column, cast, pool, r); - } - break; - default: - break; +constexpr void FillCompareColumn(CompareTable& t) { + constexpr size_t kCol = StorageType::GetTypeIndex<T>(); + t[Op::GetTypeIndex<Eq>()][kCol] = &Compare<T, Eq>; + t[Op::GetTypeIndex<Ne>()][kCol] = &Compare<T, Ne>; + t[Op::GetTypeIndex<Lt>()][kCol] = &Compare<T, Lt>; + t[Op::GetTypeIndex<Le>()][kCol] = &Compare<T, Le>; + t[Op::GetTypeIndex<Gt>()][kCol] = &Compare<T, Gt>; + t[Op::GetTypeIndex<Ge>()][kCol] = &Compare<T, Ge>; + t[Op::GetTypeIndex<In>()][kCol] = &KeepIn<T>; + if constexpr (std::is_same_v<T, String>) { + t[Op::GetTypeIndex<Glob>()][kCol] = &Compare<T, Glob>; + t[Op::GetTypeIndex<Regex>()][kCol] = &Compare<T, Regex>; } - PERFETTO_FATAL("Unsupported filter operator"); } -// Drops the nulls, which no comparison or IN is true of. False if nothing is -// left to compare: no row can match the cast value, every row left does, or no -// row is left. -bool KeepRowsWhichCanMatch(const ParamValue& cast, - const ColumnView& column, - Rows& r) { - if (cast.value.validity == CastFilterValueResult::kNoneMatch) { - r.count = 0; - return false; - } - KeepNulls(column, /*nulls=*/false, r); - return cast.value.validity == CastFilterValueResult::kValid && r.count != 0; +constexpr CompareTable MakeCompareTable() { + CompareTable t{}; + FillCompareColumn<Uint32>(t); + FillCompareColumn<Int32>(t); + FillCompareColumn<Int64>(t); + FillCompareColumn<Double>(t); + FillCompareColumn<String>(t); + return t; } +constexpr CompareTable kCompareTable = MakeCompareTable(); + // Keeps the rows of a string `column` whose value `glob`'s pattern matches. // Rows are matched one at a time until as many have been as a quarter of the // pool's strings; then, as the interpreter does for a large input, every // string in the pool is matched in one pass, and each row after is a bit test. -void KeepGlobMatches(const ColumnView& column, - GlobMatch& glob, - const StringPool& pool, - Rows& r) { +PERFETTO_NO_INLINE Rows KeepGlobMatches(const ColumnView& column, + GlobMatch& glob, + const StringPool& pool, + Rows r) { const util::GlobMatcher& matcher = glob.matcher; - Materialize(r); + if (r.contiguous) { + r = Materialize(r); + } const auto* data = static_cast<const StringPool::Id*>(column.data()) + column.start(); uint32_t kept = 0; @@ -281,7 +293,7 @@ kept += matcher.Matches(pool.Get(data[row])); } r.count = kept; - return; + return r; } const BitVector& matches = glob.matches; for (uint32_t i = 0; i < count; ++i) { @@ -295,13 +307,17 @@ kept += match; } r.count = kept; + return r; } // Copies the values of a sparse `column` at the rows kept, none of which is // null, into `values` at each row, and views them as a flat column. template <uint32_t kWidth> -ColumnView GatherSparse(const ColumnView& column, Rows& r, uint8_t* values) { - Materialize(r); +PERFETTO_NO_INLINE std::pair<ColumnView, Rows> +GatherSparse(const ColumnView& column, Rows r, uint8_t* values) { + if (r.contiguous) { + r = Materialize(r); + } const auto* packed = static_cast<const uint8_t*>(column.sparse_data()); // Locals: the bytes written could otherwise alias any of them. const uint32_t* rows = r.rows; @@ -321,14 +337,14 @@ memcpy(value, &slot, sizeof(slot)); } } - return ColumnView::Reference(column.type(), values); + return {ColumnView::Reference(column.type(), values), r}; } // Writes the values of a sequence at the rows kept into `values` at each row, // and views them as a flat column. Contiguous rows stay contiguous, so the // comparison can still filter them as a range. PERFETTO_NO_INLINE ColumnView GatherSequence(const ColumnView& column, - Rows& r, + Rows r, uint32_t* values) { uint32_t start = column.start(); if (r.contiguous) { @@ -343,44 +359,45 @@ return ColumnView::Reference(StorageType{Uint32{}}, values); } -// Applies a comparison or IN to the rows kept so far. The values of a sparse -// column or a sequence at those rows are written to `sparse_values` first. -void ApplyCondition(const Op& op, - const ParamValue& cast, - const ColumnView& lent, - const StringPool& pool, - uint64_t* sparse_values, - Rows& r) { - if (!KeepRowsWhichCanMatch(cast, lent, r)) { - return; +PERFETTO_NO_INLINE void UnpackSelection(const uint16_t* indices, + uint32_t count, + uint32_t* out) { + for (uint32_t i = 0; i < count; ++i) { + out[i] = indices[i]; + } +} + +PERFETTO_NO_INLINE Rows ApplySlow(const ColumnView& lent, + Op op, + const ParamValue& cast, + const StringPool& pool, + uint64_t* sparse_values, + Rows r) { + if (cast.validity == CastFilterValueResult::kNoneMatch) { + r.count = 0; + return r; + } + if (lent.kind() == ColumnView::Kind::kVariant || lent.validity() != nullptr) { + r = KeepNulls(lent, /*nulls=*/false, r); + } + if (cast.validity != CastFilterValueResult::kValid || r.count == 0) { + return r; } ColumnView column = lent; if (lent.kind() == ColumnView::Kind::kSparse) { auto* values = reinterpret_cast<uint8_t*>(sparse_values); - column = ValueWidth(lent.type()) == sizeof(uint32_t) - ? GatherSparse<sizeof(uint32_t)>(lent, r, values) - : GatherSparse<sizeof(uint64_t)>(lent, r, values); + auto gathered = ValueWidth(lent.type()) == sizeof(uint32_t) + ? GatherSparse<sizeof(uint32_t)>(lent, r, values) + : GatherSparse<sizeof(uint64_t)>(lent, r, values); + column = gathered.first; + r = gathered.second; } else if (lent.kind() == ColumnView::Kind::kSequence) { column = GatherSequence(lent, r, reinterpret_cast<uint32_t*>(sparse_values)); } - if (op.Is<Glob>() && cast.glob) { - return KeepGlobMatches(column, *cast.glob, pool, r); - } - switch (column.type().index()) { - case StorageType::GetTypeIndex<Uint32>(): - return CompareByOp<Uint32>(op, column, cast, pool, r); - case StorageType::GetTypeIndex<Int32>(): - return CompareByOp<Int32>(op, column, cast, pool, r); - case StorageType::GetTypeIndex<Int64>(): - return CompareByOp<Int64>(op, column, cast, pool, r); - case StorageType::GetTypeIndex<Double>(): - return CompareByOp<Double>(op, column, cast, pool, r); - case StorageType::GetTypeIndex<String>(): - return CompareByOp<String>(op, column, cast, pool, r); - default: - PERFETTO_FATAL("Unsupported storage type"); - } + CompareFn fn = kCompareTable[op.index()][column.type().index()]; + PERFETTO_DCHECK(fn != nullptr); + return fn(column, cast, pool, r); } } // namespace @@ -402,32 +419,53 @@ bool Filter::Process(RowBatch& batch, OperatorState& state) const { State& s = state.Cast<State>(); - const Context& context = s.context(); - const ParamValue* values = context.params().b; const Selection& selection = batch.selection(); Rows r{s.rows.begin(), selection.size(), selection.prefix()}; - if (!r.contiguous) { - const uint16_t* indices = selection.indices(); - for (uint32_t i = 0, n = r.count; i < n; ++i) { - r.rows[i] = indices[i]; - } - } - for (uint32_t i = 0; i < conditions_.size() && r.count != 0; ++i) { - const Condition& c = conditions_[i]; - const ColumnView& column = batch.column(c.column); - if (c.op.IsAnyOf<NullOp>()) { - KeepNulls(column, c.op.Is<IsNull>(), r); - continue; - } - PERFETTO_DCHECK(column.kind() != ColumnView::Kind::kVariant); - PERFETTO_DCHECK(c.param < context.params().size()); - ApplyCondition(c.op, values[c.param], column, *pool_, - s.sparse_values.begin(), r); - } - if (r.contiguous && r.count == selection.size()) { + if (PERFETTO_UNLIKELY(r.count == 0 || conditions_.empty())) { return true; } - Materialize(r); + if (PERFETTO_UNLIKELY(!r.contiguous)) { + UnpackSelection(selection.indices(), r.count, r.rows); + } + const Context& context = s.context(); + const ParamValue* values = context.params().b; + for (const Condition& c : conditions_) { + const ColumnView& column = batch.column(c.column); + if (PERFETTO_UNLIKELY(c.op.IsAnyOf<NullOp>())) { + r = KeepNulls(column, c.op.Is<IsNull>(), r); + } else { + PERFETTO_DCHECK(column.kind() != ColumnView::Kind::kVariant); + PERFETTO_DCHECK(c.param < context.params().size()); + const ParamValue& cast = values[c.param]; + // Dense nullable flat columns hold initialized slots, so numeric + // comparisons and interned-id string = / != can filter contiguously + // first and drop nulls only from the surviving rows. + bool null_before_compare = column.validity() != nullptr && + column.type().Is<String>() && !c.op.Is<Eq>() && + !c.op.Is<Ne>(); + if (PERFETTO_UNLIKELY(cast.validity != CastFilterValueResult::kValid || + column.kind() != ColumnView::Kind::kFlat || + null_before_compare)) { + r = ApplySlow(column, c.op, cast, *pool_, s.sparse_values.begin(), r); + } else { + CompareFn fn = kCompareTable[c.op.index()][column.type().index()]; + PERFETTO_DCHECK(fn != nullptr); + r = fn(column, cast, *pool_, r); + if (column.validity() != nullptr && r.count != 0) { + r = KeepNulls(column, /*nulls=*/false, r); + } + } + } + if (r.count == 0) { + break; + } + } + if (PERFETTO_UNLIKELY(r.contiguous)) { + if (r.count == selection.size()) { + return true; + } + r = Materialize(r); + } batch.mutable_selection().KeepRows( base::Span<const uint32_t>(r.rows, r.rows + r.count)); return true;
diff --git a/src/trace_processor/core/exec/filter_unittest.cc b/src/trace_processor/core/exec/filter_unittest.cc index b043848..fe6b4ee 100644 --- a/src/trace_processor/core/exec/filter_unittest.cc +++ b/src/trace_processor/core/exec/filter_unittest.cc
@@ -437,5 +437,54 @@ EXPECT_THAT(kept(IsNotNull{}), ElementsAre(20, 30)); } +TEST(FilterNullTest, DenseNullDropsMatchingNullSlots) { + StringPool pool; + StringPool::Id a = pool.InternString("a"); + StringPool::Id b = pool.InternString("b"); + // Rows 1 and 3 are null even though their underlying storage holds matching + // values (e.g. placeholder 0 or a stale interned id). + StringPool::Id strings[] = {a, a, b, StringPool::Id::Null(), a}; + int64_t numbers[] = {0, 0, 10, 10, 20}; + int64_t ids[] = {0, 1, 2, 3, 4}; + BitVector validity = BitVector::CreateWithSize(5, true); + validity.clear(1); + validity.clear(3); + + auto run = [&](ColumnView col, StorageType type, Op op, Value v) { + Filter filter(test::InArena<Filter::Condition>({{0, op, 0}}), &pool); + Context context; + OperatorState* state = filter.MakeState(context); + Values fetcher; + fetcher.v = {v}; + ParamValue cast[1]; + GetParamCaster<Values>(type, op)(fetcher, 0, pool, &cast[0]); + context.SetParams(base::Span<const ParamValue>(cast)); + RowBatch batch; + batch.AddBorrowedColumn(col); + batch.AddBorrowedColumn( + ColumnView::Reference(StorageType{Int64{}}, ids, nullptr)); + batch.SetRowCount(5); + PERFETTO_CHECK(filter.Process(batch, *state)); + return test::ReadColumn<int64_t>(batch, 1); + }; + + ColumnView str_col = + ColumnView::Reference(StorageType{String{}}, strings, &validity); + EXPECT_THAT(run(str_col, StorageType{String{}}, Eq{}, "a"), + ElementsAre(0, 4)); + EXPECT_THAT(run(str_col, StorageType{String{}}, Ne{}, "a"), ElementsAre(2)); + EXPECT_THAT(run(str_col, StorageType{String{}}, Lt{}, "b"), + ElementsAre(0, 4)); + + ColumnView num_col = + ColumnView::Reference(StorageType{Int64{}}, numbers, &validity); + EXPECT_THAT(run(num_col, StorageType{Int64{}}, Eq{}, int64_t{0}), + ElementsAre(0)); + EXPECT_THAT(run(num_col, StorageType{Int64{}}, Ne{}, int64_t{0}), + ElementsAre(2, 4)); + EXPECT_THAT(run(num_col, StorageType{Int64{}}, Lt{}, int64_t{15}), + ElementsAre(0, 2)); +} + } // namespace } // namespace perfetto::trace_processor::core::exec
diff --git a/src/trace_processor/core/exec/index_scan.cc b/src/trace_processor/core/exec/index_scan.cc index 5e5dc33..b349404 100644 --- a/src/trace_processor/core/exec/index_scan.cc +++ b/src/trace_processor/core/exec/index_scan.cc
@@ -113,11 +113,11 @@ const Equality& eq = equalities_[i]; PERFETTO_DCHECK(eq.param < context.params().size()); const ParamValue& cast = values[eq.param]; - if (cast.value.validity == CastFilterValueResult::kNoneMatch) { + if (cast.validity == CastFilterValueResult::kNoneMatch) { entries.e = entries.b; break; } - if (cast.value.validity == CastFilterValueResult::kAllMatch) { + if (cast.validity == CastFilterValueResult::kAllMatch) { one_key = false; continue; } @@ -129,7 +129,7 @@ CastFilterValueResult::Value(pool_->Get(cast.string_id).c_str()), entries); } else { - entries = filter::SearchIndexEqual(column, cast.value.value, entries); + entries = filter::SearchIndexEqual(column, cast.value, entries); } } if (searches_.empty() || entries.empty()) {
diff --git a/src/trace_processor/core/exec/lookup_benchmark.cc b/src/trace_processor/core/exec/lookup_benchmark.cc index c2036f5..204b6f8 100644 --- a/src/trace_processor/core/exec/lookup_benchmark.cc +++ b/src/trace_processor/core/exec/lookup_benchmark.cc
@@ -71,8 +71,12 @@ keys.push_back(pool.InternString( base::StackString<32>("debug.key_%u", i).string_view())); } - dataframe::AdhocDataframeBuilder builder({"arg_set_id", "key", "int_value"}, - &pool); + // Match ArgTable's DenseNull key column (a trailing null row keeps the + // DenseNull overlay without affecting any lookup in [0, kRows)). + dataframe::AdhocDataframeBuilder builder( + {"arg_set_id", "key", "int_value"}, &pool, + dataframe::AdhocDataframeBuilder::Options{ + {}, dataframe::NullabilityType::kDenseNull}); uint32_t row = 0; while (row < kRows) { uint32_t size = @@ -92,10 +96,14 @@ } row += size; } + PERFETTO_CHECK(builder.PushNonNull(kArgSetId, int64_t{kRows})); + builder.PushNull(kKey); + builder.PushNull(kIntValue); auto built = std::move(builder).Build(); PERFETTO_CHECK(built.ok()); df = std::make_unique<dataframe::Dataframe>(std::move(*built)); PERFETTO_CHECK(df->column(kArgSetId).sort_state.Is<SetIdSorted>()); + PERFETTO_CHECK(df->column(kKey).null_storage.nullability().Is<DenseNull>()); // Each lookup is of a key its set has. struct Key : dataframe::CellCallback {
diff --git a/src/trace_processor/core/exec/operator.h b/src/trace_processor/core/exec/operator.h index 907e4d1..6031cb4 100644 --- a/src/trace_processor/core/exec/operator.h +++ b/src/trace_processor/core/exec/operator.h
@@ -172,6 +172,8 @@ virtual const RowsInPlace* InPlace(const OperatorState&) const { return nullptr; } + // Whether InPlace() may ever return non-null for this source. + virtual bool HasInPlace() const { return false; } // Identifies the concrete class. Each class has a `static constexpr char // kType` and returns its address. virtual const void* type() const = 0;
diff --git a/src/trace_processor/core/exec/param_value.h b/src/trace_processor/core/exec/param_value.h index 3355b34..312d41b 100644 --- a/src/trace_processor/core/exec/param_value.h +++ b/src/trace_processor/core/exec/param_value.h
@@ -66,79 +66,119 @@ // allocates, and nothing dispatches on the column's type or the comparison, // which matters when each run looks up one row. struct ParamValue { - filter::CastFilterValueResult value = - filter::CastFilterValueResult::NoneMatch(); + // Cold storage allocated lazily for string ordering, REGEXP, GLOB, and IN. + struct Extra { + std::string text; + std::optional<base::Regex> regex; + filter::CastFilterValueListResult::Ptr list; + std::unique_ptr<GlobMatch> glob; + }; + + Extra& EnsureExtra() { + if (!extra) { + extra = std::make_unique<Extra>(); + } + return *extra; + } + + const std::string& text() const { return extra->text; } + const base::Regex& regex() const { return *extra->regex; } + const filter::CastFilterValueListResult& list() const { return *extra->list; } + GlobMatch* glob() const { return extra ? extra->glob.get() : nullptr; } + + filter::CastFilterValueResult::Value value = + filter::CastFilterValueResult::Id{0}; + filter::CastFilterValueResult::Validity validity = + filter::CastFilterValueResult::kNoneMatch; // For a string compared by = or !=: the string's id in the pool. StringPool::Id string_id = StringPool::Id::Null(); - // For a string compared by order: its text, which `value` points to. Its - // buffer is reused, so only a string longer than any before allocates. - std::string text; - // For REGEXP: the pattern, compiled once a run rather than once a batch. - std::optional<base::Regex> regex; - // For IN: the list's values, while `value` says whether any can match. Its - // allocations are reused from run to run. - filter::CastFilterValueListResult::Ptr list; - // For a GLOB literal with wildcards (see AttachGlob). - std::unique_ptr<GlobMatch> glob; + std::unique_ptr<Extra> extra; }; +static_assert(sizeof(ParamValue) <= 32); // Gives `value`, a GLOB literal just cast, its GlobMatch, so that it is // matched against the pool once however many runs read it. A pattern without // wildcards still compares string ids, and needs none. inline void AttachGlob(ParamValue* value) { - value->glob.reset(); - if (value->value.validity != filter::CastFilterValueResult::kValid) { + if (value->extra) { + value->extra->glob.reset(); + } + if (value->validity != filter::CastFilterValueResult::kValid) { return; } - auto glob = std::make_unique<GlobMatch>(base::StringView(value->text)); + auto glob = std::make_unique<GlobMatch>(base::StringView(value->text())); if (!glob->matcher.IsEquality()) { - value->glob = std::move(glob); + value->EnsureExtra().glob = std::move(glob); } } namespace internal { -// Casts the value `fetcher` has at `index` into `out`, for a column of -// storage type T compared by `op`. `pool` holds the column's strings. +// Casts the value `fetcher` has at `index` into `out`, for a numeric column of +// storage type T compared by `op`. template <typename T, typename Fetcher> void CastParamFor(Fetcher& fetcher, uint32_t index, NonNullOp op, - const StringPool& pool, + const StringPool&, ParamValue* out) { - out->value = filter::CastFilterValue<T>(index, fetcher, op); - if constexpr (std::is_same_v<T, String>) { - if (out->value.validity != filter::CastFilterValueResult::kValid) { + auto res = filter::CastFilterValue<T>(index, fetcher, op); + out->validity = res.validity; + if (PERFETTO_LIKELY(res.validity == filter::CastFilterValueResult::kValid)) { + out->value = res.value; + } +} + +// Casts a string param for = or !=, resolving it to an interned pool id. +// Split from CastStringOtherParam so string equality avoids regex stack state. +template <bool kEq, typename Fetcher> +void CastStringEqParam(Fetcher& fetcher, + uint32_t index, + NonNullOp, + const StringPool& pool, + ParamValue* out) { + auto type = fetcher.GetValueType(index); + if (PERFETTO_LIKELY(type == Fetcher::kString)) { + const char* text = fetcher.GetStringValue(index); + std::optional<StringPool::Id> id = pool.GetId(base::StringView(text)); + if (PERFETTO_LIKELY(id.has_value())) { + out->validity = filter::CastFilterValueResult::kValid; + out->string_id = *id; + } else { + out->validity = kEq ? filter::CastFilterValueResult::kNoneMatch + : filter::CastFilterValueResult::kAllMatch; + } + return; + } + PERFETTO_DCHECK(type == Fetcher::kNull || type == Fetcher::kInt64 || + type == Fetcher::kDouble); + out->validity = type == Fetcher::kNull || kEq + ? filter::CastFilterValueResult::kNoneMatch + : filter::CastFilterValueResult::kAllMatch; +} + +// Casts a string param for ordering, GLOB, or REGEXP. +template <typename Fetcher> +void CastStringOtherParam(Fetcher& fetcher, + uint32_t index, + NonNullOp op, + const StringPool&, + ParamValue* out) { + auto res = filter::CastFilterValue<String>(index, fetcher, op); + out->validity = res.validity; + if (res.validity != filter::CastFilterValueResult::kValid) { + return; + } + ParamValue::Extra& extra = out->EnsureExtra(); + extra.text.assign(base::unchecked_get<const char*>(res.value)); + out->value = extra.text.c_str(); + if (op.Is<Regex>()) { + base::StatusOr<base::Regex> regex = base::Regex::Create(extra.text); + if (!regex.ok()) { + out->validity = filter::CastFilterValueResult::kNoneMatch; return; } - const char* text = base::unchecked_get<const char*>(out->value.value); - if (op.Is<Eq>() || op.Is<Ne>()) { - // A string no row holds equals none of them, and differs from all. - std::optional<StringPool::Id> id = pool.GetId(base::StringView(text)); - if (!id) { - out->value = op.Is<Eq>() ? filter::CastFilterValueResult::NoneMatch() - : filter::CastFilterValueResult::AllMatch(); - return; - } - out->string_id = *id; - // Whatever `text` points to need not outlive the cast. - out->value.value = static_cast<const char*>(nullptr); - } else { - out->text.assign(text); - out->value.value = out->text.c_str(); - if (op.Is<Regex>()) { - // As the interpreter does, a pattern which does not compile matches - // nothing. - base::StatusOr<base::Regex> regex = base::Regex::Create(out->text); - if (!regex.ok()) { - out->value = filter::CastFilterValueResult::NoneMatch(); - return; - } - out->regex = std::move(*regex); - } - } - } else { - base::ignore_result(pool); + extra.regex = std::move(*regex); } } @@ -150,7 +190,7 @@ NonNullOp op, const StringPool& pool, ParamValue* out) { - auto& list = out->list; + auto& list = out->EnsureExtra().list; if (!list) { list = std::make_unique<filter::CastFilterValueListResult>(); list->Init<T>(); @@ -158,7 +198,7 @@ list->Clear<T>(); } filter::CastFilterValueList<T>(index, fetcher, op, &pool, *list); - out->value.validity = list->validity; + out->validity = list->validity; } } // namespace internal @@ -202,7 +242,13 @@ case StorageType::GetTypeIndex<Double>(): return {&internal::CastParamFor<Double, Fetcher>, non_null}; case StorageType::GetTypeIndex<String>(): - return {&internal::CastParamFor<String, Fetcher>, non_null}; + if (non_null.Is<Eq>()) { + return {&internal::CastStringEqParam<true, Fetcher>, non_null}; + } + if (non_null.Is<Ne>()) { + return {&internal::CastStringEqParam<false, Fetcher>, non_null}; + } + return {&internal::CastStringOtherParam<Fetcher>, non_null}; default: PERFETTO_FATAL("Unsupported storage type"); }
diff --git a/src/trace_processor/core/exec/pipeline.cc b/src/trace_processor/core/exec/pipeline.cc index 8fb6699..d91daf3 100644 --- a/src/trace_processor/core/exec/pipeline.cc +++ b/src/trace_processor/core/exec/pipeline.cc
@@ -160,9 +160,17 @@ } // A batch with no rows left has nothing for later transforms to do. if (PERFETTO_UNLIKELY(!batch->size())) { + if (batch->last() && !(flags & State::kSegmented) && + !s.context().holding()) { + s.flags |= State::kSourceDone; + return nullptr; + } return NextSlow(s); } } + if (PERFETTO_UNLIKELY(s.context().holding())) { + batch->SetLast(false); + } return batch; } @@ -171,6 +179,9 @@ RowBatch* batch = SegmentNext(last, *s.output, s); if (!batch) { Stop(s); + } else { + // Slow paths may hold state that requires Stop() on exhaustion. + batch->SetLast(false); } return batch; }
diff --git a/src/trace_processor/core/exec/pipeline.h b/src/trace_processor/core/exec/pipeline.h index fcd9515..58b93e0 100644 --- a/src/trace_processor/core/exec/pipeline.h +++ b/src/trace_processor/core/exec/pipeline.h
@@ -58,6 +58,13 @@ void Rewind(OperatorState& state) const override; // The last step's rows in place, if it is an operator serving them so. const RowsInPlace* InPlace(const OperatorState& state) const override; + bool HasInPlace() const override { + const Segment& last = segments_[last_segment_]; + if (last.first != last.end) { + return false; + } + return last.start_op != nullptr || source_.HasInPlace(); + } // The pipeline's input. const Source& input() const { return source_; }
diff --git a/src/trace_processor/core/exec/row_batch.cc b/src/trace_processor/core/exec/row_batch.cc index 80af734..1abef8e 100644 --- a/src/trace_processor/core/exec/row_batch.cc +++ b/src/trace_processor/core/exec/row_batch.cc
@@ -53,6 +53,7 @@ other.buffer_count_); } selection_.CopyFrom(other.selection_); + last_ = false; other.Reset(); return *this; } @@ -63,6 +64,7 @@ own_buffers_.assign(other.buffers_, other.buffers_ + other.buffer_count_); Repoint(); selection_.CopyFrom(other.selection_); + last_ = false; } void RowBatch::Project(const RowBatch& other, @@ -72,6 +74,7 @@ AddColumn(other.column(column), other.buffer(column)); } selection_.CopyFrom(other.selection_); + last_ = false; } void RowBatch::AddColumn(const ColumnView& view, ColumnBuffer buffer) { @@ -95,6 +98,7 @@ own_buffers_.clear(); Repoint(); selection_.Reset(0); + last_ = false; } void RowBatch::Own() {
diff --git a/src/trace_processor/core/exec/row_batch.h b/src/trace_processor/core/exec/row_batch.h index 0992dd5..c19c420 100644 --- a/src/trace_processor/core/exec/row_batch.h +++ b/src/trace_processor/core/exec/row_batch.h
@@ -67,7 +67,14 @@ uint32_t row_count() const { return selection_.rows(); } // Gives every column `rows` rows, all kept. - void SetRowCount(uint32_t rows) { selection_.Reset(rows); } + void SetRowCount(uint32_t rows) { + selection_.Reset(rows); + last_ = false; + } + + // Whether the source has no more batches after this one (clean EOF). + bool last() const { return last_; } + void SetLast(bool last = true) { last_ = last; } const Selection& selection() const { return selection_; } Selection& mutable_selection() { return selection_; } @@ -154,6 +161,10 @@ const ColumnBuffer* buffers, uint32_t buffer_count) { PERFETTO_DCHECK(buffer_count == view_count); + if (PERFETTO_LIKELY(!owned_ && columns_ == views && + column_count_ == view_count)) { + return; + } // Buffers kept from before would only be released later. own_buffers_.clear(); owned_ = false; @@ -182,6 +193,7 @@ const ColumnBuffer* buffers_ = nullptr; uint32_t buffer_count_ = 0; bool owned_ = true; + bool last_ = false; std::vector<ColumnView> own_columns_; std::vector<ColumnBuffer> own_buffers_; Selection selection_;
diff --git a/src/trace_processor/core/exec/row_batch_unittest.cc b/src/trace_processor/core/exec/row_batch_unittest.cc index 5714be8..24c5a03 100644 --- a/src/trace_processor/core/exec/row_batch_unittest.cc +++ b/src/trace_processor/core/exec/row_batch_unittest.cc
@@ -96,5 +96,24 @@ EXPECT_THAT(test::ReadColumn<int64_t>(batch, 0), ElementsAre(0, 10)); } +TEST(RowBatchTest, ReversesSelection) { + std::vector<int64_t> values = {0, 10, 20, 30}; + RowBatch batch; + batch.AddBorrowedColumn( + ColumnView::Reference(StorageType{Int64{}}, values.data())); + batch.SetRowCount(4); + batch.mutable_selection().Reverse(); + EXPECT_FALSE(batch.selection().prefix()); + EXPECT_THAT(test::ReadColumn<int64_t>(batch, 0), ElementsAre(30, 20, 10, 0)); + + batch.mutable_selection().Reverse(); + EXPECT_THAT(test::ReadColumn<int64_t>(batch, 0), ElementsAre(0, 10, 20, 30)); + + batch.SetRowCount(1); + batch.mutable_selection().Reverse(); + EXPECT_TRUE(batch.selection().prefix()); + EXPECT_THAT(test::ReadColumn<int64_t>(batch, 0), ElementsAre(0)); +} + } // namespace } // namespace perfetto::trace_processor::core::exec
diff --git a/src/trace_processor/core/exec/row_cursor.cc b/src/trace_processor/core/exec/row_cursor.cc index bb517cb..09f237f 100644 --- a/src/trace_processor/core/exec/row_cursor.cc +++ b/src/trace_processor/core/exec/row_cursor.cc
@@ -21,10 +21,12 @@ namespace perfetto::trace_processor::core::exec { RowCursor::RowCursor(const Source& source) - : source_(source), state_(source.MakeState(context_)) {} + : source_(source), + state_(source.MakeState(context_)), + may_be_in_place_(source.HasInPlace()) {} RowCursor::RowCursor(const Source& source, const Pipelines& pipelines) - : source_(source) { + : source_(source), may_be_in_place_(source.HasInPlace()) { context_.SetPipelines(&pipelines); state_ = source.MakeState(context_); } @@ -41,13 +43,22 @@ current_ = nullptr; return false; } + exhausted_ = batch_->last(); if (batch_->size()) { index_ = 0; size_ = batch_->size(); - in_place_ = source_.InPlace(*state_); + if (PERFETTO_UNLIKELY(may_be_in_place_)) { + in_place_ = source_.InPlace(*state_); + } Locate(); return true; } + if (exhausted_) { + index_ = 0; + size_ = 0; + current_ = nullptr; + return false; + } } }
diff --git a/src/trace_processor/core/exec/row_cursor.h b/src/trace_processor/core/exec/row_cursor.h index 008a756..fb7f518 100644 --- a/src/trace_processor/core/exec/row_cursor.h +++ b/src/trace_processor/core/exec/row_cursor.h
@@ -61,19 +61,23 @@ in_place_ = nullptr; current_ = nullptr; current_row_ = 0; + exhausted_ = false; return Pull(); } // Moves to the next row, pulling a new batch if this one is exhausted. At // eof it stays put and keeps returning false. - bool Next() { - if (index_ == size_) { + PERFETTO_ALWAYS_INLINE bool Next() { + if (PERFETTO_UNLIKELY(index_ == size_)) { return false; } if (++index_ != size_) { Locate(); return true; } + if (PERFETTO_LIKELY(exhausted_)) { + return false; + } return Pull(); } @@ -90,7 +94,10 @@ } // The batch the current row is in. - const RowBatch& batch() const { return current_ ? *current_ : scratch_; } + PERFETTO_ALWAYS_INLINE const RowBatch& batch() const { + PERFETTO_DCHECK(current_); + return *current_; + } // Which of the rows batch() keeps the current row is. Not for rows read in // place (see Source::InPlace). uint32_t row() const { @@ -108,7 +115,7 @@ // Finds where the current row is held. PERFETTO_ALWAYS_INLINE void Locate() { - if (in_place_) { + if (PERFETTO_UNLIKELY(in_place_ != nullptr)) { uint32_t at = placed_ + index_; current_ = in_place_->batches[in_place_->batch[at]]; current_row_ = in_place_->row[at]; @@ -135,6 +142,8 @@ // Where the current row is held. const RowBatch* current_ = nullptr; uint32_t current_row_ = 0; + bool may_be_in_place_ = false; + bool exhausted_ = false; }; } // namespace perfetto::trace_processor::core::exec
diff --git a/src/trace_processor/core/exec/selection.cc b/src/trace_processor/core/exec/selection.cc index 456ea82..3995a2c 100644 --- a/src/trace_processor/core/exec/selection.cc +++ b/src/trace_processor/core/exec/selection.cc
@@ -16,6 +16,7 @@ #include "src/trace_processor/core/exec/selection.h" +#include <algorithm> #include <cstdint> #include <cstring> @@ -163,6 +164,20 @@ size_ = n; } +void Selection::Reverse() { + if (size_ <= 1) { + return; + } + if (prefix_) { + for (uint32_t i = 0; i < size_; ++i) { + indices_[i] = static_cast<uint16_t>(size_ - 1 - i); + } + prefix_ = false; + } else { + std::reverse(indices_, indices_ + size_); + } +} + void Selection::KeepRange(uint32_t first, uint32_t count) { PERFETTO_DCHECK(first + count <= size_); if (first != 0) {
diff --git a/src/trace_processor/core/exec/selection.h b/src/trace_processor/core/exec/selection.h index 901700b..a7eaaec 100644 --- a/src/trace_processor/core/exec/selection.h +++ b/src/trace_processor/core/exec/selection.h
@@ -82,6 +82,9 @@ // row. void KeepMatching(const uint32_t* values, uint32_t value, bool equal); + // Reverses the order of the rows kept. + void Reverse(); + // Keeps `count` of the rows kept, from the `first`. void KeepRange(uint32_t first, uint32_t count);
diff --git a/src/trace_processor/core/exec/sink.h b/src/trace_processor/core/exec/sink.h index 3157a19..4829eab 100644 --- a/src/trace_processor/core/exec/sink.h +++ b/src/trace_processor/core/exec/sink.h
@@ -132,6 +132,7 @@ bool GetData(RowBatch& out, OperatorState& state) const override; void Rewind(OperatorState&) const override {} const RowsInPlace* InPlace(const OperatorState& state) const override; + bool HasInPlace() const override { return true; } uint32_t id() const { return id_; } const Breaker& breaker() const { return breaker_; }
diff --git a/src/trace_processor/core/exec/sorted_scan.cc b/src/trace_processor/core/exec/sorted_scan.cc index b624b63..e9ed9da 100644 --- a/src/trace_processor/core/exec/sorted_scan.cc +++ b/src/trace_processor/core/exec/sorted_scan.cc
@@ -42,6 +42,16 @@ using filter::CastFilterValueResult; +// Out-of-line so FindAll avoids constructing CastFilterValueResult::Value on +// the common integer/SetId path. +PERFETTO_NO_INLINE Range SearchSortedString(const filter::SortedColumn& column, + RangeOp op, + const char* str, + Range range) { + return filter::SearchSorted(column, op, CastFilterValueResult::Value(str), + range); +} + } // namespace SortedSearches::SortedSearches(core::Arena& arena, @@ -98,46 +108,42 @@ const Search& s = searches_[i]; PERFETTO_DCHECK(s.param < context.params().size()); const ParamValue& cast = values[s.param]; - if (cast.value.validity == CastFilterValueResult::kNoneMatch) { + if (cast.validity == CastFilterValueResult::kNoneMatch) { range.e = range.b; break; } - if (cast.value.validity == CastFilterValueResult::kAllMatch) { + if (cast.validity == CastFilterValueResult::kAllMatch) { continue; } - const dataframe::Column& column = *s.column; switch (s.kind) { case Search::Kind::kBinarySearch: { const Sorted& sorted = sorted_[i]; if (sorted.string_eq) { // String = values are cast to an interned id. - range = filter::SearchSorted( - sorted.column, sorted.op, - CastFilterValueResult::Value(pool_->Get(cast.string_id).c_str()), - range); + range = SearchSortedString(sorted.column, sorted.op, + pool_->Get(cast.string_id).c_str(), range); } else if (sorted.column.type.Is<String>()) { // `value` points into `text`, and goes stale if the value is moved. - range = filter::SearchSorted( - sorted.column, sorted.op, - CastFilterValueResult::Value(cast.text.c_str()), range); + range = SearchSortedString(sorted.column, sorted.op, + cast.text().c_str(), range); } else { - range = filter::SearchSorted(sorted.column, sorted.op, - cast.value.value, range); + range = + filter::SearchSorted(sorted.column, sorted.op, cast.value, range); } break; } case Search::Kind::kSetIdEq: filter::SetIdSortedEqualRange( - column.storage.unchecked_data<Uint32>(), - base::unchecked_get<uint32_t>(cast.value.value), range); + static_cast<const uint32_t*>(sorted_[i].column.data), + base::unchecked_get<uint32_t>(cast.value), range); break; case Search::Kind::kSmallValueEq: { const auto& small = - column.specialized_storage + s.column->specialized_storage .unchecked_get<dataframe::SpecializedStorage::SmallValueEq>(); filter::SmallValueEqualRange( small.bit_vector, small.prefix_popcount.data(), - base::unchecked_get<uint32_t>(cast.value.value), range); + base::unchecked_get<uint32_t>(cast.value), range); break; } } @@ -148,10 +154,9 @@ Range SortedSearches::FindIdEq(const Context& context, Range rows) const { Range range = rows; const ParamValue& cast = context.params().b[searches_[0].param]; - if (PERFETTO_LIKELY(cast.value.validity == CastFilterValueResult::kValid)) { - filter::IdEqualRange(base::unchecked_get<uint32_t>(cast.value.value), - range); - } else if (cast.value.validity == CastFilterValueResult::kNoneMatch) { + if (PERFETTO_LIKELY(cast.validity == CastFilterValueResult::kValid)) { + filter::IdEqualRange(base::unchecked_get<uint32_t>(cast.value), range); + } else if (cast.validity == CastFilterValueResult::kNoneMatch) { range.e = range.b; } return range;
diff --git a/src/trace_processor/perfetto_sql/pipeline/operations/scan.cc b/src/trace_processor/perfetto_sql/pipeline/operations/scan.cc index 7ded0aa2..f96dc3e 100644 --- a/src/trace_processor/perfetto_sql/pipeline/operations/scan.cc +++ b/src/trace_processor/perfetto_sql/pipeline/operations/scan.cc
@@ -561,7 +561,7 @@ // A condition no row can match leaves nothing to read. for (const Condition& condition : conditions) { if (condition.value && - condition.value->value.validity == + condition.value->validity == core::filter::CastFilterValueResult::kNoneMatch) { plan.kind = ScanSearch::Kind::kEmpty; for (uint32_t i = 0; i < conditions.size(); ++i) {
diff --git a/src/trace_processor/perfetto_sql/pipeline/physical_plan_unittest.cc b/src/trace_processor/perfetto_sql/pipeline/physical_plan_unittest.cc index 59d962d..3ddc060 100644 --- a/src/trace_processor/perfetto_sql/pipeline/physical_plan_unittest.cc +++ b/src/trace_processor/perfetto_sql/pipeline/physical_plan_unittest.cc
@@ -809,8 +809,8 @@ StringArgs args; args.values = {"", run_pattern}; base::Span<const core::exec::ParamValue> run = params.Bind(args); - EXPECT_NE(run[0].glob, nullptr); - EXPECT_EQ(run[1].glob, nullptr); + EXPECT_NE(run[0].glob(), nullptr); + EXPECT_EQ(run[1].glob(), nullptr); std::vector<int64_t> out; for (bool row = cursor.Open(run); row; row = cursor.Next()) { out.push_back(*IntAt(cursor, physical->columns()[0].index));