Skip to content

Change BaseFrame.__setattr__ to be fast for private attributes. - #6940

Merged
eteq merged 1 commit into
astropy:masterfrom
mhvk:baseframe-trivial-huge-speedup
Dec 22, 2017
Merged

eteq merged 1 commit into
astropy:masterfrom
mhvk:baseframe-trivial-huge-speedup

Conversation

@mhvk

@mhvk mhvk commented Dec 7, 2017

Copy link
Copy Markdown
Contributor

Add one line and get a speed-up of more than a factor 3 in slicing; not bad!

But is also shows the risk inherent in overriding __getattr__ and __setattr__. Here, just to protect the user from accidentally writing an attribute with the same name as that of a representation coordinate, we slowed down things considerably.

And of course this is partially why I'm hesitant to try to paper over simple mistakes such as this by caching yet more stuff (though caching the representation names set seems an obvious next fix).

import astropy.units as u
from astropy.coordinates import ICRS
c = ICRS(np.arange(30.)*u.deg, np.arange(30.)*u.deg)
%timeit c[1:2]
# with current master 1000 loops, best of 3: 579 µs per loop
# with this PR: 10000 loops, best of 3: 185 µs per loop

@astropy-bot

astropy-bot Bot commented Dec 7, 2017 •

Copy link
Copy Markdown

Hi there @mhvk 👋 - thanks for the pull request! I'm just a friendly 🤖 that checks for issues related to the changelog and making sure that this pull request is milestoned and labeled correctly. This is mainly intended for the maintainers, so if you are not a maintainer you can ignore this, and a maintainer will let you know if any action is required on your part 😃.

Everything looks good from my point of view! 👍

If there are any issues with this message, please report them here.

@mhvk mhvk added this to the v3.0.0 milestone Dec 7, 2017
for representation_attr in self.representation_info.values():
repr_attr_names.update(representation_attr['names'])

if attr in repr_attr_names:

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 logic has changed in that this block is now inside the above if hasattr(..) block. Are you sure this is correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, since we know that without that block, repr_attr_names is an empty set.

@taldcroft

Copy link
Copy Markdown
Member

Looks great, but you still need another factor of 6 to get to the speed of the template idea (and I would need to re-profile that with this fix in place). 😄

@mhvk

mhvk commented Dec 7, 2017

Copy link
Copy Markdown
Contributor Author

Test failure is unrelated (open file of some web thingy).

@mhvk

mhvk commented Dec 7, 2017

Copy link
Copy Markdown
Contributor Author

@taldcroft - see #6941 for a factor 50(!).

p.s. Just in case: unlike for #6941, for this PR I have no qualms about implementation at all; it should just go in, as it speeds up without any downsides.

@mhvk

mhvk commented Dec 21, 2017

Copy link
Copy Markdown
Contributor Author

@adrn - would you be able to do a quick review? This small change is not controversial, and while it is about performance, there seems to be no reason not to have it in 3.0.

@eteq eteq 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.

Agree this is not controversial, so will merge now.

@eteq
eteq merged commit 584ecf3 into astropy:master Dec 22, 2017
@bsipocz

bsipocz commented Dec 22, 2017

Copy link
Copy Markdown
Member

Can we also backport it to LTS?

@mhvk

mhvk commented Dec 22, 2017

Copy link
Copy Markdown
Contributor Author

@bsipocz - I guess no real reason not to. In a way it was a bug.

@bsipocz bsipocz modified the milestones: v3.0.0, v2.0.4 Dec 22, 2017
bsipocz pushed a commit that referenced this pull request Dec 22, 2017
Change BaseFrame.__setattr__ to be fast for private attributes.
@mhvk
mhvk deleted the baseframe-trivial-huge-speedup branch December 20, 2018 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants