From 437bea73ec6908631e0794f4cd47cee587fb8d94 Mon Sep 17 00:00:00 2001 From: Akos Kiss Date: Fri, 24 Nov 2023 10:00:28 +0100 Subject: [PATCH] Avoid potentially expensive computations when logging is disabled (#46) --- picire/cli.py | 3 ++- picire/dd.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/picire/cli.py b/picire/cli.py index dfb1c84..5ab1370 100644 --- a/picire/cli.py +++ b/picire/cli.py @@ -250,7 +250,8 @@ def reduce(src, *, # (minus src, as that parameter can be arbitrarily large) args = locals().copy() del args['src'] - logger.info('Reduce session starts\n%s', indent(pretty_str(args), '\t')) + if logger.isEnabledFor(logging.INFO): + logger.info('Reduce session starts\n%s', indent(pretty_str(args), '\t')) cache = cache_class(**cache_config) if cache_class else None diff --git a/picire/dd.py b/picire/dd.py index 39db065..a65e694 100644 --- a/picire/dd.py +++ b/picire/dd.py @@ -184,7 +184,8 @@ def _lookup_cache(self, config, config_id): is disabled, PASS or FAIL otherwise. """ cached_result = self._cache.lookup(config) - if cached_result is not None: + + if cached_result is not None and logger.isEnabledFor(logging.DEBUG): logger.debug('\t[ %s ]: cache = %r', self._pretty_config_id(self._iteration_prefix + config_id), cached_result.name) return cached_result @@ -200,9 +201,14 @@ def _test_config(self, config, config_id): """ config_id = self._iteration_prefix + config_id - logger.debug('\t[ %s ]: test...', self._pretty_config_id(config_id)) + if logger.isEnabledFor(logging.DEBUG): + pretty_config_id = self._pretty_config_id(config_id) + logger.debug('\t[ %s ]: test...', pretty_config_id) + outcome = self._test(config, config_id) - logger.debug('\t[ %s ]: test = %r', self._pretty_config_id(config_id), outcome.name) + + if logger.isEnabledFor(logging.DEBUG): + logger.debug('\t[ %s ]: test = %r', pretty_config_id, outcome.name) if 'assert' not in config_id: self._cache.add(config, outcome)