gh-150819: Cache ConfigParser converter names per class#150826
Open
gaborbernat wants to merge 1 commit into
Open
gh-150819: Cache ConfigParser converter names per class#150826gaborbernat wants to merge 1 commit into
gaborbernat wants to merge 1 commit into
Conversation
ConverterMapping discovered the get* converter methods with dir() plus a regex over every attribute on each construction, producing the same result for every instance of a parser class. Cache the discovered names per class and reuse them. The discovered converters are identical.
3673545 to
3a9df7c
Compare
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.
Constructing a
ConfigParserscans every attribute on the parser withdir()and matches each name against a regular expression to discover itsget<type>converter methods. That discovery produces the same answer for every instance of a given parser class, yet it runs in full on each construction. Tools that read several config files, or build a fresh parser per file, pay this cost repeatedly — build backends, linters and test runners commonly create many parsers over a run.This caches the discovered converter names per parser class and reuses them on later constructions, keyed on the exact type so a subclass that adds converters is handled correctly. The discovered set is identical to what the scan produces today.
Constructing a parser improves from 390 µs to 78.9 µs, 394% faster; constructing and reading a real
setup.cfgend to end improves by 19%.Benchmark (pyperf)
Run base vs patched by swapping
Lib/configparser.pyon the same interpreter. The config text is a realsetup.cfgfrom the top-1000 corpus (msal).Resolves #150819.