Skip to content

Commit

Permalink
Avoid potentially expensive computations when logging is disabled (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
akosthekiss committed Nov 17, 2023
1 parent 7535d97 commit 8e621f2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 7 additions & 6 deletions picireny/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2016-2022 Renata Hodovan, Akos Kiss.
# Copyright (c) 2016-2023 Renata Hodovan, Akos Kiss.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
Expand Down Expand Up @@ -120,11 +120,12 @@ def process_args(args):


def log_tree(title, hdd_tree):
logger.debug('%s\n\theight: %s\n\tshape: %s\n\tnodes: %s\n',
title,
info.height(hdd_tree),
', '.join(str(cnt) for cnt in info.shape(hdd_tree)),
', '.join(f'{cnt} {ty}' for ty, cnt in sorted(info.count(hdd_tree).items())))
if logger.isEnabledFor(logging.DEBUG):
logger.debug('%s\n\theight: %s\n\tshape: %s\n\tnodes: %s\n',
title,
info.height(hdd_tree),
', '.join(str(cnt) for cnt in info.shape(hdd_tree)),
', '.join(f'{cnt} {ty}' for ty, cnt in sorted(info.count(hdd_tree).items())))
logger.trace('%r', hdd_tree)


Expand Down
5 changes: 3 additions & 2 deletions picireny/hdd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) 2007 Ghassan Misherghi.
# Copyright (c) 2016-2022 Renata Hodovan, Akos Kiss.
# Copyright (c) 2016-2023 Renata Hodovan, Akos Kiss.
# Copyright (c) 2021 Daniel Vince
#
# Licensed under the BSD 3-Clause License
Expand Down Expand Up @@ -73,7 +73,8 @@ def _collect_level_nodes(node, current_level):
if not level_nodes:
continue

logger.info('Checking level %d / %d ...', level, height(hdd_tree))
if logger.isEnabledFor(logging.INFO):
logger.info('Checking level %d / %d ...', level, height(hdd_tree))

for trans_cnt, transformation in enumerate(transformations):
hdd_tree, transformed = transformation(hdd_tree, level_nodes,
Expand Down
3 changes: 2 additions & 1 deletion picireny/hoist.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def _collect_hoistables(desc):
for run in itertools.count():
logger.info('Run #%d', run)
logger.info('\tMapping size: %d', len(mapping))
logger.debug('\tMapping: %r', {c.id: m.id for c, m in mapping.items()})
if logger.isEnabledFor(logging.DEBUG):
logger.debug('\tMapping: %r', {c.id: m.id for c, m in mapping.items()})

for i, (c, m) in enumerate((c, m) for c in config for m in collect_hoistables(mapping.get(c, c))):
new_mapping = mapping.copy()
Expand Down

0 comments on commit 8e621f2

Please sign in to comment.