You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
⚡️ Speed up function should_modify_pyproject_toml by 48% in PR #102 (ruff-format)
To optimize the provided Python script, I have made some improvements to the performance and memory efficiency. made.
1. Moved imports inside functions when they are not needed globally to reduce initial load time and potentially minimize memory footprint.
2. Changed `tomlkit.exceptions.NonExistentKey` to `KeyError` as `tomlkit` does not have `NonExistentKey` and works with standard `KeyError`.
3. Replaced multiple dictionary lookup operations with dict's `get` method where appropriate.
4. Used list and dictionary comprehensions where possible for better performance.
Explanation.
- Imports: Restricted global imports and moved the `import` statements within the functions where they are needed.
- Dictionary lookups: Improved dictionary lookups by making use of `.get()` method which is slightly faster when checking for variable existence and using comprehensions.
- Combined certain for loops and conditionals reducing code redundancy and enhancing readability.
- Replaced the custom `tomlkit.exceptions.NonExistentKey` by `KeyError`.
The code will function in the same way as before but now should be slightly more efficient.
0 commit comments