Cache mesh bounding boxes correctly#5180
Conversation
connorjward
left a comment
There was a problem hiding this comment.
The really nice way to do this is:
def cached_property_until(cond: Callable[[Self], bool]):
def decorator(func):
def wrapper(self):
if cond(self):
return self._cache[some key]
else:
return self._cache.setdefault(some key, func(self))
cached_property_until_coords_change = cached_property_until(lambda self: self._saved_coords_dat_version == self.coordinates.dat.dat_version)so you can have
@cached_property_until_coords_change
def bounding_box_coords(self):
...|
@achanbour this is very relevant to your work, and you have already probably done something similar. Any comments? |
2fe3ae2 to
534b1fd
Compare
|
I currently have an attribute that keeps track of the mesh's topology In both regimes (coordinates change OR topology changes), the |
| Use this if you move the mesh (for example by reassigning to | ||
| the coordinate field).""" | ||
| self._rtree = None | ||
| self._rtree_cache = None |
There was a problem hiding this comment.
cached_property_until stores the rtree in self._rtree_cache. That method clear_rtree is public API (I know thetis are using it). Maybe it can be deprecated now, I'm not sure.
There was a problem hiding this comment.
Without context this line makes no sense. I would prefer to hide it as much as possible. Why not:
def clear_rtree(self):
# comment about why this does the right thing
self._rtree_cache = NoneWe should definitely look to deprecate clear_rtree because we can manage it ourselves.
After changing mesh coordinates, accessing
mesh.bounding_box_coordswill now recalculate the bounding boxes.Added a
mesh._check_coordinate_dat_versionhelper because this sort of thing happens in a few places (bounding boxes, rtree, soon to be distributed rtree)