Skip to content

Add batch line-of-sight queries to VisibilityChecker - #531

Merged
pnxenopoulos merged 4 commits into
pnxenopoulos:mainfrom
0wGrant:batch-visibility-queries
Oct 27, 2026
Merged

pnxenopoulos merged 4 commits into
pnxenopoulos:mainfrom
0wGrant:batch-visibility-queries

Conversation

@0wGrant

@0wGrant 0wGrant commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Adds VisibilityChecker.is_visible_many(a, b, *, threads=None) and is_occluded_many. They check many segments per call and return a Boolean polars.Series; each value equals is_visible(a_i, b_i).

Per-call overhead makes is_visible slow for workloads with millions of rays, such as per-tick visible areas (about 12M calls per round in my case).

Changes

  • awpy: the BVH traversal moves into a private occluded_with with a reusable stack, shared by is_occluded (behaviour unchanged) and the new VisibilityMesh::occluded_into(a, b, out). No threads, no new dependencies.
  • Python: is_visible_many / is_occluded_many take float32 or float64 buffers (NumPy not required) or sequences of points; a is one shared start or one per segment. Work runs on a rayon pool with the GIL released; threads=1 stays on the calling thread.
  • rayon = "1.12" is added to awpy-python only; it is already in Cargo.lock through polars.
  • Stubs, docstrings, Rust and Python tests.

Validation

Bit for bit equal to an is_visible loop on de_anubis, de_inferno, de_nuke and de_mirage: 11M segments per map, including tangential cases (ends on triangles, segments in a triangle's plane, vertices, zero length), across float32 / float64, thread counts, strided and list inputs. The scalar is_visible is unchanged.

Performance

Recorded batches of one real round (8,347 calls, 12.0M segments); i5-10400, 6 cores / 12 threads, Windows.

time
is_visible loop 14.15 s 1×
is_visible_many, threads=1 10.01 s 1.4×
is_visible_many, default 1.76 s 8.0×

Spawning threads per call (std::thread::scope) instead of a pool was about 25% slower here (6.7×).

Notes

  • threads=None uses every core; pass threads=1 inside a process pool that already uses every core.
  • polars has its own rayon pool; these queries use rayon's global pool.

@pnxenopoulos pnxenopoulos left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for identifying a great area to improve perf!

Added one small request r.e. error propagation on pool creation.

Once we get that in, we can release 3.2!

Comment thread crates/awpy-python/src/lib.rs Outdated
.num_threads(threads)
.thread_name(|i| format!("awpy-visibility-{i}"))
.build()
.ok()?;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I believe this discards a pool-creation error (the .build().ok()? part)

So if one does

checker.is_visible_many(starts, ends, threads=2)

and then say the OS cannot create the 2 thread pool, then the error becomes None which affects None => job() (ends up using Rayon's global pool)

I think the way to fix this is to simply propagate the pool-creation error. Maybe something like

let pool = rayon::ThreadPoolBuilder::new()
    .num_threads(threads)
    .thread_name(|i| format!("awpy-visibility-{i}"))
    .build()
    .map_err(|error| {
        PyRuntimeError::new_err(format!(
            "cannot create a visibility pool with {threads} threads: {error}"
        ))
    })?;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Fixed in 4e5c7ac: the pool is now looked up before the GIL is released, and a failed build raises RuntimeError (listed under Raises) instead of falling back to the global pool.

@0wGrant
0wGrant requested a review from pnxenopoulos October 26, 2026 22:29
pnxenopoulos
pnxenopoulos previously approved these changes Oct 26, 2026

@pnxenopoulos pnxenopoulos left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

These changes look good to me!

If you don't mind, under crates/awpy-python/docs/changelog.md can you start at the top a new header 3.2.0 and then add a line for your change?

@0wGrant

0wGrant commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Done in a649198.

@0wGrant
0wGrant requested a review from pnxenopoulos October 27, 2026 10:01
@pnxenopoulos
pnxenopoulos merged commit cebc476 into pnxenopoulos:main Oct 27, 2026
16 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.

2 participants