Skip to content

Commit

Permalink
perf: Access grid only once
Browse files Browse the repository at this point in the history
  • Loading branch information
Corvince committed Aug 3, 2023
1 parent 2cec830 commit 7adf4ad
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
)


Expand Down

0 comments on commit 7adf4ad

Please sign in to comment.