-
-
Notifications
You must be signed in to change notification settings - Fork 366
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
Simplify asdict and has. #48
Conversation
ef05b51
to
91568d7
Compare
Current coverage is 100% (diff: 100%)@@ master #48 diff @@
===================================
Files 7 7
Lines 368 364 -4
Methods 0 0
Messages 0 0
Branches 86 86
===================================
- Hits 368 364 -4
Misses 0 0
Partials 0 0
|
return False | ||
else: | ||
return True | ||
return hasattr(cl, "__attrs_attrs__") |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
I don't think I made any behavioral changes, so I can just mention the speedup in the changelog. A question though. Why does |
Yeah I was kind of overly careful. I kind of still see it's value for user-facing stuff (maybe people don’t realize literally everything breaks if they fuck around with the returned Attributes?) but it def shouldn’t be used for internal stuff. IOW if you find more usages feel free to swap them out. :) |
Alright, review comments applied. Why don't we just make the attributes themselves immutable? Then sharing a tuple of immutable objects would always be safe. (Well, not many things in Python are truly immutable, but within reason.) |
Sure that would work too. I didn’t spend too much time thinking about that back then. |
So, as you might have noticed, I'm totally a fan of this library :) Also, we've been using it at work a lot lately, and it's really working out for us. So I thought I might see if anything can be optimized a little.
We use
asdict
a lot. I actually totally love the way attrs interacts with data contained in Python primitives, like dicts and lists. At a later date I might submit some converters that make these transformations really easy. However, now I was taking a look atasdict
performance, to see if it can be optimized.Actually my first instinct was to see if I can use Cython to speed things up, mostly because I wanted to learn Cython a little more. The changes I'm submitting now have a far greater effect than using Cython though, so I'm not sure Cython's worth it. Maybe later?
Anyway, on to measuring. I'm using the
perf
library as a more reliable, fancy timeit. (Newest version:pip install git+https://github.com/haypo/perf.git#egg=perf
) We actually have a way of generating attrs classes, in tests: the nested_classes Hypothesis strategy. We can use a pre-seeded Random instance to make it reproducible.For example, seeding with the number 1, pre-changes:
Post changes:
So that's a change of 513 us to 21.4 us. We can get more examples by seeding the Random instance with other numbers. Seeding with 2 will produce a larger class, and the difference will be 1.74 ms +- 0.03 ms to 72.9 us +- 1.4 us.
I think you'll find the changes are reasonable. :)