Optimize type checking range lookup with binary search#7
Conversation
|
Thanks for the contribution. I don’t think we should merge the binary-search optimization for The current implementation uses Even if the new implementation is correct, it adds complexity to code that is currently simple. It also makes the behavior harder to reason about in edge cases such as overlapping or nested ranges, because the binary-search version only checks one candidate range instead of naturally checking all ranges. From a KISS perspective, I don’t think the likely performance gain justifies the added complexity and maintenance cost here. The normalization change looks reasonable to me, though: precomputing the normalized Python file set avoids rebuilding it repeatedly and keeps the code simple. Could you please update the PR so it only includes the normalized file set change, and leaves the |
|
Sure. |
…ith binary search Now `_find_type_checking_ranges()` will return a range array sorted by start line, and the lineno for any import node will be matched using binary search with the help of the `bisect` library for faster lookup. Currently, this assumes that there are no overlapping ranges in the ranges array. Overlapping ranges can occur only when there are nested `if TYPE_CHECKING:` blocks.
Use `isinstance()` to ensure `node.lineno` is an integer before checking TYPE_CHECKING ranges. This avoids returning `Any` from `_in_type_checking()` and fixes the mypy `no-any-return` error.
Replaced the inline normalization of file paths with a precomputed set to improve performance and reduce redundant computations during graph extraction.
1ae06b8 to
bab0ec2
Compare
Pull Request
Key changes include:
Functional Improvements:
bisect_rightfor efficient range lookup in_in_type_checking, enhancing both correctness and performance.Code Style and Readability:
ruff formatandruff check --fixChange Type