Skip to content

Faster Manhattan indexing for ProductSector - #115

Merged
lkdvos merged 9 commits into
mainfrom
isless
Oct 19, 2026
Merged

lkdvos merged 9 commits into
mainfrom
isless

Conversation

@lkdvos

@lkdvos lkdvos commented Oct 15, 2026

Copy link
Copy Markdown
Member

Speeds up findindex, getindex, iteration and isless for values(::ProductSector) (and for SU(N) irreps in SUNRepresentations.jl, which use the same helpers).

  • to_manhattan_index / manhattan_to_multidimensional_index now count points in closed form (inclusion-exclusion) instead of recursively enumerating them, so the cost no longer grows with the Manhattan distance.
  • Iterating values(::ProductSector) keeps the multi-index in the iterator state and advances it with next_manhattan_index, instead of calling getindex at every step.
before after
findindex, U1 ⊠ U1 ⊠ SU2, charge 50 2.3 ms 18 ns
getindex, same 2.2 ms 260 ns
isless, U1 ⊠ SU4 4.3 μs 36 ns
iterate first 1000 values, U1 ⊠ U1 ⊠ SU2 489 μs 12 μs

Small-index getindex on finite products is slightly slower (e.g. 59 → 87 ns for Z3 ⊠ Ising ⊠ Fib).

🤖 Generated with Claude Code

lkdvos and others added 8 commits October 15, 2026 10:21
Replace the recursive enumeration in `to_manhattan_index` and
`manhattan_to_multidimensional_index` by an inclusion-exclusion count of the
points below a given Manhattan distance, together with a binary search for the
inverse mapping. This makes both mappings O(N 2^N) instead of growing
polynomially with the distance, which speeds up `findindex` and `getindex` for
`values(::ProductSector)`, and with it `isless` for sectors such as SU(N)
irreps whose `findindex` relies on these functions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Iterating `values(::ProductSector)` previously called `getindex` at every step,
recomputing the multidimensional index from scratch. Instead keep the current
index in the iterator state and advance it to its Manhattan-order successor
with `next_manhattan_index`, which is O(N) per step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Benchmarks show that only `num_manhattan_points_below`, both `localoffset`
methods and the recursive `invertlocaloffset` method benefit from the
annotation; the recursive base case of `localoffset` is not inlined otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/auxiliary.jl 94.94% <100.00%> (+1.52%) ⬆️
src/product.jl 94.49% <100.00%> (+1.03%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@borisdevos borisdevos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had also looked into stars and bars with my previous attempt but rejected that since it was (as you noted) not better for the finite product sector types I was then interested in. I'm surprised it's this much better for the infinite stuff. Looks definitely good to me!

@lkdvos
lkdvos merged commit 6066a15 into main Oct 19, 2026
12 checks passed
@lkdvos
lkdvos deleted the isless branch October 19, 2026 11:59
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.

3 participants