Skip to content

feat(acceleration): BackendRegistry O(n²) → O(k) backend selection index - #4620

Merged
makr-code merged 2 commits into
developfrom
copilot/implement-backend-selection-index
Apr 13, 2026
Merged

makr-code merged 2 commits into
developfrom
copilot/implement-backend-selection-index

Conversation

Copilot AI commented Apr 13, 2026 •

Copy link
Copy Markdown
Contributor

Eliminates the O(|kFallbackOrder| × |backends|) nested loop with per-iteration dynamic_cast calls in backend selection. Every typed selector (selectVectorBackendFor, getBestGraphBackend, etc.) previously scanned all registered backends (~15) for each of 13 fallback priority levels and cast on each match. With ~15 backends this is negligible today, but the pattern scales poorly and the dynamic_cast cost accumulates on every query.

Changes

include/acceleration/compute_backend.h

  • Add RegisteredBackend struct: base, vectorPtr, graphPtr, geoPtr, matrixPtr — raw pointers into heap-owned backends, set once at registration
  • Add std::unordered_map<BackendType, RegisteredBackend> typeIndex_ to BackendRegistry private section

src/acceleration/backend_registry.cpp

  • registerBackend(): one dynamic_cast per interface per registration (first-wins per type/interface) to populate typeIndex_
  • selectTyped<T>(): replaces nested loop with O(|kFallbackOrder|) hash-map lookup + stored typed pointer; zero hot-path dynamic_cast. Added getTypedPtr<T> specializations for all 5 interface types
  • getBackend(type): O(n) → O(1) via typeIndex_.find()
  • getBest*Backend(): iterate kFallbackOrder, index lookup, return stored typed pointer — no dynamic_cast
  • selectBackendFor(): retains original linear scan; generic IComputeBackend* semantics require seeing all per-type registrations (e.g. CPUGraphBackend alongside CPUVectorBackend under BackendType::CPU)
  • shutdownAll(): clears typeIndex_ alongside backends_
// registerBackend() — one cast per interface, once:
RegisteredBackend& rb = typeIndex_[bt];
if (!rb.vectorPtr) rb.vectorPtr = dynamic_cast<IVectorBackend*>(backend.get());
// ...

// selectTyped<T>() — O(k) lookups, zero casts:
for (auto type : kFallbackOrder) {
    auto it = index.find(type);
    if (it == index.end()) continue;
    T* typed = getTypedPtr<T>(it->second);   // stored pointer, no cast
    if (typed && satisfies(typed->getCapabilities(), reqs)) return typed;
}

Multiple CPU backends sharing BackendType::CPU are handled correctly: typeIndex_[CPU].vectorPtr → CPUVectorBackend*, typeIndex_[CPU].graphPtr → CPUGraphBackend*, etc.

tests/test_backend_selection_matrix.cpp

  • 7 new TypeIndex_* tests verifying correct typed-pointer aggregation for all four CPU backends sharing BackendType::CPU, distinct per-interface returns, and capability declarations

Documentation

  • FUTURE_ENHANCEMENTS.md / future_enhancements.md: all 3 implementation notes marked [x]
  • ROADMAP.md: Phase 5 entry added

Linked Issues

Closes #236

Type of Change

  • Bug fix (non-breaking)
  • New feature (non-breaking)
  • Refactoring (non-breaking)
  • Documentation
  • Breaking change (requires MAJOR version bump — see VERSIONING.md)
  • Security fix
  • Other:

Breaking Change Checklist

  • MAJOR version bump planned in VERSION and CMakeLists.txt
  • Migration guide added in docs/migration/
  • Announcement prepared for GitHub Discussions (≥ 2 weeks before release)
  • CHANGELOG ### Removed / ### Changed section updated

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing performed
  • Benchmarks run (if performance-sensitive change)

📚 Research & Knowledge (wenn applicable)

  • Diese PR basiert auf wissenschaftlichen Paper(s) oder Best Practices?
    • Falls JA: Research-Dateien in /docs/research/ angelegt?
    • Falls JA: Im Modul-README unter "Wissenschaftliche Grundlagen" verlinkt?
    • Falls JA: In /docs/research/implementation_influence/ eingetragen?

Relevante Quellen:

  • Paper:
  • Best Practice:
  • Architecture Decision:

Checklist

  • Code follows project style guidelines (clang-format / clang-tidy)
  • Self-review completed
  • Documentation updated (if needed)
  • CHANGELOG.md updated under [Unreleased]
  • No new warnings introduced
  • Security-sensitive paths reviewed by security maintainer (if applicable)

Copilot AI linked an issue Apr 13, 2026 that may be closed by this pull request
3 tasks
…imization (Issue #236)

- Add RegisteredBackend struct to compute_backend.h with pre-cast typed
  interface pointers (base, vectorPtr, graphPtr, geoPtr, matrixPtr)
- Add typeIndex_ (unordered_map<BackendType, RegisteredBackend>) to BackendRegistry
- registerBackend(): populate typeIndex_ with one dynamic_cast per interface
  at registration time — never in the hot query path
- selectTyped<T>(): now O(|kFallbackOrder|) using map lookup instead of
  O(|kFallbackOrder| × |backends|) nested scan with dynamic_cast per iteration
- getBackend(): O(1) hash-map lookup via typeIndex_
- getBestVectorBackend/GraphBackend/GeoBackend/MatrixBackend(): use typed
  pointers from typeIndex_ directly — zero dynamic_cast calls
- selectBackendFor(): keeps original linear scan (backward compatible for
  multi-backend-per-type correctness with generic IComputeBackend* return)
- shutdownAll(): clears typeIndex_ alongside backends_
- Add 7 new type-index correctness tests to test_backend_selection_matrix.cpp
- Mark all 3 implementation notes as [x] in FUTURE_ENHANCEMENTS.md files
- Add Phase 5 ROADMAP entry for the optimization

Agent-Logs-Url: https://github.com/makr-code/ThemisDB/sessions/e6e8c47e-38ac-4f08-bc8c-a6fa076dfda9

Co-authored-by: makr-code <[email protected]>
Copilot AI changed the title [WIP] Implement BackendRegistry: O(n²) Backend Selection Index feat(acceleration): BackendRegistry O(n²) → O(k) backend selection index Apr 13, 2026
Copilot AI requested a review from makr-code April 13, 2026 12:25
@makr-code
makr-code marked this pull request as ready for review April 13, 2026 13:35
@makr-code
makr-code merged commit b5921c3 into develop Apr 13, 2026
11 checks passed
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.

BackendRegistry: O(n²) Backend Selection Index 05-implement-flash-attention-kernels.md

2 participants