Skip to content

Commit 734c7fc

Browse files
James Molloycopybara-github
authored andcommitted
[xls] RangeQueryEngine: Add fast path for ArrayIndex
If all array indices are precise, i.e. the IntervalSet has only one value, then we don't need to scan all array indices and can simply look up our result. PiperOrigin-RevId: 808137825
1 parent 65aa30d commit 734c7fc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

xls/passes/range_query_engine.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,26 @@ absl::Status RangeQueryVisitor::HandleArrayIndex(ArrayIndex* array_index) {
674674

675675
IntervalSetTree result = EmptyIntervalSetTree(array_index->GetType());
676676

677+
// Fast path: if all of the index intervals are precise, we can just perform
678+
// a lookup.
679+
if (absl::c_all_of(index_intervals,
680+
[](const IntervalSet& is) { return is.IsPrecise(); })) {
681+
std::vector<int64_t> indexes;
682+
indexes.reserve(index_intervals.size());
683+
for (int64_t i = 0; i < index_intervals.size(); ++i) {
684+
const IntervalSet& intervals = index_intervals[i];
685+
XLS_ASSIGN_OR_RETURN(uint64_t index,
686+
intervals.GetPreciseValue()->ToUint64());
687+
int64_t bound = dimension[i] - 1;
688+
// Clamp to boundary if out of bounds.
689+
indexes.push_back(std::clamp<uint64_t>(index, 0, bound));
690+
}
691+
result.Set({}, array_interval_set_tree->Get(indexes));
692+
693+
SetIntervalSetTree(array_index, std::move(result));
694+
return absl::OkStatus();
695+
}
696+
677697
// Returns true if the given interval set covers the given index value for an
678698
// array of dimension `dim`. Includes handling of OOB conditions.
679699
auto intervals_cover_index = [&](const IntervalSet& intervals, int64_t index,

0 commit comments

Comments
 (0)