Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize BZ2File, GzipFile, and LZMAFile __iter__ method. #87953

Closed
methane opened this issue Apr 9, 2021 · 3 comments
Closed

Optimize BZ2File, GzipFile, and LZMAFile __iter__ method. #87953

methane opened this issue Apr 9, 2021 · 3 comments
Labels
3.10 only security fixes performance Performance or resource usage stdlib Python modules in the Lib dir

Comments

@methane
Copy link
Member

methane commented Apr 9, 2021

BPO 43787
Nosy @gpshead, @methane, @animalize, @corona10
PRs
  • bpo-43787: Add __iter__ to GzipFile, BZ2File, and LZMAFile #25353
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-04-13.04:52:26.466>
    created_at = <Date 2021-04-09.08:49:42.998>
    labels = ['library', '3.10', 'performance']
    title = 'Optimize BZ2File, GzipFile, and LZMAFile __iter__ method.'
    updated_at = <Date 2021-04-13.04:52:26.466>
    user = 'https://github.com/methane'

    bugs.python.org fields:

    activity = <Date 2021-04-13.04:52:26.466>
    actor = 'methane'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-04-13.04:52:26.466>
    closer = 'methane'
    components = ['Library (Lib)']
    creation = <Date 2021-04-09.08:49:42.998>
    creator = 'methane'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43787
    keywords = ['patch']
    message_count = 3.0
    messages = ['390599', '390836', '390921']
    nosy_count = 4.0
    nosy_names = ['gregory.p.smith', 'methane', 'malin', 'corona10']
    pr_nums = ['25353']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue43787'
    versions = ['Python 3.10']

    @methane
    Copy link
    Member Author

    methane commented Apr 9, 2021

    iter method of BZ2File, GzipFile, and LZMAFile is IOBase.iter. It calls readline() for each line.

    Since readline() is defined as Python function, it is slower than C iterator. Adding custom iter method that delegates to underlying buffer iter makes for line in file 2x faster.

        def __iter__(self):
            self._check_can_read()
            return self._buffer.__iter__()

    The original issue is reported here.
    https://discuss.python.org/t/non-optimal-bz2-reading-speed/6869
    This issue is relating to bpo-43785.

    @methane methane added 3.10 only security fixes stdlib Python modules in the Lib dir performance Performance or resource usage labels Apr 9, 2021
    @animalize
    Copy link
    Mannequin

    animalize mannequin commented Apr 12, 2021

    I think this change is safe.

    The behaviors should be exactly the same, except the iterators are different objects (obj vs obj._buffer).

    @methane
    Copy link
    Member Author

    methane commented Apr 13, 2021

    New changeset d2a8e69 by Inada Naoki in branch 'master':
    bpo-43787: Add __iter__ to GzipFile, BZ2File, and LZMAFile (GH-25353)
    d2a8e69

    @methane methane closed this as completed Apr 13, 2021
    @methane methane closed this as completed Apr 13, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes performance Performance or resource usage stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant