Skip to content

configparser clear() on a section or parser takes quadratic time #161108

Closed

Description

@gpshead

Bug report

Bug description:

SectionProxy and RawConfigParser inherit clear() from MutableMapping, which calls popitem() once per entry. Each popitem() call copies all remaining keys (SectionProxy.__iter__() builds the merged option list, RawConfigParser.popitem() calls sections()), so clearing a section or the parser takes quadratic time.

import configparser, time
cp = configparser.RawConfigParser()
cp.read_string("[s]\n" + "".join(f"k{i} = v\n" for i in range(20000)))
t = time.perf_counter(); cp["s"].clear(); print(time.perf_counter() - t)

This takes several seconds for 20000 options and four times as long with twice as many; parser.clear() with many sections behaves the same way. parser.clear() dates from gh-61024.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. self-assigned this
    on Nov 1, 2026
  2. added
    type-bugAn unexpected behavior, bug, or error
    performancePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 1, 2026
  3. added a commit that references this issue on Nov 1, 2026
  4. added 2 commits that reference this issue on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions