Skip to content

Commit 6869658

Browse files
committed
gh-92810: Add What's New entry
1 parent 295f64d commit 6869658

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,33 @@ arguments (:pep:`791`).
413413
Improved modules
414414
================
415415

416+
abc
417+
---
418+
419+
* Reduce memory usage of :func:`issubclass` checks for :class:`abc.ABCMeta` subclasses.
420+
421+
:class:`abc.ABCMeta` subclasses can trigger downstream checks:
422+
423+
``issubclass(Some, Class)`` -> ``issubclass(Some, Parent)`` -> ``issubclass(Some, Top)``
424+
(nothing found) -> ``issubclass(Some, Class.__subclasses__)`` -> ``issubclass(Some, SubClass)``
425+
-> ``issubclass(Some, SubClass.__subclasses__)`` -> ...
426+
427+
Due to caching of ``issubclass`` result within each ABC class,
428+
this could lead to memory bloat in large class trees, e.g. thousands of subclasses.
429+
430+
Now :meth:`!abc.ABCMeta.register` recursively calls ``Parent.register(subclass)``,
431+
``Top.register(subclass)`` and so on for all base classes, so downstream checks are not needed.
432+
Also :meth:`!abc.ABCMeta.__new__` checks if ``__subclasses__`` method is present within class,
433+
and if not, disables recursive downstream checks in :func:`issubclass`
434+
for specific abstract class and all its children.
435+
436+
This reduces both the number of checks, and the RAM usage by internal caches:
437+
438+
``issubclass(Some, Class)`` -> ``issubclass(Some, Parent)`` -> ``issubclass(Some, Top)``
439+
(nothing found, stops here)
440+
441+
(Contributed by Maxim Martynov in :gh:`92810`.)
442+
416443
argparse
417444
--------
418445

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reduce memory usage by :meth:`~type.__subclasscheck__` for
2+
:class:`abc.ABCMeta` with large class trees.

0 commit comments

Comments
 (0)