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

bytes(x) is slow when x is bytearray #71891

Closed
methane opened this issue Aug 7, 2016 · 8 comments
Closed

bytes(x) is slow when x is bytearray #71891

methane opened this issue Aug 7, 2016 · 8 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@methane
Copy link
Member

methane commented Aug 7, 2016

BPO 27704
Nosy @vstinner, @methane, @serhiy-storchaka, @matrixise
Files
  • fast-bytearray-fromobject.patch
  • fast-bytearray-fromobject.patch
  • 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 = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2016-08-15.08:55:08.607>
    created_at = <Date 2016-08-07.19:22:27.381>
    labels = ['interpreter-core', 'performance']
    title = 'bytes(x) is slow when x is bytearray'
    updated_at = <Date 2016-08-15.08:55:51.642>
    user = 'https://github.com/methane'

    bugs.python.org fields:

    activity = <Date 2016-08-15.08:55:51.642>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2016-08-15.08:55:08.607>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2016-08-07.19:22:27.381>
    creator = 'methane'
    dependencies = []
    files = ['44039', '44059']
    hgrepos = []
    issue_num = 27704
    keywords = ['patch']
    message_count = 8.0
    messages = ['272130', '272131', '272249', '272258', '272260', '272261', '272721', '272739']
    nosy_count = 5.0
    nosy_names = ['vstinner', 'methane', 'python-dev', 'serhiy.storchaka', 'matrixise']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue27704'
    versions = ['Python 3.6']

    @methane
    Copy link
    Member Author

    methane commented Aug 7, 2016

    When bytes(x), bytes_new checks if x is integer via PyNumber_AsSize_t(x).
    It cause TypeError internally.

    When x is not an integer, especially bytearray or memoryview, the internal
    exception cause significant overhead.

    # HEAD
    $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
    1000000 loops, best of 3: 0.696 usec per loop
    $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
    1000000 loops, best of 3: 0.699 usec per loop
    $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
    1000000 loops, best of 3: 0.701 usec per loop

    # this patch
    $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
    1000000 loops, best of 3: 0.265 usec per loop
    $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
    1000000 loops, best of 3: 0.265 usec per loop
    $ ./python -m timeit -s 'data=bytearray(b"xyz")' 'bytes(data)'
    1000000 loops, best of 3: 0.267 usec per loop

    @methane methane added interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Aug 7, 2016
    @matrixise
    Copy link
    Member

    the patch seems to be fine.

    @serhiy-storchaka
    Copy link
    Member

    bytearray suffers from the same issue. It would be nice to optimize it too.

    @matrixise
    Copy link
    Member

    Hi Serhiy

    Thank you for your feedback.

    Stephane

    @methane
    Copy link
    Member Author

    methane commented Aug 9, 2016

    Thanks for comments.

    @serhiy-storchaka
    Copy link
    Member

    LGTM.

    @serhiy-storchaka serhiy-storchaka self-assigned this Aug 9, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 15, 2016

    New changeset 789a42401009 by Serhiy Storchaka in branch 'default':
    Issue bpo-27704: Optimized creating bytes and bytearray from byte-like objects
    https://hg.python.org/cpython/rev/789a42401009

    @serhiy-storchaka
    Copy link
    Member

    Thank you for your contribution Naoki.

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants