| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
I can find one significant bug report open regarding inspect.getattr_static: Having studied the bug report, I don't think this patch impacts the bug (or any possible solutions to the bug) either way. |
Sorry, something went wrong.
|
Hello, Alex! |
Sorry, something went wrong.
There's not really any difference between the two; it's just a matter of taste. Some people consider vars(x) to be "more Pythonic" than accessing the __dict__ variable directly, but I don't really have a strong preference either way. In this case, the main reason I'm accessing it via .__dict__ is just because that's what the existing code already does, and I don't see any reason to change the style here. It's also arguably more explicit in this case -- but again, that's just a subjective opinion about code readability, really. |
Sorry, something went wrong.
There was a problem hiding this comment.
There are several things I can think of to speed up getattr_static.
def _check_instance(obj, attr):
try:
instance_dict = object.__getattribute__(obj, "__dict__")
except AttributeError:
return _sentinel
return dict.get(instance_dict, attr, _sentinel)if (_check_class(type(klass_result), '__get__') is not _sentinel and
_check_class(type(klass_result), '__set__') is not _sentinel):Ideally we can also call _check_class once with several attributes 🤔
Because in this case it will allow us to decrease the amount of _static_mro calls.
I think that a variable might be a bit faster
Sorry, something went wrong.
Those sound great! My instinct is to tackle them in a separate PR, so that each change can be evaluated and benchmarked independently. Would you like to submit a PR? |
Sorry, something went wrong.
|
Yes, I will do it tomorrow 👍 |
Sorry, something went wrong.
|
Would it be ok to have _dunder_dict_descriptor_get = type.__dict__["__dict__"].__get__ instead of _dunder_dict_descriptor = type.__dict__["__dict__"] in order to minimize the number of accesses to __get__ when iterating over the classes ? |
Sorry, something went wrong.
I considered it, but felt like it would make it significantly less readable (_dunder_dict_descriptor already isn't a great name, and _dunder_dict_descriptor_get feels even worse). Happy to reconsider if others agree that we should go that way, though. |
Sorry, something went wrong.
IMO, since we are already micro-optimizing an internal helper of an internal function, we may not necessarily need to be exact so perhaps we can name it _dunder_dict_of ? |
Sorry, something went wrong.
That's a decent name. Or maybe _get_dunder_dict_of_class. I'll make the change — thanks! |
Sorry, something went wrong.
|
I pushed the change and updated the benchmark results in the PR description (it's maybe a teeny tiny bit faster than it was, but not by much) |
Sorry, something went wrong.
I think the impact will be clearer when we have a complicated inheritance diagram (e.g., large projects with abstract interfaces and/or mixins here and there). |
Sorry, something went wrong.
There was a problem hiding this comment.
Very nice!
Sorry, something went wrong.
I tried these out locally, but unfortunately I can't measure any speedup from them :/ It's possible I'm not using the right benchmark to show a speedup, however -- happy to be proven wrong if you can get a speedup somewhere! |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Micro-optimising inspect._static_getmro and inspect._shadowed_dict leads to a significant improvement in the time taken to call isinstance() on a runtime-checkable protocol. (This is a useful benchmark, as it's a real-world use of inspect.getattr_static in a tight loop, that's found in the stdlib.)
Benchmark results on a0305c5:
Benchmark results with this PR: