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 str%tuple for the PEP 393 #58892

Closed
vstinner opened this issue Apr 28, 2012 · 23 comments
Closed

Optimize str%tuple for the PEP 393 #58892

vstinner opened this issue Apr 28, 2012 · 23 comments
Labels
performance Performance or resource usage

Comments

@vstinner
Copy link
Member

BPO 14687
Nosy @loewis, @pitrou, @kristjanvalur, @vstinner, @serhiy-storchaka
Files
  • pyunicode_format-2.patch
  • pyunicode_format_writer.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 2012-05-03.11:17:57.582>
    created_at = <Date 2012-04-28.00:47:57.984>
    labels = ['performance']
    title = 'Optimize str%tuple for the PEP 393'
    updated_at = <Date 2012-05-03.21:42:48.130>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2012-05-03.21:42:48.130>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-05-03.11:17:57.582>
    closer = 'vstinner'
    components = []
    creation = <Date 2012-04-28.00:47:57.984>
    creator = 'vstinner'
    dependencies = []
    files = ['25413', '25437']
    hgrepos = []
    issue_num = 14687
    keywords = ['patch']
    message_count = 23.0
    messages = ['159508', '159529', '159663', '159664', '159768', '159822', '159824', '159831', '159846', '159849', '159850', '159851', '159852', '159854', '159855', '159856', '159867', '159869', '159872', '159874', '159875', '159876', '159877']
    nosy_count = 6.0
    nosy_names = ['loewis', 'pitrou', 'kristjan.jonsson', 'vstinner', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue14687'
    versions = ['Python 3.3']

    @vstinner
    Copy link
    Member Author

    PyUnicode_Format() creates short temporary substrings. Attached patch tries to avoid substrings. For example, it avoids write of 1 character and repetition of 1 character like a space. PyUnicode_Format() now works in two steps:

    • computes the maximum character and the length of the output string
    • write characters into the output string

    I'm not sure that my patch is correct, nor that the change does really speed-up Python.

    Benchmark:

    ./python -m timeit \
    -s 's="x=%s, y=%u, z=%x"; args=(123, 456, 789)' \
    's%args'
    ./python -m timeit \
    -s 's="The %(k1)s is %(k2)s the %(k3)s."; args={"k1":"x","k2":"y","k3":"z",}' \
    's%args'

    Python 3.2:

    1000000 loops, best of 3: 0.482 usec per loop
    1000000 loops, best of 3: 0.295 usec per loop

    Python 3.3:

    1000000 loops, best of 3: 0.653 usec per loop
    1000000 loops, best of 3: 0.666 usec per loop

    Python 3.3 + patch:

    1000000 loops, best of 3: 0.596 usec per loop
    1000000 loops, best of 3: 0.566 usec per loop

    @vstinner vstinner added the performance Performance or resource usage label Apr 28, 2012
    @serhiy-storchaka
    Copy link
    Member

    I see sped up +10% on Intel Atom (but 3.2 still 2x fast).

    With non-ascii arguments speed up can be a little bit larger.

    @vstinner
    Copy link
    Member Author

    Updated patch:

    • use also PyUnicode_Kind for kind and fmtkind
    • fix compiler warnings
    • initialize data outside the loop
    • avoid duplicate PyUnicode_READ() where it is possible
    • minor code cleanup

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 30, 2012

    New changeset 42fbb4f9b540 by Victor Stinner in branch 'default':
    Issue bpo-14687: Cleanup PyUnicode_Format()
    http://hg.python.org/cpython/rev/42fbb4f9b540

    New changeset 08b54c635586 by Victor Stinner in branch 'default':
    Issue bpo-14687: Avoid an useless duplicated string in PyUnicode_Format()
    http://hg.python.org/cpython/rev/08b54c635586

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 1, 2012

    New changeset 4b98ce6ef95e by Victor Stinner in branch 'default':
    Issue bpo-14687: Optimize str%args
    http://hg.python.org/cpython/rev/4b98ce6ef95e

    New changeset a966f9311ebb by Victor Stinner in branch 'default':
    Issue bpo-14687: Cleanup PyUnicode_Format()
    http://hg.python.org/cpython/rev/a966f9311ebb

    @vstinner
    Copy link
    Member Author

    vstinner commented May 2, 2012

    pyunicode_format_writer.patch: a new completly different approach. It's an optimistic patch: start with a short ASCII buffer, and grows slowly the buffer, and convert to UCS2 and maybe to UCS4 if needed. The UTF-8 decoder is based on the same idea.

    The patch adds a "unicode writer", the optimistic writer. It overallocates the buffer by 50% to limit the number of calls to PyUnicode_Resize(). It may be reused by other functions.

    My dummy benchmark script:
    ------------

    $ cat ~/bench.sh 
    ./python -m timeit \
        -s 'fmt="%s:"; arg="abc"' \
        'fmt % arg'
    ./python -m timeit \
        -s 'N=200; L=3; fmt="%s"*N; args=("a"*L,)*N' \
        'fmt % args'
    ./python -m timeit \
        -s 's="x=%s, y=%u, z=%x"; args=(123, 456, 789)' \
        's%args'
    ./python -m timeit \
        -s 's="The %(k1)s is %(k2)s the %(k3)s."; args={"k1":"x","k2":"y","k3":"z",}' \
        's%args'

    Results.

    Python 3.2:

    10000000 loops, best of 3: 0.0916 usec per loop
    100000 loops, best of 3: 4.04 usec per loop
    1000000 loops, best of 3: 0.492 usec per loop
    1000000 loops, best of 3: 0.305 usec per loop

    Python 3.3:

    10000000 loops, best of 3: 0.169 usec per loop
    100000 loops, best of 3: 8.02 usec per loop
    1000000 loops, best of 3: 0.648 usec per loop
    1000000 loops, best of 3: 0.658 usec per loop

    Python 3.3 optimist (compared to 3.3):

    10000000 loops, best of 3: 0.123 usec per loop (-27%)
    100000 loops, best of 3: 5.73 usec per loop (-29%)
    1000000 loops, best of 3: 0.466 usec per loop (-28%)
    1000000 loops, best of 3: 0.454 usec per loop (-31%)

    Overhead of the PEP-393 (Python 3.2 => 3.3) without -> with the patch:

    • 85% -> 35%
    • 99% -> 41%
    • 31% -> -5% (Python 3.3 is *faster* on this specific case! maybe thanks to f4837725c50f)
    • 115% -> 49%

    --

    "%(name)s" syntax is still *much* slower than Python 3.2, I don't understand why.

    Parameters of the Unicode writer (overallocation factor and initial size) may be adjusted (later?) for better performances.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 2, 2012

    New changeset 90b4c2d7c90d by Victor Stinner in branch 'default':
    Issue bpo-14687: Optimize str%tuple for the "%(name)s" syntax
    http://hg.python.org/cpython/rev/90b4c2d7c90d

    @vstinner
    Copy link
    Member Author

    vstinner commented May 3, 2012

    Parameters of the Unicode writer (overallocation factor
    and initial size) may be adjusted (later?) for better performances.

    I tried to compute the initial size from the args object. It is hard because we don't know how arguments are used. For example, an argument may not be a number formatted as decimal, but the width of an argument. Another example: "%.3s" may be used to only read the 3 first characters of a very long string.

    So I consider that it is *simpler and safer* to not guess anything from args, but only choose correctly the overallocation factor. I tried the following factors: 1 (no overallocation), 1.25, 1.5 and 2.0. It looks like 1.25 (pos*5/4) is the best compromise and offers the best performances.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 3, 2012

    New changeset 830eeff4fe8f by Victor Stinner in branch 'default':
    Issue bpo-14624, bpo-14687: Optimize unicode_widen()
    http://hg.python.org/cpython/rev/830eeff4fe8f

    @vstinner
    Copy link
    Member Author

    vstinner commented May 3, 2012

    Results on 32 bits (Intel Core i5 CPU 661 @ 3.33GHz) on Linux 3.0 with a new patch.

    Python 3.2:

    10000000 loops, best of 3: 0.133 usec per loop
    100000 loops, best of 3: 4.64 usec per loop
    1000000 loops, best of 3: 0.637 usec per loop
    1000000 loops, best of 3: 0.364 usec per loop

    Python 3.3 @ 1439e2d1f490 (before my first optimization on str%tuple):

    10000000 loops, best of 3: 0.193 usec per loop
    100000 loops, best of 3: 10.1 usec per loop
    1000000 loops, best of 3: 0.838 usec per loop
    1000000 loops, best of 3: 0.825 usec per loop

    Python 3.3 + patch, overallocate 50%:

    10000000 loops, best of 3: 0.15 usec per loop
    100000 loops, best of 3: 8.27 usec per loop
    1000000 loops, best of 3: 0.527 usec per loop
    1000000 loops, best of 3: 0.566 usec per loop

    Python 3.3 + patch, overallocate 25%:

    10000000 loops, best of 3: 0.142 usec per loop
    100000 loops, best of 3: 7.93 usec per loop
    1000000 loops, best of 3: 0.532 usec per loop
    1000000 loops, best of 3: 0.546 usec per loop

    I'm going to commit the new patch with an overallocation of 25%.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 3, 2012

    New changeset f1db931b93d3 by Victor Stinner in branch 'default':
    Issue bpo-14687: str%tuple now uses an optimistic "unicode writer" instead of an
    http://hg.python.org/cpython/rev/f1db931b93d3

    @vstinner vstinner closed this as completed May 3, 2012
    @pitrou
    Copy link
    Member

    pitrou commented May 3, 2012

    Results on 32 bits (Intel Core i5 CPU 661 @ 3.33GHz) on Linux 3.0 with
    a new patch.

    It would be nice to have measurements under Windows.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 3, 2012

    New changeset 0a9143d7b097 by Victor Stinner in branch 'default':
    Issue bpo-14687: Cleanup unicode_writer_prepare()
    http://hg.python.org/cpython/rev/0a9143d7b097

    @serhiy-storchaka
    Copy link
    Member

    It would be nice to have measurements under Windows.

    The differences between 32-bit Linux and 32-bit Windows should not be.
    But 64-bit can be different, and 64-bit Linux and 64-bit Windows can
    vary from 32-bit, and from each other. There are also differences
    between high-end Intel Core and low-end Intel Atom, and AMD processors.

    @pitrou
    Copy link
    Member

    pitrou commented May 3, 2012

    > It would be nice to have measurements under Windows.

    The differences between 32-bit Linux and 32-bit Windows should not be.

    The Windows memory allocator is quite different from the glibc's, so the
    overallocation / resizing approach may play differently.

    @serhiy-storchaka
    Copy link
    Member

    Results on 32 bits (Intel Core i5 CPU 661 @ 3.33GHz) on Linux 3.0 with a new patch.

    Your tests only for ascii. You should also see some of corner cases --
    a large format string and a few small arguments (templating),
    a small simple format string and one large argument
    (idiomatic '[%s]' % ', '.join(long_list)).

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented May 3, 2012

    New changeset f58159e5d52f by Victor Stinner in branch 'default':
    Issue bpo-14687: Remove redundant length attribute of unicode_write_t
    http://hg.python.org/cpython/rev/f58159e5d52f

    @kristjanvalur
    Copy link
    Mannequin

    kristjanvalur mannequin commented May 3, 2012

    Do you think that this optimization is relevant to 2.7?
    In that case, it might be worthwhile for me to backport it to our EVE branch...

    @serhiy-storchaka
    Copy link
    Member

    Do you think that this optimization is relevant to 2.7?

    No, this is just an attempt to deal with the shortcomings of PEP-393.

    @vstinner
    Copy link
    Member Author

    vstinner commented May 3, 2012

    Results on Windows Seven 64 bits, on Intel i7 2600 @ 3.4 GHz (8 cores), Windows running in a virtual machine (kvm) with hardware virtualization.

    Python 3.2.2:

    10000000 loops, best of 3: 0.12 usec per loop
    100000 loops, best of 3: 5.12 usec per loop
    1000000 loops, best of 3: 0.581 usec per loop
    1000000 loops, best of 3: 0.397 usec per loop

    Python 3.3 @ 1439e2d1f490:

    1000000 loops, best of 3: 0.265 usec per loop
    100000 loops, best of 3: 11 usec per loop
    1000000 loops, best of 3: 0.961 usec per loop
    1000000 loops, best of 3: 0.924 usec per loop

    Python 3.3 @ cdc4e0f8135d:

    10000000 loops, best of 3: 0.154 usec per loop
    100000 loops, best of 3: 7.85 usec per loop
    1000000 loops, best of 3: 0.583 usec per loop
    1000000 loops, best of 3: 0.535 usec per loop

    To be honest, I'm surprised that my work speeds up Python 3.3 on Windows, because I read that realloc() on Windows is not efficient. It is maybe no more true with Windows Seven? It would be interesting if someone could run the benchmark on Windows XP or 2000.

    @pitrou
    Copy link
    Member

    pitrou commented May 3, 2012

    Python 3.3 @ 1439e2d1f490:

    1000000 loops, best of 3: 0.265 usec per loop
    100000 loops, best of 3: 11 usec per loop
    1000000 loops, best of 3: 0.961 usec per loop
    1000000 loops, best of 3: 0.924 usec per loop

    Python 3.3 @ cdc4e0f8135d:

    10000000 loops, best of 3: 0.154 usec per loop
    100000 loops, best of 3: 7.85 usec per loop
    1000000 loops, best of 3: 0.583 usec per loop
    1000000 loops, best of 3: 0.535 usec per loop

    Very nice, thank you!

    @kristjanvalur
    Copy link
    Mannequin

    kristjanvalur mannequin commented May 3, 2012

    because I read that realloc() on Windows is not efficient.
    Efficiency is not a Boolean property, you know :) Anyway, I´d be surprised if it were very iniefficient, given that the heap allocators on Windows are quite mature by now.

    @vstinner
    Copy link
    Member Author

    vstinner commented May 3, 2012

    > because I read that realloc() on Windows is not efficient.
    Efficiency is not a Boolean property, you know :) Anyway,
    I´d be surprised if it were very iniefficient, given that
    the heap allocators on Windows are quite mature by now.

    My benchmark is more a *micro* benchmark on some very basic cases (short ASCII strings). But it looks like overallocating *helps*. In the following example, only two resize are needed:

    ./python -m timeit \
    -s 'N=200; L=3; fmt="%s"*N; args=("a"*L,)*N' \
    'fmt % args'

    len(fmt)+100 = 500 characters are allocated for the initial buffer. Writing the 501st character enlarges the buffer to 626 characters: first resize. The output string is truncated to 600 characters: second and final resize.

    @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
    performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants