This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Accelerate string.Template by using formatted string literals
Type: performance Stage:
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, georg.brandl, serhiy.storchaka
Priority: low Keywords: patch

Created on 2016-09-29 09:01 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
faster_template.patch serhiy.storchaka, 2016-09-29 09:02
Messages (1)
msg277690 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-09-29 09:01
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+.
History
Date User Action Args
2022-04-11 14:58:37adminsetgithub: 72496
2016-09-29 09:02:14serhiy.storchakasetfiles: + faster_template.patch
keywords: + patch
2016-09-29 09:01:26serhiy.storchakacreate