python / python/cpython

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

オープン
#146,385 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

3.14 3.15 performance stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

shlex.quote のエントリーポイントから始め、現在の動作をレポートに示されている 3.13 の実装と比較してください。リンクされている PR gh-146408 を確認し、その後、提供されている timeit のケースを使ってリグレッションが解消されていることを検証してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
cli
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。