Performance regression in `shlex.quote` from 3.13 to 3.14
未关闭
还没有人认领这个 Issue。
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 shlex.quote 入口点开始,将其当前行为与报告中所示的 3.13 实现进行比较。查看链接的 PR gh-146408,然后使用提供的 timeit 用例验证回归问题已得到修复。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- cli
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100