Skip to content

Accelerate neck cutoff detection with batched KDTree queries - #49

Merged
sergioald merged 2 commits into
mainfrom
perf/geometry-migration-hotpath
Sep 27, 2026
Merged

sergioald merged 2 commits into
mainfrom
perf/geometry-migration-hotpath

Conversation

@sergioald

Copy link
Copy Markdown
Owner

Summary

This PR accelerates neck-cutoff detection in geometry4 by replacing repeated scalar cKDTree.query_ball_point() calls with a bounded batched search.

The new implementation preserves the existing detector contract exactly:

  • left-to-right first valid i
  • eligible j domain
  • strict < dslim3 cutoff test
  • closest valid j
  • smallest-j tie break
  • original fast-path behavior
  • refinement fallback
  • refined-to-original index mapping
  • original-coordinate revalidation

The scalar implementation is retained as a private reference for validation.

The validated production batch size is:

32

Replace repeated scalar KDTree radius queries with a bounded batched search while preserving exact first-hit, threshold, closest-candidate, tie-break, and refinement semantics. Add detailed geometry timing, detector benchmarks, randomized differential validation, and exact 5,000-step solver parity evidence.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fc7fd850bf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ldsfl/geometry.py
Comment on lines +143 to +145
elapsed = perf_counter() - t_neck0
timing["neck"] = float(timing.get("neck", 0.0) + elapsed)
timing["geometry_neck"] = float(timing.get("geometry_neck", 0.0) + elapsed)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Time only the detector in the detector benchmark

When a cutoff is found, this elapsed interval also covers plot_cut, save_xy_cut, point-mask construction, array slicing, and subsequent searches. neck_detector_solver_ab._timing_summary nevertheless exposes geometry_neck as neck_detector_seconds and uses it to calculate the detector speedup, so runs containing cutoffs can report mostly output and cutoff-processing time rather than the scalar-versus-batched query cost. Use a separate timer around find_neck_cutoff_kdtree_with_refine for that comparison, or label this metric as the full cutoff phase.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Addressed. geometry_neck remains the full cutoff-processing phase for compatibility, and a new geometry_neck_detector subset now measures only calls to find_neck_cutoff_kdtree_with_refine. The detector subset is excluded from additive geometry accounting.

I reran the exact scalar/batched A/B validation. At 5,000 steps both runs completed with identical state histories, 44 identical cutoff events, identical selected pairs, and identical points removed.

Updated paired timings:

detector only: 106.610 s → 18.785 s (5.675×)
full cutoff phase: 106.796 s → 18.946 s (5.637×)
geometry: 127.566 s → 37.100 s (3.438×)
whole solver: 161.731 s → 66.780 s (2.422×)

Full suite: 391 passed, 1 skipped.

Separate detector-only timing from the full cutoff-processing phase, preserve existing geometry accounting semantics, and report validated detector and cutoff-phase speedups independently.
@sergioald
sergioald merged commit 583aaf1 into main Sep 27, 2026
4 checks passed
@sergioald
sergioald deleted the perf/geometry-migration-hotpath branch September 27, 2026 19:48
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.

1 participant