gh-117348: restore import time performance of configparser (#117703)

Reduces import time by over 50% (10431µs vs 4350µs on Apple M3 Pro).
This commit is contained in:
Jason R. Coombs 2024-04-14 07:10:09 -04:00 committed by GitHub
parent c99d374fc7
commit 9c93b7402b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 13 deletions

View file

@ -143,17 +143,18 @@ ConfigParser -- responsible for parsing a list of
between keys and values are surrounded by spaces. between keys and values are surrounded by spaces.
""" """
from collections.abc import MutableMapping # Do not import dataclasses; overhead is unacceptable (gh-117703)
from collections.abc import Iterable, MutableMapping
from collections import ChainMap as _ChainMap from collections import ChainMap as _ChainMap
import contextlib import contextlib
from dataclasses import dataclass, field
import functools import functools
import io import io
import itertools import itertools
import os import os
import re import re
import sys import sys
from typing import Iterable import types
__all__ = ("NoSectionError", "DuplicateOptionError", "DuplicateSectionError", __all__ = ("NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
"NoOptionError", "InterpolationError", "InterpolationDepthError", "NoOptionError", "InterpolationError", "InterpolationDepthError",
@ -538,21 +539,18 @@ class ExtendedInterpolation(Interpolation):
"found: %r" % (rest,)) "found: %r" % (rest,))
@dataclass
class _ReadState: class _ReadState:
elements_added : set[str] = field(default_factory=set) elements_added : set[str]
cursect : dict[str, str] | None = None cursect : dict[str, str] | None = None
sectname : str | None = None sectname : str | None = None
optname : str | None = None optname : str | None = None
lineno : int = 0 lineno : int = 0
indent_level : int = 0 indent_level : int = 0
errors : list[ParsingError] = field(default_factory=list) errors : list[ParsingError]
def __init__(self):
@dataclass self.elements_added = set()
class _Prefixes: self.errors = list()
full : Iterable[str]
inline : Iterable[str]
class _Line(str): class _Line(str):
@ -560,7 +558,7 @@ class _Line(str):
def __new__(cls, val, *args, **kwargs): def __new__(cls, val, *args, **kwargs):
return super().__new__(cls, val) return super().__new__(cls, val)
def __init__(self, val, prefixes: _Prefixes): def __init__(self, val, prefixes):
self.prefixes = prefixes self.prefixes = prefixes
@functools.cached_property @functools.cached_property
@ -653,7 +651,7 @@ class RawConfigParser(MutableMapping):
else: else:
self._optcre = re.compile(self._OPT_TMPL.format(delim=d), self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
re.VERBOSE) re.VERBOSE)
self._prefixes = _Prefixes( self._prefixes = types.SimpleNamespace(
full=tuple(comment_prefixes or ()), full=tuple(comment_prefixes or ()),
inline=tuple(inline_comment_prefixes or ()), inline=tuple(inline_comment_prefixes or ()),
) )

View file

@ -0,0 +1,2 @@
Largely restored import time performance of configparser by avoiding
dataclasses.