Skip to content

bpo-44525: Specialize for calls to type and other builtin classes with 1 argument. - #29942

Merged
markshannon merged 5 commits into
python:mainfrom
faster-cpython:specialize-calls-to-builtin-types
Dec 15, 2021
Merged

markshannon merged 5 commits into
python:mainfrom
faster-cpython:specialize-calls-to-builtin-types

Conversation

@markshannon

@markshannon markshannon commented Dec 6, 2021 •

Copy link
Copy Markdown
Member

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code LGTM. Interested to see the benchmark results.

@markshannon

Copy link
Copy Markdown
Member Author

Benchmark results show a slight speedup, but very close to noise.

I think this is worth including because:

  1. The nbody and spectral norm benchmarks show decent speedups and have range(arg) calls in the hot parts of the benchmark, suggesting that specialization of calls to cls(one_arg) is effective.
  2. Some "real" code includes a lot of type(obj) calls (at least statically).

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Comment thread Python/ceval.c
assert(GET_CACHE()->adaptive.original_oparg == 1);
PyObject *obj = TOP();
PyObject *callable = SECOND();
DEOPT_IF(callable != (PyObject *)&PyType_Type, CALL_NO_KW);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember if you like using the Py_IS_TYPE inline function, but just mentioning it here and leaving it up to you :).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll skip on the obfuscation macro 🙂

Comment thread Python/specialize.c
return -1;
}
if (nargs == 1) {
if (tp == &PyType_Type) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

@markshannon

Copy link
Copy Markdown
Member Author

Increases CALL_NO_KW hit rate from ~71% to ~74% on the standard benchmark suite, with only a slight increase in the miss rate.
Reduces failed specializations for calls to classes (SPEC_FAIL_CLASS) by over half.

@markshannon
markshannon merged commit 3a60bfe into python:main Dec 15, 2021
@markshannon
markshannon deleted the specialize-calls-to-builtin-types branch September 26, 2023 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants