warm start and some perf

This commit is contained in:
Jan Petykiewicz 2026-03-20 16:59:36 -07:00
commit f505694523

View file

@ -30,7 +30,7 @@ class ComponentResult:
__slots__ = (
'_geometry', '_dilated_geometry', '_proxy_geometry', '_actual_geometry', '_dilated_actual_geometry',
'end_port', 'length', 'move_type', '_bounds', '_dilated_bounds',
'_total_bounds', '_total_dilated_bounds', '_t_cache', '_total_geom_list', '_offsets', '_coords_cache',
'_total_bounds', '_total_dilated_bounds', '_bounds_cached', '_total_geom_list', '_offsets', '_coords_cache',
'_base_result', '_offset', '_lazy_evaluated', 'rel_gx', 'rel_gy', 'rel_go'
)
@ -58,11 +58,11 @@ class ComponentResult:
self.end_port = end_port
self.length = length
self.move_type = move_type
self._t_cache = {}
self._base_result = _base_result
self._offset = _offset
self._lazy_evaluated = False
self._bounds_cached = False
if rel_gx is not None:
self.rel_gx = rel_gx
@ -138,6 +138,7 @@ class ComponentResult:
self._total_bounds = None
self._dilated_bounds = None
self._total_dilated_bounds = None
self._bounds_cached = True
def _ensure_evaluated(self) -> None:
if self._base_result is None or self._lazy_evaluated:
@ -145,16 +146,11 @@ class ComponentResult:
# Perform Translation
dx, dy = self._offset
# Base uses its own coords cache
base_coords = self._base_result._coords_cache
if base_coords is None:
self._lazy_evaluated = True
return
new_coords = base_coords + [dx, dy]
# Translate ALL geometries at once
new_total_arr = shapely.set_coordinates(list(self._base_result._total_geom_list), new_coords)
# Use shapely.transform which is vectorized and doesn't modify in-place.
# This is MUCH faster than cloning with copy.copy and then set_coordinates.
import shapely
new_total_arr = shapely.transform(self._base_result._total_geom_list, lambda x: x + [dx, dy])
new_total = new_total_arr.tolist()
o = self._base_result._offsets
@ -193,47 +189,53 @@ class ComponentResult:
@property
def bounds(self) -> numpy.ndarray:
if self._bounds is None:
if self._base_result is not None:
dx, dy = self._offset
self._bounds = self._base_result.bounds + [dx, dy, dx, dy]
if not self._bounds_cached:
self._ensure_bounds_evaluated()
return self._bounds
@property
def total_bounds(self) -> numpy.ndarray:
if self._total_bounds is None:
if self._base_result is not None:
dx, dy = self._offset
self._total_bounds = self._base_result.total_bounds + [dx, dy, dx, dy]
if not self._bounds_cached:
self._ensure_bounds_evaluated()
return self._total_bounds
@property
def dilated_bounds(self) -> numpy.ndarray | None:
if self._dilated_bounds is None:
if self._base_result is not None and self._base_result.dilated_bounds is not None:
dx, dy = self._offset
self._dilated_bounds = self._base_result.dilated_bounds + [dx, dy, dx, dy]
if not self._bounds_cached:
self._ensure_bounds_evaluated()
return self._dilated_bounds
@property
def total_dilated_bounds(self) -> numpy.ndarray | None:
if self._total_dilated_bounds is None:
if self._base_result is not None and self._base_result.total_dilated_bounds is not None:
dx, dy = self._offset
self._total_dilated_bounds = self._base_result.total_dilated_bounds + [dx, dy, dx, dy]
if not self._bounds_cached:
self._ensure_bounds_evaluated()
return self._total_dilated_bounds
def _ensure_bounds_evaluated(self) -> None:
if self._bounds_cached: return
base = self._base_result
if base is not None:
dx, dy = self._offset
# Vectorized addition is faster if we avoid creating new lists/arrays in the loop
if base._bounds is not None:
self._bounds = base._bounds + [dx, dy, dx, dy]
if base._total_bounds is not None:
b = base._total_bounds
self._total_bounds = numpy.array([b[0]+dx, b[1]+dy, b[2]+dx, b[3]+dy])
if base._dilated_bounds is not None:
self._dilated_bounds = base._dilated_bounds + [dx, dy, dx, dy]
if base._total_dilated_bounds is not None:
b = base._total_dilated_bounds
self._total_dilated_bounds = numpy.array([b[0]+dx, b[1]+dy, b[2]+dx, b[3]+dy])
self._bounds_cached = True
def translate(self, dx: float, dy: float, rel_gx: int | None = None, rel_gy: int | None = None, rel_go: int | None = None) -> ComponentResult:
"""
Create a new ComponentResult translated by (dx, dy).
"""
dxr, dyr = round(dx, 3), round(dy, 3)
if (dxr, dyr) == (0.0, 0.0):
return self
if (dxr, dyr) in self._t_cache:
return self._t_cache[(dxr, dyr)]
new_port = Port(self.end_port.x + dx, self.end_port.y + dy, self.end_port.orientation)
# Optimized: no internal cache (already cached in router) and no rounding
# Also skip snapping since parent and relative move are already snapped
new_port = Port(self.end_port.x + dx, self.end_port.y + dy, self.end_port.orientation, snap=False)
# LAZY TRANSLATE
if self._base_result:
@ -244,7 +246,7 @@ class ComponentResult:
base = self
new_offset = [dx, dy]
res = ComponentResult(
return ComponentResult(
end_port=new_port,
length=self.length,
move_type=self.move_type,
@ -254,9 +256,6 @@ class ComponentResult:
rel_gy=rel_gy,
rel_go=rel_go
)
self._t_cache[(dxr, dyr)] = res
return res
class Straight: