Skip to content

Improve performance in DictConfigurator by optimizing logger lookup #132372

@allrob23

Description

@allrob23

Feature or enhancement

Proposal:

In logging.config.DictConfigurator.configure() (in Lib/logging/config.py), the current implementation uses a list called existing to store logger names:

existing = list(root.manager.loggerDict.keys())
existing.sort()

This list is then used in both an if name in existing check and an existing.index(name) call:

if name in existing:
    i = existing.index(name)

Both operations are O(n) on a list. While this likely isn’t a major performance bottleneck for a small number of loggers, it could be avoided — and the change would be a net gain in efficiency.

One idea would be to build a set_existing = set(existing) for O(1) membership checks, and possibly a dict_existing = {name: idx} to avoid linear-time indexing, while keeping the original sorted list for the required prefix-based logic:

existing = list(root.manager.loggerDict.keys())
existing.sort()
existing_set = set(existing)
index_map = {name: idx for idx, name in enumerate(existing)}

Then the logic becomes:

if name in existing_set:
    i = index_map[name]

This avoids repeated linear scans without changing existing behavior.

Would this kind of change be reasonable here, or is there a better approach you’d recommend for balancing performance and clarity?

Happy to draft a PR if the direction makes sense.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions