Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance of PyObject_HasAttrString #104078

Closed
itamaro opened this issue May 2, 2023 · 3 comments · Fixed by #104079
Closed

Performance of PyObject_HasAttrString #104078

itamaro opened this issue May 2, 2023 · 3 comments · Fixed by #104079
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement

Comments

@itamaro
Copy link
Contributor

itamaro commented May 2, 2023

Feature or enhancement

PyObject_HasAttrString is implemented in terms of PyObject_GetAttrString / PyErr_Clear.
for non-existing attributes, there's significant overhead from creating an error only to clear it later.
this can be optimized by implementing it in terms of PyObject_HasAttr.

microbenchmark

python -m pyperf timeit -s '
import _testcapi
hasattr_string = _testcapi.hasattr_string

class A:
  def __init__(self):
    self.attr = 1

a = A()' 'hasattr_string(a, "noattr")'
.....................
Mean +- std dev: 487 ns +- 7 ns

Linked PRs

@itamaro itamaro added the type-feature A feature request or enhancement label May 2, 2023
@arhadthedev arhadthedev added performance Performance or resource usage interpreter-core (Objects, Python, Grammar, and Parser dirs) labels May 2, 2023
itamaro added a commit to itamaro/cpython that referenced this issue May 2, 2023
@ericvsmith
Copy link
Member

What are the before and after timings? Have you tested the impact on something other than a micro benchmark?

@JelleZijlstra
Copy link
Member

There appear to be no calls to PyObject_HasAttrString in CPython itself, other than internal extension modules. So it's unlikely there will be an effect on CPython's own performance. However, the function is part of the public API, so speeding it up may help some third-party extensions.

@itamaro
Copy link
Contributor Author

itamaro commented May 2, 2023

What are the before and after timings?

the before and after numbers are quoted in the PR description (the optimization is about 3.3X speedup).

Have you tested the impact on something other than a micro benchmark?

as @JelleZijlstra pointed out, this doesn't really show up within cpython itself. it showed up in Instagram profiling data due to internal and/or third-party usage (I don't have the data available to pinpoint it).
based on code search I see usage of this API in BoostPython & Cython for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants