Skip to content

Commit 4c47ca0

Browse files
committed
Reuses postprocessing environments in a safer way.
The previous implementation of PostProcessPath could potentially generate unbounded numbers of environments, as it could generate a new robot instance every time we clone into the same environment. Here, we keep a module-level mapping from environments to postprocessing environments and create them on demand, which guarantees that each cloned environment will have at most one postprocessing environment.
1 parent 84f7a9b commit 4c47ca0

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/prpy/base/robot.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2828
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2929
# POSSIBILITY OF SUCH DAMAGE.
30-
30+
import collections
3131
import functools, logging, openravepy, numpy
3232
from .. import bind, named_config, exceptions, util
3333
from ..clone import Clone, Cloned
@@ -42,6 +42,12 @@
4242

4343

4444
class Robot(openravepy.Robot):
45+
46+
_postprocess_envs = collections.defaultdict(openravepy.Environment)
47+
"""
48+
Mapping from robot environments to plan postprocessing environments.
49+
"""
50+
4551
def __init__(self, robot_name=None):
4652
self.actions = None
4753
self.planner = None
@@ -74,6 +80,13 @@ def __init__(self, robot_name=None):
7480
self.smoother = self.retimer
7581
self.affine_retimer = OpenRAVEAffineRetimer()
7682

83+
# Since we don't want to endlessly create postprocessing environments,
84+
# we maintain a map that uniquely associates each OpenRAVE environment
85+
# with a given postprocessing environment. This way, if we re-clone
86+
# into a previously used environment, we will not create a new one.
87+
self._postprocess_env = Robot._postprocess_envs[
88+
openravepy.RaveGetEnvironmentId(self.GetEnv())]
89+
7790
def __dir__(self):
7891
# We have to manually perform a lookup in InstanceDeduplicator because
7992
# __methods__ bypass __getattribute__.
@@ -165,6 +178,7 @@ def CloneBindings(self, parent):
165178
self.base_manipulation = openravepy.interfaces.BaseManipulation(self)
166179
self.task_manipulation = openravepy.interfaces.TaskManipulation(self)
167180

181+
168182
def AttachController(self, name, args, dof_indices, affine_dofs, simulated):
169183
"""
170184
Create and attach a controller to a subset of this robot's DOFs. If
@@ -286,11 +300,6 @@ def PostProcessPath(self, path,
286300
logger.debug('Detected "%s" tag on trajectory: Setting smooth'
287301
' = True', Tags.SMOOTH)
288302

289-
# Lazily create an environment for post-processing.
290-
# This is faster than creating a new environment for every operation.
291-
if not hasattr(self, '_postprocess_env'):
292-
self._postprocess_env = openravepy.Environment()
293-
294303
with Clone(self.GetEnv(),
295304
clone_env=self._postprocess_env) as cloned_env:
296305
cloned_robot = cloned_env.Cloned(self)

0 commit comments

Comments
 (0)