From 0d1038bcd4d4aa253a03068667b812b108fac1f0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 25 May 2026 18:34:59 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20redundant=20type=20casti?= =?UTF-8?q?ng=20in=20rasterizer=20loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactored `rasterize` to combine validation and processing into a single loop. Ensured `class_id` is cast to an integer only once per geometry. This optimization reduces redundant iterations and type casting, and improves support for single-use iterators. Co-authored-by: tahamukhtar20 <91777330+tahamukhtar20@users.noreply.github.com> --- src/mapcv/rasterizer.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mapcv/rasterizer.py b/src/mapcv/rasterizer.py index cbe95c1..c61c05e 100644 --- a/src/mapcv/rasterizer.py +++ b/src/mapcv/rasterizer.py @@ -47,13 +47,13 @@ def rasterize( Background pixels are 0; polygons in later list positions overwrite earlier ones. Non-polygon geometries are silently skipped. """ - if any(int(cid) <= 0 or int(cid) > 255 for _, cid in geometries): - raise ValueError("class_id must be in 1..=255 (0 is reserved for background)") - height, width = out_shape polygons: List[Tuple[RingSet, int]] = [] for geom, cid in geometries: - polygons.extend(_flatten(geom, int(cid))) + cid_int = int(cid) + if cid_int <= 0 or cid_int > 255: + raise ValueError("class_id must be in 1..=255 (0 is reserved for background)") + polygons.extend(_flatten(geom, cid_int)) return cast( npt.NDArray[np.uint8],