fix(python): remove redundant debug setter call in __deepcopy__
Hi! It seems I'm hitting an extremely pathological case during the creation of many openapi-generated objects in a very large (many registered loggers) codebase.
The __deepcopy__ method on the Configuration copies all __dict__ items (including _Configuration__debug) and then loggers are shallow-copied over. After that, we call result.debug = self.debug which fires the property setter with the same value already present. On Python 3.12+ (actually maybe earlier, just what im using here) , the setter's logger.setLevel() call triggers logging.Manager._clear_cache() which iterates every registered logger — O(n_loggers) per deepcopy.
In applications with many loggers, this causes severe performance degradation when Configuration objects are deepcopied frequently (e.g., per-request in API clients).
I let my friendly bot create an example perf bechmmark to showcase the issue and fix:
https://gist.github.com/markjm/bb5805505d9dfddc5d595cadd0beaff1
$ python3 scripts/python_debug_setter_repro.py
Python 3.12.3
Registered loggers: 20,000
deepcopy() calls: 1,000
Before fix: 1.744s
After fix: 0.004s
Speedup: 413.2x
The redundant debug setter in deepcopy causes measurable overhead that scales with the number of registered loggers (O(n_loggers) per copy).
Because we already copy over the parts we want (and explicitly configure loggers how we want on copy), I believe this change to be essentially a noop except for the perf improvement.
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master../mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
🫡 @cbornet @tomplus @krjakbrjak @fa0311 @multani
Summary by cubic
Remove a redundant debug setter call in Python clients’ Configuration.__deepcopy__. This avoids O(n_loggers) logger cache clears on Python 3.12+ and significantly speeds up deepcopy in apps with many loggers.
- Bug Fixes
- Stop calling
result.debug = self.debugin__deepcopy__; the value is already copied from__dict__. - Keep the
logger_filesetter to re-create the file handler excluded from the dict copy. - Update Python templates and regenerated samples; add tests ensuring deepcopy preserves
debugTrue/False.
- Stop calling