Repository navigation
Conversation
…loggers) Manager._clear_cache() walked every logger in loggerDict on each setLevel() or disable() call, so configuring N loggers one at a time was quadratic. Track loggers that have populated their isEnabledFor cache in a WeakSet and clear only those.
Contributor
|
As mentioned on the issue, is this a real-world problem? There are some issues with the current approach:
Both go away by clearing all loggers in the set unconditionally and then emptying the set: with _lock:
for logger in self._cached_loggers:
logger._cache.clear()
self._cached_loggers.clear()
self.root._cache.clear()Some more detailsClearing a superset of what `main` clears is safe, and as a bonus it fixes a stale cache for loggers not in `loggerDict` (`Logger("x").setLevel(ERROR)` currently leaves `isEnabledFor(DEBUG)` True). Please add a test for that and for `_cached_loggers` being empty after `setLevel()`.Reproducer for both: import logging, time
lg = logging.getLogger("one")
def us_per_setlevel():
t0 = time.perf_counter()
for _ in range(2000):
lg.setLevel(logging.INFO)
lg.isEnabledFor(logging.DEBUG)
return (time.perf_counter() - t0) / 2000 * 1e6
print(f"baseline: {us_per_setlevel():7.1f} us")
registered = [logging.getLogger(f"app.mod{i}") for i in range(4000)]
for l in registered:
l.isEnabledFor(logging.DEBUG)
lg.setLevel(logging.INFO)
print(f"after bulk invalidate: {us_per_setlevel():7.1f} us") # (2)
logging.root.manager.loggerDict.clear()
standalone = [logging.Logger(f"s{i}") for i in range(4000)]
for l in standalone:
l.isEnabledFor(logging.DEBUG)
print(f"4000 standalone alive: {us_per_setlevel():7.1f} us") # (1)Another approach would be to check the cache with a global counter. That might also help with gh-155106 (the PR now is neutral for contention) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Logger.setLevel()andlogging.disable()callManager._clear_cache(), which walks every logger inloggerDictto clear itsisEnabledForcache, whether or not it has cached anything. Setting levels on N loggers one at a time is O(N^2):(debug build of main, script from the issue)
The manager now keeps a
WeakSetof loggers that have populated their cache.isEnabledForadds itself on the cache-miss path, inside the lock it already takes, so the hot cache-hit path is untouched._clear_cacheonly visits those loggers, and still only clears ones that are registered inloggerDictunder their name, same rule as before.test_loggingpasses; the existingtest_cachingcovers the invalidation behaviour.