Skip to content

Commit

Permalink
Refactor: Replace all with iterall where beneficial (#6130)
Browse files Browse the repository at this point in the history
Whenever a `QueryBuilder` result is used in a loop and the total result
is not fully stored in memory in some way, it is beneficial to use
`iterall` since that prevents loading everything in memory for no
reason.
  • Loading branch information
sphuber authored Sep 22, 2023
1 parent 1b5b669 commit 8a2fece
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions aiida/cmdline/commands/cmd_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ def group_list(
builder.append(orm.Node, filters={'id': {'==': node.pk}}, with_group='group')

builder.order_by({orm.Group: {order_by: order_dir}})
result = builder.all()

projection_lambdas = {
'pk': lambda group: str(group.pk),
Expand All @@ -394,7 +393,7 @@ def group_list(
projection_header.append('Node count')
projection_fields.append('count')

for group in result:
for group in builder.iterall():
table.append([projection_lambdas[field](group[0]) for field in projection_fields])

if not all_entries:
Expand Down
4 changes: 2 additions & 2 deletions aiida/cmdline/commands/cmd_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@ def rehash(nodes, entry_point, force):
else:
builder = QueryBuilder()
builder.append(classes, tag='node')
to_hash = builder.all()
to_hash = builder.iterall()
num_nodes = builder.count()

if not to_hash:
if not num_nodes:
echo.echo_critical('no matching nodes found')

with click.progressbar(to_hash, label='Rehashing Nodes:') as iter_hash:
Expand Down
4 changes: 2 additions & 2 deletions aiida/orm/nodes/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def _iter_all_same_nodes(self, allow_before_store=False) -> t.Iterator['Node']:
builder.append(self._node.__class__, filters={f'extras.{self._HASH_EXTRA_KEY}': node_hash}, subclassing=False)

return (
node for node in builder.all(flat=True) if node.base.caching.is_valid_cache
) # type: ignore[misc,union-attr]
node for node, in builder.iterall() if node.base.caching.is_valid_cache # type: ignore[misc,union-attr]
)

@property
def is_valid_cache(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion aiida/orm/utils/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def get_calcjob_remote_paths( # pylint: disable=too-many-locals

path_mapping = {}

for remote_data, computer_uuid in query.all():
for remote_data, computer_uuid in query.iterall():
path_mapping.setdefault(computer_uuid, []).append(remote_data)

return path_mapping

0 comments on commit 8a2fece

Please sign in to comment.