Skip to content

BracketGallop: return the hint after two comparisons when it is the answer - #119

Merged
ChrisRackauckas merged 2 commits into
SciML:mainfrom
SebastianM-C:bracket-gallop-hit-fastpath
Oct 1, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:mainfrom
SebastianM-C:bracket-gallop-hit-fastpath

Conversation

@SebastianM-C

@SebastianM-C SebastianM-C commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

What

The hinted BracketGallop kernels (_kernel_last_bracket_gallop / _kernel_first_bracket_gallop) are now a single pass:

  1. Hit path. The two comparisons at the hint decide the answer when v[hint] ≤ x < v[hint + 1] (searchsorted_last) or v[hint - 1] < x ≤ v[hint] (searchsorted_first), and the hint is returned. Previously bracketstrictlymonotonic found that width-one bracket after the same two comparisons and then still ran the bounded bisection over it.
  2. Miss path. The gallop continues from the side those comparisons established, doubling its step until it brackets the answer, and a bisection that knows one end of the bracket is a valid candidate finishes. Previously the bracket expansion re-read the hint's neighbours and handed the bracket to Base's bisection, which widens it by one on each side and knows nothing about either end.

Duplicates are handled by the predicates: searchsorted_last asks for the last v[i] ≤ x, so an equal right neighbour is not a hit and the gallop moves on; likewise an earlier duplicate for searchsorted_first. Out-of-range hints fall back to the plain bisection as before. Results are unchanged. bracketstrictlymonotonic and its _first twin stay defined (the QA suite checks them) but the kernels no longer call them; happy to delete them if preferred.

This is the pattern of a warm-started interval lookup (an interpolation evaluated at slowly moving times, each answer fed back as the next hint): the hint is the answer most of the time and, when it is not, the answer is a few intervals away.

Also in this PR:

  • a randomized test set (hints in and out of range, duplicates, both ends, reverse ordering, Int32 hint);
  • a warm_walk cell in benchmark/benchmarks.jl (hint carried from the previous answer);
  • a docstring note on the hit path and on the cost of the default isless ordering for float vectors (order = Base.Order.Lt(<) skips the NaN and signed-zero handling when the vector has no NaN).

Measurements

Correlated query walk over a 1e5-knot sorted Float64 vector, 83 % of queries land in the hint's interval. "Hand-written" is a minimal warm-started interval search (gallop from the hint, closed-bracket bisection, plain <) used as the reference; the FFF result is clamped to the interval range so the two are bit-identical (checked on 90 000 randomized cases and on every query of the walks below).

CPU, ns per query (single thread):

host v3.4.0 this PR this PR, Lt(<) hand-written
desktop (Zen 4) 6.2 3.6 2.4 3.1
workstation (Zen 5) 5.1 2.9 2.1 3.1
server (demeter-class Xeon) — 5.0 3.2 4.4

GPU, ms per launch of 67 M queries in a KernelAbstractions kernel (each thread walks its own sequence, hint carried in a register), minimum of 15 launches:

GPU v3.4.0 this PR this PR, Lt(<) hand-written
RTX 4080 SUPER 4.03 1.89 1.98 2.18
Radeon Pro W7900 3.66 2.06 2.02 2.09
A100 PCIe 5.22 2.65 2.28 3.19
H100 PCIe 1.51 1.10 0.82 1.11

Cold (no hint) and out-of-range paths are unchanged. An intermediate version with only the hit path (first commit) regressed on the H100 with the default ordering; the fused miss path (second commit) removed that.

Tests

GROUP=Core (251 683 tests) and GROUP=QA pass locally.

🤖 Generated with Claude Code · model: claude-fable-5-1
Review: skimmed

SebastianM-C and others added 2 commits September 30, 2026 03:23
…nswer

A warm-started caller (an interpolation interval lookup feeding each answer
back as the next hint) hits its hint most of the time, and the two
comparisons `v[hint] ≤ x < v[hint + 1]` already decide the result. The
hinted bracket-gallop kernels found that width-one bracket and then still
ran the bounded bisection over it, two more comparisons and loads per query.
Both polarities now return early on a hit; an equal neighbour fails the
second test so duplicates still go through the bisection.

On a 1e5-knot vector with a correlated query walk (83 % hint hits) the
hinted search goes from 6.3 to 4.7 ns per query on the CPU, and 3.3 ns with
`order = Base.Order.Lt(<)`, against 3.1 ns for a hand-written interval
search; in a KernelAbstractions kernel on an RTX 4080 SUPER the launch goes
from 4.0 to 2.7 ms against 2.2 ms. Cold and miss paths are unchanged.

Adds a randomized test set over hints in and out of range, duplicates and
reverse ordering, a `warm_walk` benchmark cell, and a docstring note on the
hit path and the `isless` cost.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The hit check and the bracket expansion were two stages: on a miss the
expansion re-read the hint's neighbours and then handed a bracket to Base's
bounded bisection, which widens it by one on each side and knows nothing
about either end. The hinted kernels are now one pass: the two comparisons
at the hint decide a hit, and otherwise the gallop starts from the side
they established, doubling its step until it brackets the answer; a
bisection that knows one end of the bracket is a valid candidate finishes.
`bracketstrictlymonotonic` and its `_first` twin stay defined (the QA suite
checks them) but the kernels no longer call them.

Same correlated walk as the previous commit, RTX 4080 SUPER: 2.7 → 1.9 ms
per launch with the default ordering, now ahead of the hand-written interval
search (2.2 ms); CPU 4.7 → 3.6 ns per query, 2.4 ns with
`order = Base.Order.Lt(<)`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@ChrisRackauckas
ChrisRackauckas merged commit d0bb23e into SciML:main Oct 1, 2026
25 of 26 checks passed
@SebastianM-C
SebastianM-C deleted the bracket-gallop-hit-fastpath branch October 1, 2026 11:43
ChrisRackauckas referenced this pull request Oct 13, 2026
Agent-Harness: Devin CLI 3000.11.3
Agent-Model: Fusion (Claude Opus 5.5 Medium + SWE-2 Medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/flash-hour.json

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants