Repository navigation
argparse: remove redundant len() #104273
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on May 7, 2023 hello @buraksaler!
it is not clear from this issue what
len()calls you are referring to and why they are redundant.looking at the linked PR, I'm inferring you refer to the argparse module, where you suggest replacing 3
len()calls with one.
while the change looks functionally correct to me, any change has inherent risks, so making such a change should be justifiable.
can you explain the benefits of making this change? it looks like the intention could have been improving performance by caching the result of a function call, in which case, you should provide benchmarking results (micro or macro) or profiling data showing the improvement from making this change.Reacted by Alex Waygood and Burak Saler- changed the title
[-]redundant len() calling[/-][+]argparse: remove redundant len()[/+]on May 7, 2023 Justification:
indentis a parameter of the helper function get_lines. It is not rebound within the function. Its length is therefore a constant within the function. Hencelen(indent)is only needed once.Calculating common subexpressions just once is a common optimization. I don't think benchmarking is needed.
Reacted by Itamar Oren and Burak Saler- addedperformancePerformance or resource usagePerformance or resource usageand removedtype-featureA feature request or enhancementA feature request or enhancement
on May 7, 2023 I guess I'm okay with the specific PR, but in general we discourage microoptimisations in cold code, especially if done without any kind of benchmarking. These cost reviewer time, can hurt readability and if poorly done run the risk of regressions. I certainly wouldn't welcome PRs doing CSE wherever possible for calls to len in the standard library.
You'd need several millions of lines of help for this to add up to anything meaningful.
Reacted by Alex Waygood, Jelle Zijlstra, Burak Saler and Erlend E. Aasland- removedperformancePerformance or resource usagePerformance or resource usage
on May 7, 2023 - added a commit that references this issue
on May 9, 2023
Feature or enhancement
I decreased calling of redundant len() function.
Pitch
(Explain why this feature or enhancement should be implemented and how it would be used.
Add examples, if applicable.)
Previous discussion
Linked PRs