python / python/cpython

Performance regression in `shlex.quote` from 3.13 to 3.14

Offen
#146,385 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

3.14 3.15 performance stdlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

#132036 included an algorithmic change to shlex.quote that made it slower when the input has to be quoted. This is because the regular expression search was able to short-circuit at the first unsafe character.

However, the isascii check is worthwhile

Cc @picnixz

import re
import shlex
import timeit

# From 3.13
_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search
def old_quote(s):
    """Return a shell-escaped version of the string *s*."""
    if not s:
        return "''"
    # BEST: if s.isascii() and _find_unsafe(s) is None:
    if _find_unsafe(s) is None:
        return s

    # use single quotes, and put single quotes into double quotes
    # the string $'b is then quoted as '$'"'"'b'
    return "'" + s.replace("'", "'\"'\"'") + "'"


g = {'old_quote': old_quote, 'new_quote': shlex.quote}
print('with spaces')
print('  old', timeit.timeit("old_quote('the quick brown fox jumps over the lazy dog')", globals=g, number=1000000))
print('  new', timeit.timeit("new_quote('the quick brown fox jumps over the lazy dog')", globals=g, number=1000000))
print('without spaces')
print('  old', timeit.timeit("old_quote('thequickbrownfoxjumpsoverthelazydog')", globals=g, number=1000000))
print('  new', timeit.timeit("new_quote('thequickbrownfoxjumpsoverthelazydog')", globals=g, number=1000000))
print('non-ASCII')
print('  old', timeit.timeit("old_quote('mötley')", globals=g, number=1000000))
print('  new', timeit.timeit("new_quote('mötley')", globals=g, number=1000000))
print('short')
print('  old', timeit.timeit("old_quote('a')", globals=g, number=1000000))
print('  new', timeit.timeit("new_quote('a')", globals=g, number=1000000))

sample output:

with spaces
  old 0.4148377259989502
  new 0.5036935329990229
without spaces
  old 0.3872929839999415
  new 0.3540855330065824
ascii
  old 0.4636239370011026
  new 0.20726546400692314
short
  old 0.1217202929983614
  new 0.2977778149943333
CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-146408

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie am Einstiegspunkt shlex.quote und vergleichen Sie sein aktuelles Verhalten mit der im Bericht gezeigten Implementierung von 3.13. Prüfen Sie den verknüpften PR gh-146408 und verwenden Sie anschließend die bereitgestellten timeit-Fälle, um zu verifizieren, dass die Regression behoben ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
cli
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.