Hoist the crossed offset test in fast.cpp

Test end.offset <= 1 before revTruncLeOneLow in the reversed crossed
dispatch, and specialize revTruncLeOneLow under that path fact.  The
helper drops the now-tautological end.offset conjuncts and the key
comparison implied by a failed optLe.

The crossed truncated reverse benchmark drops from about 67 ns to
about 61 ns.  Sweep, corpus, SMT cases, and 100% line and branch
coverage still pass.
This commit is contained in:
andrew committed 2026-11-18 14:14:44 -05:00
1 parent e1395f20ef
commit e351a0cf8d
2 files changed
+19 -20

No files matched your search

+16 -18
View File
@@ -749,22 +749,17 @@ struct Impl {
return c.r->begin.offset == 0;
}
bool negCrossTruncRevUnder(const Ctx& c, const Obs& o) const {
return optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1;
}
bool negCrossTruncRevCrossedLow(const Ctx& c, const Obs& o) const {
return optLt(c.r->end_.key, c.r->begin.key) &&
c.r->end_.offset <= 1 &&
niLe(niAdd(niAdd(cardEB(c),
NatInf::of(static_cast<std::uint64_t>(c.n))),
NatInf::of(toNat(1 - c.r->end_.offset))),
NatInf::of(bSlack(c) + 1));
}
// `revTruncLeOneLow` is reached only under `end.offset <= 1` (the dispatch
// tests that comparison first), so the `end.offset <= 1` conjunct in each
// original disjunct is discharged by the caller and has been dropped here.
bool revTruncLeOneLow(const Ctx& c, const Obs& o) const {
return negCrossTruncRevUnder(c, o) ||
negCrossTruncRevCrossedLow(c, o);
if (optLe(c.r->begin.key, c.r->end_.key)) return true;
// The failed `optLe` test already gives `end.key < begin.key`, so the
// original `optLt` conjunct is implied here.
return niLe(niAdd(niAdd(cardEB(c),
NatInf::of(static_cast<std::uint64_t>(c.n))),
NatInf::of(toNat(1 - c.r->end_.offset))),
NatInf::of(bSlack(c) + 1));
}
bool negCrossTruncRevBelowEnd(const Ctx& c, const Obs& o) const {
@@ -896,10 +891,13 @@ struct Impl {
if (c.r->reverse) {
if (zeroCrossTruncRevCrossed(c, o))
return closedFormZeroCrossTruncRevCrossed(c, o);
if (revTruncLeOneLow(c, o))
return mk(OptKey{o.lastK()}, c.r->end_.key);
if (c.r->end_.offset <= 1)
// `revTruncLeOneLow` implies `end.offset <= 1`, so test the cheap
// comparison first and keep the disjunction on that branch only.
if (c.r->end_.offset <= 1) {
if (revTruncLeOneLow(c, o))
return mk(OptKey{o.lastK()}, c.r->end_.key);
return mk(OptKey{o.lastK()}, c.r->begin.key);
}
if (revTruncGeTwoTop(c, o))
return mk(OptKey{o.lastK()}, topKey());
if (revTruncGeTwoLarge(c, o))