https://github.com/python/cpython/commit/9c93b7402ba11d1d68e856516e56ca72989a7db9
commit: 9c93b7402ba11d1d68e856516e56ca72989a7db9
branch: main
author: Jason R. Coombs <[email protected]>
committer: jaraco <[email protected]>
date: 2024-04-14T11:10:09Z
summary:

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

Reduces import time by over 50% (10431µs vs 4350µs on Apple M3 Pro).

files:
A Misc/NEWS.d/next/Library/2024-04-09-20-14-44.gh-issue-117348.A2NAAz.rst
M Lib/configparser.py

diff --git a/Lib/configparser.py b/Lib/configparser.py
index d0326c60e9b907..ff7d712bed4afc 100644
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -143,17 +143,18 @@
         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
 import contextlib
-from dataclasses import dataclass, field
 import functools
 import io
 import itertools
 import os
 import re
 import sys
-from typing import Iterable
+import types
 
 __all__ = ("NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
            "NoOptionError", "InterpolationError", "InterpolationDepthError",
@@ -538,21 +539,18 @@ def _interpolate_some(self, parser, option, accum, rest, 
section, map,
                     "found: %r" % (rest,))
 
 
-@dataclass
 class _ReadState:
-    elements_added : set[str] = field(default_factory=set)
+    elements_added : set[str]
     cursect : dict[str, str] | None = None
     sectname : str | None = None
     optname : str | None = None
     lineno : int = 0
     indent_level : int = 0
-    errors : list[ParsingError] = field(default_factory=list)
-
+    errors : list[ParsingError]
 
-@dataclass
-class _Prefixes:
-    full : Iterable[str]
-    inline : Iterable[str]
+    def __init__(self):
+        self.elements_added = set()
+        self.errors = list()
 
 
 class _Line(str):
@@ -560,7 +558,7 @@ class _Line(str):
     def __new__(cls, val, *args, **kwargs):
         return super().__new__(cls, val)
 
-    def __init__(self, val, prefixes: _Prefixes):
+    def __init__(self, val, prefixes):
         self.prefixes = prefixes
 
     @functools.cached_property
@@ -653,7 +651,7 @@ def __init__(self, defaults=None, dict_type=_default_dict,
             else:
                 self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
                                           re.VERBOSE)
-        self._prefixes = _Prefixes(
+        self._prefixes = types.SimpleNamespace(
             full=tuple(comment_prefixes or ()),
             inline=tuple(inline_comment_prefixes or ()),
         )
diff --git 
a/Misc/NEWS.d/next/Library/2024-04-09-20-14-44.gh-issue-117348.A2NAAz.rst 
b/Misc/NEWS.d/next/Library/2024-04-09-20-14-44.gh-issue-117348.A2NAAz.rst
new file mode 100644
index 00000000000000..2451a4e4f622e4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-04-09-20-14-44.gh-issue-117348.A2NAAz.rst
@@ -0,0 +1,2 @@
+Largely restored import time performance of configparser by avoiding
+dataclasses.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to