python-attrs / python-attrs/attrs
attrs.astuple() leaves performance on the table compared to operator.attrgetter
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
Description
I played around and found out that attrs.astuple is quite slow in comparison to using operator.attrgetter with all the field names.
Maybe the performance of attrs.astuple(recurse=False) can be improved, benefiting all library users?
Here is an example, save as .ipy file and open with ipython (I couldn't be bothered to fiddle with the timeit module manually):
from attrs import define
import attrs
import operator
@define
class A:
a: int = 1
b: int = 2
c: int = 3
d: int = 4
e: int = 5
f: int = 6
def faster_astuple(x):
return operator.attrgetter(*(f.name for f in attrs.fields(type(x))))(x)
a = A()
print(attrs.astuple(a))
print(faster_astuple(a))
%timeit attrs.astuple(a, recurse=False)
%timeit faster_astuple(a)
output on my machine:
(1, 2, 3, 4, 5, 6)
(1, 2, 3, 4, 5, 6)
5.6 µs ± 159 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
1.35 µs ± 28.4 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
Speedup is around a factor of four.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the attrs.astuple entry point and reproduce the supplied IPython benchmark, focusing on recurse=False. Compare the current behavior with the operator.attrgetter example and verify that any optimization preserves the tuple output and non-recursive semantics while improving the measured performance.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- performance
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100