Rules/gci22 remove unnecessary method calls#165
Open
Loup-K wants to merge 4 commits into
Open
Conversation
75af719 to
34c4be2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.