Skip to content

Avoid nonneeded use of PyUnicode_FromObject() #70245

Closed

Description

@serhiy-storchaka
BPO 26057
Nosy @ncoghlan, @vstinner, @vadmium, @serhiy-storchaka
Files
  • no_unicode_copy.patch
  • no_unicode_copy_2.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-04-13.12:45:58.826>
    created_at = <Date 2016-01-09.08:41:41.041>
    labels = ['interpreter-core', 'type-feature']
    title = 'Avoid nonneeded use of PyUnicode_FromObject()'
    updated_at = <Date 2016-04-14.09:31:23.514>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2016-04-14.09:31:23.514>
    actor = 'python-dev'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2016-04-13.12:45:58.826>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2016-01-09.08:41:41.041>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['41541', '42421']
    hgrepos = []
    issue_num = 26057
    keywords = ['patch']
    message_count = 8.0
    messages = ['257806', '257807', '263125', '263128', '263326', '263332', '263333', '263392']
    nosy_count = 5.0
    nosy_names = ['ncoghlan', 'vstinner', 'python-dev', 'martin.panter', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue26057'
    versions = ['Python 3.6']

    Activity

    1. serhiy-storchaka commented on Jan 9, 2016

      @serhiy-storchaka
      MemberAuthor

      In Python 2 PyUnicode_FromObject() was used for coercing 8-bit strings to unicode by decoding them with the default encoding. But in Python 3 there is no such coercing. The effect of PyUnicode_FromObject() in Python 3 is ensuring that the argument is a string and convert an instance of str subtype to exact str. The latter often is just a waste of memory and time, since resulted string is used only for retrieving UTF-8 representation or raw data.

      Proposed patch makes following things:

      1. Avoids unneeded copying of string's content.
      2. Avoids raising some unneeded exceptions.
      3. Gets rid of unneeded incref/decref.
      4. Makes some error messages more correct or informative.
      5. Converts runtime checks PyBytes_Check() for results of string encoding to asserts.

      Example of performance gain:

      Unpatched:
      $ ./python -m timeit -s "a = 'a'*100; b = 'b'*1000" -- "a in b"
      1000000 loops, best of 3: 0.404 usec per loop
      $ ./python -m timeit -s "class S(str): pass" -s "a = S('a'*100); b = S('b'*1000)" -- "a in b"
      1000000 loops, best of 3: 0.723 usec per loop

      Patched:
      $ ./python -m timeit -s "a = 'a'*100; b = 'b'*1000" -- "a in b"
      1000000 loops, best of 3: 0.383 usec per loop
      $ ./python -m timeit -s "class S(str): pass" -s "a = S('a'*100); b = S('b'*1000)" -- "a in b"
      1000000 loops, best of 3: 0.387 usec per loop

    2. serhiy-storchaka commented on Jan 9, 2016

      @serhiy-storchaka
      MemberAuthor

      See also bpo-15984 about correcting the documentation of PyUnicode_FromObject().

    3. vadmium commented on Apr 10, 2016

      @vadmium
      Member

      Left some comments

    4. serhiy-storchaka commented on Apr 10, 2016

      @serhiy-storchaka
      MemberAuthor

      Updated patch addresses Martin's comments.

    5. vadmium commented on Apr 13, 2016

      @vadmium
      Member

      Apart from one redundancy (see review), this looks good to me.

    6. python-dev commented on Apr 13, 2016

      python-devmannequin
      Mannequin

      New changeset 3f3b3d4881f6 by Serhiy Storchaka in branch 'default':
      Issue bpo-26057: Got rid of nonneeded use of PyUnicode_FromObject().
      https://hg.python.org/cpython/rev/3f3b3d4881f6

    7. serhiy-storchaka commented on Apr 13, 2016

      @serhiy-storchaka
      MemberAuthor

      Thank you for your review Martin.

    8. python-dev commented on Apr 14, 2016

      python-devmannequin
      Mannequin

      New changeset 19dec08e54a8 by Serhiy Storchaka in branch 'default':
      Issues bpo-26716, bpo-26057: Regenerate Argument Clinic code.
      https://hg.python.org/cpython/rev/19dec08e54a8

    9. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions