-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
bpo-30962: Add caching to Logger.isEnabledFor() #2752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
26a8efc
aeec4d9
7deaaab
f3b23ad
d10e0e2
d6d28ec
11fee13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1244,6 +1244,17 @@ def _fixupChildren(self, ph, alogger): | |
| alogger.parent = c.parent | ||
| c.parent = alogger | ||
|
|
||
| def _clear_cache(self): | ||
| """ | ||
| Clear the cache for all loggers in loggerDict | ||
| Called when level changes are made | ||
| """ | ||
|
|
||
| _acquireLock() | ||
| for logger in self.loggerDict.values(): | ||
| logger._cache.clear() | ||
| _releaseLock() | ||
|
|
||
| #--------------------------------------------------------------------------- | ||
| # Logger classes and functions | ||
| #--------------------------------------------------------------------------- | ||
|
|
@@ -1274,12 +1285,14 @@ def __init__(self, name, level=NOTSET): | |
| self.propagate = True | ||
| self.handlers = [] | ||
| self.disabled = False | ||
| self._cache = {} | ||
|
|
||
| def setLevel(self, level): | ||
| """ | ||
| Set the logging level of this logger. level must be an int or a str. | ||
| """ | ||
| self.level = _checkLevel(level) | ||
| self.manager._clear_cache() | ||
|
|
||
| def debug(self, msg, *args, **kwargs): | ||
| """ | ||
|
|
@@ -1543,9 +1556,15 @@ def isEnabledFor(self, level): | |
| """ | ||
| Is this logger enabled for level 'level'? | ||
| """ | ||
| if self.manager.disable >= level: | ||
| return False | ||
| return level >= self.getEffectiveLevel() | ||
| if level not in self._cache: | ||
| _acquireLock() | ||
| if self.manager.disable >= level: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it not make more sense to cache the result of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is where most of the time savings is, but it's still faster (Something like 5%) than checking self.manager.disable every time.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does have me thinking, in the LoggerAdapter class, there's currently no caching. LoggerAdapter.isEnabledFor() reimplements the logic from Logger.isEnabledFor(). If we point to Logger.isEnabledFor() instead of reimplementing, we get caching. I don't see a functional difference. Is there?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vsajip The only thing I see is
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The
Although it slows down
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand why the I wasn't associating the caching with the levels, but rather the |
||
| self._cache[level] = False | ||
| else: | ||
| self._cache[level] = level >= self.getEffectiveLevel() | ||
| _releaseLock() | ||
|
|
||
| return self._cache[level] | ||
|
||
|
|
||
| def getChild(self, suffix): | ||
| """ | ||
|
|
@@ -1910,6 +1929,7 @@ def disable(level=CRITICAL): | |
| Disable all logging calls of severity 'level' and below. | ||
| """ | ||
| root.manager.disable = level | ||
| root.manager._clear_cache() | ||
|
|
||
| def shutdown(handlerList=_handlerList): | ||
| """ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that a lock is useful here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it needed to keep another thread from changing loggerDict or the cache dictionaries?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ok, right, I first understood that the lock was supposed to protect the cache :-) I don't think that the cache deserves a lock.