python / cpython

The Python programming language
https://www.python.org
Other
61.97k stars 29.8k forks source link

Accelerate string.Template by using formatted string literals #72496

Open serhiy-storchaka opened 7 years ago

serhiy-storchaka commented 7 years ago
BPO 28309
Nosy @birkenfeld, @ericvsmith, @serhiy-storchaka
Files
  • faster_template.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: ```python assignee = None closed_at = None created_at = labels = ['3.7', 'library', 'performance'] title = 'Accelerate string.Template by using formatted string literals' updated_at = user = 'https://github.com/serhiy-storchaka' ``` bugs.python.org fields: ```python activity = actor = 'serhiy.storchaka' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'serhiy.storchaka' dependencies = [] files = ['44878'] hgrepos = [] issue_num = 28309 keywords = ['patch'] message_count = 1.0 messages = ['277690'] nosy_count = 3.0 nosy_names = ['georg.brandl', 'eric.smith', 'serhiy.storchaka'] pr_nums = [] priority = 'low' resolution = None stage = None status = 'open' superseder = None type = 'performance' url = 'https://bugs.python.org/issue28309' versions = ['Python 3.7'] ```

    serhiy-storchaka commented 7 years ago

    Proposed patch makes string.Template compiling template to formatted string literal. Since for now using formatted string literals is the fastest way of formatting strings, this significantly speeds up Template substitution.

    $ ./python -m perf timeit -s 'from string import Template; s = Template("$who likes $what")' -- 's.substitute(who="tim", what="ham")'

    Unpatched: Median +- std dev: 46.1 us +- 4.2 us Patched: Median +- std dev: 11.1 us +- 0.5 us

    The drawback is that compiling template adds high overhead.

    $ ./python -m perf timeit -s 'from string import Template' -- 's = Template("$who likes $what"); s.substitute(who="tim", what="ham")'

    Unpatched: Median +- std dev: 51.7 us +- 1.5 us Patched: Median +- std dev: 672 us +- 38 us

    The benefit of using compiled templates is achieved only if make at least 20 substitutions with the same template.

    Third-party template engines can use the same approach in Python 3.6+.

    iritkatriel commented 1 year ago

    For this to go forward, the patch needs to be converted into a GitHub PR.

    delta1513 commented 1 year ago

    From my own testing I noticed that the performance from the main branch vs the patch is identical and no improvements were made from this patch. Someone else may want to verify this in case I'm not exactly testing this correctly.

    akay25 commented 2 months ago

    I'm available to help, can I take on this?

    u2rafi commented 1 month ago

    @akay25 there is already a pull request for it https://github.com/python/cpython/pull/99177