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

Avoid string copy when split char doesn't match #45879

Closed
smontanaro opened this issue Dec 2, 2007 · 4 comments
Closed

Avoid string copy when split char doesn't match #45879

smontanaro opened this issue Dec 2, 2007 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@smontanaro
Copy link
Contributor

BPO 1538
Nosy @gvanrossum, @smontanaro
Files
  • string-split.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 = None
    closed_at = <Date 2007-12-08.15:34:09.129>
    created_at = <Date 2007-12-02.07:51:02.919>
    labels = ['interpreter-core', 'type-feature']
    title = "Avoid string copy when split char doesn't match"
    updated_at = <Date 2007-12-08.15:34:09.127>
    user = 'https://github.com/smontanaro'

    bugs.python.org fields:

    activity = <Date 2007-12-08.15:34:09.127>
    actor = 'skip.montanaro'
    assignee = 'none'
    closed = True
    closed_date = <Date 2007-12-08.15:34:09.129>
    closer = 'skip.montanaro'
    components = ['Interpreter Core']
    creation = <Date 2007-12-02.07:51:02.919>
    creator = 'skip.montanaro'
    dependencies = []
    files = ['8864']
    hgrepos = []
    issue_num = 1538
    keywords = ['patch']
    message_count = 4.0
    messages = ['58081', '58145', '58169', '58295']
    nosy_count = 2.0
    nosy_names = ['gvanrossum', 'skip.montanaro']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue1538'
    versions = ['Python 2.6']

    @smontanaro
    Copy link
    Contributor Author

    The topic of avoiding string copies in certain string methods came up in
    the
    ChiPy list:

    http://mail.python.org/pipermail/chicago/2007-December/002975.html.

    The attached patch modifies the split and rsplit implementations to
    avoid
    making a copy of self when the split fails to find anything to split on:

        >>> s = "abc def"
        >>> x = s.split(';')
        >>> x[0] is s
        True
        >>> y = s.rsplit('-')
        >>> y[0] is s
        True
        >>> t = "abcdef"
        >>> x = t.split()
        >>> x[0] is t
        True
        >>> y = t.rsplit()
        >>> y[0] is t
        True

    All tests pass. Given that this is just a small optimization I
    don't believe any changes to the docs or the existing tests are
    necessary.

    @smontanaro smontanaro added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Dec 2, 2007
    @gvanrossum
    Copy link
    Member

    I think this is not quite right; you shouldn't return self unless it is
    an exact string instance. If it is a subclass of PyString should make a
    copy.

    @smontanaro
    Copy link
    Contributor Author

    I'm not sure why a string subclass shouldn't work, but I've attached a new
    version of the patch that calls PyString_CheckExact() to prevent using a
    string subclass.

    @smontanaro
    Copy link
    Contributor Author

    In the absence of any more feedback, I checked this in as r59420.

    @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) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants