From 7adf4adbdee42c4b6a823ba5851c34cb54345346 Mon Sep 17 00:00:00 2001 From: Corvince Date: Thu, 3 Aug 2023 21:14:19 +0200 Subject: [PATCH] perf: Access grid only once --- mesa/space.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mesa/space.py b/mesa/space.py index e5615d65665..eebcb3c1758 100644 --- a/mesa/space.py +++ b/mesa/space.py @@ -394,8 +394,9 @@ def iter_cell_list_contents( """ # iter_cell_list_contents returns only non-empty contents. return ( - self._grid[x][y] - for x, y in itertools.filterfalse(self.is_cell_empty, cell_list) + cell + for x, y in cell_list + if (cell := self._grid[x][y]) != self.default_val() ) @accept_tuple_argument @@ -568,8 +569,9 @@ def iter_cell_list_contents( An iterator of the agents contained in the cells identified in `cell_list`. """ return itertools.chain.from_iterable( - self._grid[x][y] - for x, y in itertools.filterfalse(self.is_cell_empty, cell_list) + cell + for x, y in cell_list + if (cell := self._grid[x][y]) != self.default_val() )