Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: caching bodies to avoid unnecessary object creation #1513

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/1513.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
caching bodies to avoid unnecessary object creation
1 change: 1 addition & 0 deletions src/ansys/geometry/core/designer/body.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,7 @@ def copy(self, parent: "Component", name: str = None) -> "Body": # noqa: D102
response.master_id, copy_name, self._grpc_client, is_surface=self.is_surface
)
parent._master_component.part.bodies.append(tb)
parent._clear_cached_bodies()
body_id = f"{parent.id}/{tb.id}" if parent.parent_component else tb.id
return Body(body_id, response.name, parent, tb)

Expand Down
18 changes: 17 additions & 1 deletion src/ansys/geometry/core/designer/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""Provides for managing components."""

from enum import Enum, unique
from functools import cached_property
from typing import TYPE_CHECKING, Any, Optional, Union
import uuid

Expand Down Expand Up @@ -239,6 +240,11 @@ def __init__(

self._master_component.occurrences.append(self)

def _clear_cached_bodies(self) -> None:
"""Clear the cached bodies."""
if "bodies" in self.__dict__:
del self.__dict__["bodies"]

@property
def id(self) -> str:
"""ID of the component."""
Expand All @@ -259,7 +265,7 @@ def components(self) -> list["Component"]:
"""List of ``Component`` objects inside of the component."""
return self._components

@property
@cached_property
def bodies(self) -> list[Body]:
"""List of ``Body`` objects inside of the component."""
bodies = []
Expand Down Expand Up @@ -509,6 +515,7 @@ def extrude_sketch(
response = self._bodies_stub.CreateExtrudedBody(request)
tb = MasterBody(response.master_id, name, self._grpc_client, is_surface=False)
self._master_component.part.bodies.append(tb)
self._clear_cached_bodies()
return Body(response.id, response.name, self, tb)

@min_backend_version(24, 2, 0)
Expand Down Expand Up @@ -558,6 +565,7 @@ def sweep_sketch(
response = self._bodies_stub.CreateSweepingProfile(request)
tb = MasterBody(response.master_id, name, self._grpc_client, is_surface=False)
self._master_component.part.bodies.append(tb)
self._clear_cached_bodies()
return Body(response.id, response.name, self, tb)

@min_backend_version(24, 2, 0)
Expand Down Expand Up @@ -605,6 +613,7 @@ def sweep_chain(
response = self._bodies_stub.CreateSweepingChain(request)
tb = MasterBody(response.master_id, name, self._grpc_client, is_surface=True)
self._master_component.part.bodies.append(tb)
self._clear_cached_bodies()
return Body(response.id, response.name, self, tb)

@min_backend_version(24, 2, 0)
Expand Down Expand Up @@ -720,6 +729,7 @@ def extrude_face(

tb = MasterBody(response.master_id, name, self._grpc_client, is_surface=False)
self._master_component.part.bodies.append(tb)
self._clear_cached_bodies()
return Body(response.id, response.name, self, tb)

@protect_grpc
Expand Down Expand Up @@ -753,6 +763,7 @@ def create_sphere(self, name: str, center: Point3D, radius: Distance) -> Body:
response = self._bodies_stub.CreateSphereBody(request)
tb = MasterBody(response.master_id, name, self._grpc_client, is_surface=False)
self._master_component.part.bodies.append(tb)
self._clear_cached_bodies()
return Body(response.id, response.name, self, tb)

@protect_grpc
Expand Down Expand Up @@ -819,6 +830,7 @@ def create_body_from_loft_profile(
response = self._bodies_stub.CreateExtrudedBodyFromLoftProfiles(request)
tb = MasterBody(response.master_id, name, self._grpc_client, is_surface=False)
self._master_component.part.bodies.append(tb)
self._clear_cached_bodies()
return Body(response.id, response.name, self, tb)

@protect_grpc
Expand Down Expand Up @@ -856,6 +868,7 @@ def create_surface(self, name: str, sketch: Sketch) -> Body:

tb = MasterBody(response.master_id, name, self._grpc_client, is_surface=True)
self._master_component.part.bodies.append(tb)
self._clear_cached_bodies()
return Body(response.id, response.name, self, tb)

@protect_grpc
Expand Down Expand Up @@ -896,6 +909,7 @@ def create_surface_from_face(self, name: str, face: Face) -> Body:

tb = MasterBody(response.master_id, name, self._grpc_client, is_surface=True)
self._master_component.part.bodies.append(tb)
self._clear_cached_bodies()
return Body(response.id, response.name, self, tb)

@protect_grpc
Expand Down Expand Up @@ -936,6 +950,7 @@ def create_body_from_surface(self, name: str, trimmed_surface: TrimmedSurface) -

tb = MasterBody(response.master_id, name, self._grpc_client, is_surface=response.is_surface)
self._master_component.part.bodies.append(tb)
self._clear_cached_bodies()
return Body(response.id, response.name, self, tb)

@check_input_types
Expand Down Expand Up @@ -1136,6 +1151,7 @@ def delete_body(self, body: Body | str) -> None:
# on the client side
body_requested._is_alive = False
self._grpc_client.log.debug(f"Body {body_requested.id} has been deleted.")
self._clear_cached_bodies()
else:
self._grpc_client.log.warning(
f"Body {id} is not found in this component (or subcomponents)."
Expand Down
46 changes: 46 additions & 0 deletions tests/integration/test_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -2738,3 +2738,49 @@ def test_surface_body_creation(modeler: Modeler):
assert len(design.bodies) == 6
assert not body.is_surface
assert body.faces[0].area.m == pytest.approx(39.4784176044 * 2)


def test_cached_bodies(modeler: Modeler):
"""Test verifying that bodies are cached correctly.

Whenever a new body is created, modified etc. we should make sure that the cache is updated.
"""
design = modeler.create_design("ModelingDemo")

# Define a sketch
origin = Point3D([0, 0, 10])
plane = Plane(origin, direction_x=[1, 0, 0], direction_y=[0, 1, 0])

# Create a sketch
sketch_box = Sketch(plane)
sketch_box.box(Point2D([20, 20]), 30 * UNITS.m, 30 * UNITS.m)

sketch_cylinder = Sketch(plane)
sketch_cylinder.circle(Point2D([20, 20]), 5 * UNITS.m)

design.extrude_sketch(name="BoxBody", sketch=sketch_box, distance=Distance(30, unit=UNITS.m))
design.extrude_sketch(
name="CylinderBody",
sketch=sketch_cylinder,
distance=Distance(60, unit=UNITS.m),
)

my_bodies = design.bodies
my_bodies_2 = design.bodies

# We should make sure that the object memory addresses are the same
for body1, body2 in zip(my_bodies, my_bodies_2):
assert body1 is body2 # We are comparing the memory addresses
RobPasMue marked this conversation as resolved.
Show resolved Hide resolved
assert id(body1) == id(body2)

design.extrude_sketch(
name="CylinderBody2",
sketch=sketch_cylinder,
distance=Distance(20, unit=UNITS.m),
direction="-",
)
my_bodies_3 = design.bodies

for body1, body3 in zip(my_bodies, my_bodies_3):
assert body1 is not body3
assert id(body1) != id(body3)
Loading