MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
185 stars 19 forks source link

[Adjust rule] Drop SIM105 due to performance #91

Closed atombrella closed 2 years ago

atombrella commented 2 years ago

Desired change

Explanation

By conforming to this, the code will perform worse. Please check with timeit.

In [1]: %%timeit 
   ...: try: 
   ...:     a = {'a': 1} 
   ...:     a['b'] 
   ...: except KeyError: 
   ...:     pass 
   ...:                                                                                                                                                                                                    
199 ns ± 1.16 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [5]: %%timeit 
   ...: with contextlib.suppress(KeyError): 
   ...:     a = {'a': 1} 
   ...:     a['b'] 
   ...:                                                                                                                                                                                                    
621 ns ± 6.38 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
atombrella commented 2 years ago

I found this repository because I followed your tutorial on Medium on how to write a flake8 plugin. Thanks a bunch for this! :)

MartinThoma commented 2 years ago

Uh, interesting! I'll have a look and try to fix it this week.

Do you mind sharing the project where this time actually matters?

MartinThoma commented 2 years ago

Uh, I got your point wrong the first time : you're not worried about the runtime of sim105, but if the effect of the advocated change! Now I've got you! I'll document it tomorrow 👍

atombrella commented 2 years ago

Uh, interesting! I'll have a look and try to fix it this week.

Do you mind sharing the project where this time actually matters?

https://github.com/django/django/pull/9038 It's slow because of the way Python makes function calls.

MartinThoma commented 2 years ago

@atombrella What do you think about this? https://github.com/MartinThoma/flake8-simplify/pull/93