gh-119396: Optimize %S format of PyUnicode_FromFormat()#119412
Closed
vstinner wants to merge 1 commit intopython:mainfrom
Closed
gh-119396: Optimize %S format of PyUnicode_FromFormat()#119412vstinner wants to merge 1 commit intopython:mainfrom
vstinner wants to merge 1 commit intopython:mainfrom
Conversation
Add fast paths for str, int and float object types. Benchmark on %S and %R formats: +----------------+--------+----------------------+ | Benchmark | ref | change | +================+========+======================+ | str() | 654 ns | 556 ns: 1.18x faster | +----------------+--------+----------------------+ | repr() | 722 ns | 627 ns: 1.15x faster | +----------------+--------+----------------------+ | Geometric mean | (ref) | 1.16x faster | +----------------+--------+----------------------+
Member
Author
|
Benchmark: diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index f99ebf0dde..53d2a8e5f7 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -3312,6 +3312,46 @@ function_set_warning(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
Py_RETURN_NONE;
}
+static PyObject *
+bench_str(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
+{
+ PyObject *abc = PyUnicode_FromString("abc");
+ assert(abc != NULL);
+ PyObject *intobj = PyLong_FromLong(123);
+ assert(intobj != NULL);
+ PyObject *floatobj = PyFloat_FromDouble(1.0);
+ assert(floatobj != NULL);
+
+ PyObject *res = PyUnicode_FromFormat(
+ "str: %S, int: %S, float: %S",
+ abc, intobj, floatobj);
+
+ Py_DECREF(abc);
+ Py_DECREF(intobj);
+ Py_DECREF(floatobj);
+ return res;
+}
+
+static PyObject *
+bench_repr(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
+{
+ PyObject *abc = PyUnicode_FromString("abc");
+ assert(abc != NULL);
+ PyObject *intobj = PyLong_FromLong(123);
+ assert(intobj != NULL);
+ PyObject *floatobj = PyFloat_FromDouble(1.0);
+ assert(floatobj != NULL);
+
+ PyObject *res = PyUnicode_FromFormat(
+ "str: %R, int: %R, float: %R",
+ abc, intobj, floatobj);
+
+ Py_DECREF(abc);
+ Py_DECREF(intobj);
+ Py_DECREF(floatobj);
+ return res;
+}
+
static PyMethodDef TestMethods[] = {
{"set_errno", set_errno, METH_VARARGS},
{"test_config", test_config, METH_NOARGS},
@@ -3454,6 +3494,8 @@ static PyMethodDef TestMethods[] = {
{"check_pyimport_addmodule", check_pyimport_addmodule, METH_VARARGS},
{"test_weakref_capi", test_weakref_capi, METH_NOARGS},
{"function_set_warning", function_set_warning, METH_NOARGS},
+ {"bench_str", bench_str, METH_NOARGS},
+ {"bench_repr", bench_repr, METH_NOARGS},
{NULL, NULL} /* sentinel */
};
Script: import pyperf
import _testcapi
runner = pyperf.Runner()
runner.bench_func('str()', _testcapi.bench_str)
runner.bench_func('repr()', _testcapi.bench_repr) |
Member
Author
|
This change is not worth it:
I close the PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add fast paths for str, int and float object types.
Benchmark on %S and %R formats: