Skip to content

Optimize mypy.fastparse2 #5735

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

Merged
merged 12 commits into from
Oct 5, 2018
Merged

Optimize mypy.fastparse2 #5735

merged 12 commits into from
Oct 5, 2018

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Oct 4, 2018

These seem to speed up Python 2 type checking runtimes by up to
around 3%, when compiled with mypyc.

This is similar to #5722, but for Python 2. Also added some parser
tests, since Python 2 coverage was spotty. (It's still not quite at full
coverage, though.)

@JukkaL JukkaL requested a review from ilevkivskyi October 4, 2018 13:16
Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Looks good, I have some small suggestions.

if isinstance(s.s, bytes):
n = s.s
if isinstance(n.s, bytes):
s = n.s
Copy link
Member

Choose a reason for hiding this comment

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

I would use a longer name here instead of s, maybe string. Also s used in above methods for statements.

e = SuperExpr(member_expr.name, obj) # type: Expression
else:
e = member_expr
return self.set_line(e, n)
Copy link
Member

@ilevkivskyi ilevkivskyi Oct 4, 2018

Choose a reason for hiding this comment

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

I am curious, does this (using isinstance() with native classes in this method) give a visible perf win?

Also maybe it is worth adding a comment, otherwise it is not clear why we always create a MemberExpr instead of implementing this as it was before.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't measure the impact of each individual change, but this should be slightly faster since super expressions are much less common than member expressions, and the check for a super expression will be faster in the new implementation since we can do native attribute access (obj.callee and such). The impact of this particular change may not be measurable, but the overall reduction in Python attribute accesses gave a measurable aggregate performance increase in the Python 3 AST transformation case at least. Will add a comment.

if isinstance(arg, Name):
v = arg.id
elif isinstance(arg, ast27_Tuple):
v = '__tuple_arg_{}'.format(index + 1)
Copy link
Member

Choose a reason for hiding this comment

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

Should we add a test for this? Or do we already have one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We already had tests for Python 2 specific features such as this.

FuncDef:7(
g
Block:7(
PassStmt:8()))))
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding all these tests!

@JukkaL JukkaL merged commit 1606f0f into master Oct 5, 2018
@gvanrossum gvanrossum deleted the optimize-parse-py2 branch October 5, 2018 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants