Skip to content

Python: Allow absolute imports from source directory #5614

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

Conversation

tausbn
Copy link
Contributor

@tausbn tausbn commented Apr 6, 2021

#5506 revisited, and #5552 reverted.

To fix the performance issues, we greatly restrict the set of modules that have a new name under this scheme. The previous PR considered any file that coexisted with an executable Python script to be importable using its name as an absolute import. This PR limits itself to just those files that are actually imported.

One change to the tests was needed, which reflects the fact that the two main.py files no longer have the name main (which makes sense, since they're never imported under this name). Crucially, module is still a valid name for both of the module.py files

tausbn and others added 4 commits April 6, 2021 17:39
…ange"

This reverts commit 49d1937, reversing
changes made to d4877a9.
This greatly restricts the set of modules that have a new name under
this scheme.

One change to the tests was needed, which reflects the fact that the
two `main.py` files no longer have the name `main` (which makes sense,
since they're never imported under this name).
@tausbn tausbn added Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish no-change-note-required This PR does not need a change note labels Apr 6, 2021
@tausbn tausbn requested a review from a team as a code owner April 6, 2021 20:05
@github-actions github-actions bot added the Python label Apr 6, 2021
Copy link
Contributor

@yoff yoff left a comment

Choose a reason for hiding this comment

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

I would consider renaming maybeExecutedDirectly to isPossibleEntryPoint to unify the abstractions. I think it would make mayContainEntryPoint and transitively_imported_from_entry_point read as (even more) obvious.

@yoff
Copy link
Contributor

yoff commented Apr 7, 2021

Is an evaluation running?

@tausbn
Copy link
Contributor Author

tausbn commented Apr 7, 2021

I would consider renaming maybeExecutedDirectly to isPossibleEntryPoint to unify the abstractions. I think it would make mayContainEntryPoint and transitively_imported_from_entry_point read as (even more) obvious.

Ah, mayContainEntryPoint is now obsolete, and can safely be deleted, which I'll do.

I'll also change maybeExecutedDirectly to isPossibleEntryPoint while I'm at it. This also saves me the trouble of having to figure out if it's maybe... or mayBe... 🙂

@tausbn
Copy link
Contributor Author

tausbn commented Apr 7, 2021

Is an evaluation running?

Evaluation is running here: https://github.com/dsp-testing/tausbn-dca/tree/run/python-allow-absolute-imports-3/reports

@tausbn
Copy link
Contributor Author

tausbn commented Apr 7, 2021

Evaluation is done. No noticeable change in performance. It's possible that there's a slight slowdown (as this PR permits more analysis to be done), but I think it's so small it's lost in the noise.

@yoff
Copy link
Contributor

yoff commented Apr 7, 2021

Is this not supposed to be a statistically significant 3% slowdown? It might still be small enough that we allow it, but I thought the idea was that we could trust these numbers to not be lost in the noise?

@tausbn
Copy link
Contributor Author

tausbn commented Apr 7, 2021

Is this not supposed to be a statistically significant 3% slowdown? It might still be small enough that we allow it, but I thought the idea was that we could trust these numbers to not be lost in the noise?

¯\_(ツ)_/¯

I mean, the slowdown of (say) ~4% that corresponds to an 8 second increase in time may be statistically significant, but I wouldn't personally trust it off the bat. The larger projects seem more reliable in this regard (where, indeed, cpython which has now finished seems to indicate a non-negligible slowdown). I don't think this is slowdown that is possible to recoup, though. We are computing more stuff, after all.

@tausbn tausbn removed the Awaiting evaluation Do not merge yet, this PR is waiting for an evaluation to finish label Apr 8, 2021
@tausbn
Copy link
Contributor Author

tausbn commented Apr 8, 2021

I ran a further performance test, using the entire Code Scanning suite. Results seem to indicate a 4% slowdown overall. I think this is probably acceptable in the short run.

@tausbn tausbn requested a review from yoff April 9, 2021 17:23
Copy link
Contributor

@yoff yoff left a comment

Choose a reason for hiding this comment

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

Indeed, I am happy to trade a small performance penalty for less surprising and frustrating behaviour.

Do we know of any projects that might be pushed over the edge but this (so that we could attempt some targeted optimisations up-front rather than last-minute)?

@tausbn
Copy link
Contributor Author

tausbn commented Apr 12, 2021

Alright, in it goes.

@tausbn tausbn merged commit 6d4ddc0 into github:main Apr 12, 2021
@tausbn tausbn deleted the python-allow-absolute-imports-from-source-directory branch April 12, 2021 16:02
tausbn added a commit to tausbn/codeql that referenced this pull request Apr 27, 2021
Limits the behaviour of github#5614 in two ways:

First, we only consider files that are contained in the source archive.
This prevents unnecessary computation involving files in e.g. the
standard library.

Secondly, we ignore any relative imports (e.g. `from .foo import ...`),
as these only work inside packages anyway.

This fixes an observed performance regression on projects that include
`google-cloud-sdk` as part of their source code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants