Rules/gci22 remove unnecessary method calls - #165
Conversation
75af719 to
34c4be2
Compare
| String filePath = "src/GCI22/avoidUseOfMethodForBasicOperations.py"; | ||
| String ruleId = "creedengo-python:GCI22"; | ||
| String ruleMsg = "Avoid using methods for simple basic operations."; | ||
| int[] startLines = new int[]{9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 26, 30, 35, 36, 41, 42, 44, 45, 46, 47, 48, 51, 52, 57, 58, 59, 65, 70, 73, 76, 79, 82, 85 |
There was a problem hiding this comment.
Please use multi-line declaration like :
int[] startLines = new int[]{
9, 10, 11, 12, 13,
14, 17, 18, 19, 20,
21, 22, 26, 30, 35,
...
};| String ruleMsg = "Avoid using methods for simple basic operations."; | ||
| int[] startLines = new int[]{9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 26, 30, 35, 36, 41, 42, 44, 45, 46, 47, 48, 51, 52, 57, 58, 59, 65, 70, 73, 76, 79, 82, 85 | ||
| }; | ||
| int[] endLines = new int[]{9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 26, 30, 35, 36, 41, 42, 44, 45, 46, 47, 48, 51, 52, 57, 58, 59, 65, 70, 73, 76, 79, 82, 85 |
There was a problem hiding this comment.
Please use multi-line declaration like :
int[] endLines = new int[]{
9, 10, 11, 12, 13,
14, 17, 18, 19, 20,
21, 22, 26, 30, 35,
...
};|
|
||
| <!-- temporary version waiting for a real automatic release in creedengo repository --> | ||
| <creedengo-rules-specifications.version>3.0.0</creedengo-rules-specifications.version> | ||
| <creedengo-rules-specifications.version>rules-gci22-remove-unnecessary-method-calls-SNAPSHOT</creedengo-rules-specifications.version> |
|
Hi @Loup-K Good job and thanks for the PR ! Please consider the comments. One more question, why did you not check the rule for trivial wrappers ? It seems you only measure dunder methods. |
|
This PR has been automatically marked as stale because it has no activity for 30 days. |
|
Sorry for the delay. Exams and vacation have been on my mind for a while. As for the measurements, I focused on the Dunder methods because testing everything at once meant I couldn't really pinpoint the energy cost of code smells, and due to the academic schedule (timing tied to the course), I focused mainly on the first part. |
Summary
This Pull Request provides empirical energy measurements to justify the implementation of the rule GCI22 - The use of methods for basic operations for Python. The rule detects two anti-patterns that bypass Python's optimized bytecode instructions:
a.__add__(b)instead ofa + b,x.__eq__(y)instead ofx == y,my_list.__len__()instead oflen(my_list), etc.def add(a, b): return a + band calling it instead of using+directly at the call site.Calling
a.__add__(b)incurs additional overhead due to attribute lookup and method invocation, whereasa + bis compiled into a dedicated bytecode instruction (BINARY_OP) that dispatches directly through C-level slots, avoiding Python-level method resolution.Motivation
As part of my UNamur master's degree, I have to open a pull request on the creedengo repository and justify the developed rule with empirical and scientific methods.
Energy Measurements
Measurements were performed on Apple Silicon (macOS) using fstormacq/energyTracer, a cross-platform tool that captures fine-grained per-component energy samples (CPU, GPU, ANE/CO2 eq., DRAM) for a given process.
Code Under Test
The code under test for this experiment was the following:
With the code smell (dunder method calls)
Without the code smell (native operators)
Plots
Comparisons
Box plots
Violins
Analysis
cpu_mjgpu_mjdram_mjtime_sKey Takeaways
Conclusion
The rule appears to show a noticeable impact when the code smell is repeated a large number of times. However, the relevance of the rule can still be questioned, since the likelihood of this rule actually being triggered in a programmer’s code is relatively low. Even for beginners, it is quite uncommon to write code containing this kind of code smell. In conclusion, the rule may not be as relevant as it initially seems. It depends on how much importance we assign to the energy impact of a rule compared with the importance of how frequently the rule is used.
Dataset
The raw measurements and plots are publicly available on Zenodo:
How to Reproduce
Measurements can be reproduced using fstormacq/energyTracer on any supported platform.