-
Notifications
You must be signed in to change notification settings - Fork 191
Reduce redundancy caused by optional arguments #528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce redundancy caused by optional arguments #528
Conversation
This commit replaces the if-else-block handling the optional prefix arguments. The colon which was previously appended, if the prefix argument was present, is only appended if the prefix argument contains any characters. Previously, prefix='' would end up getting extended to pref=': '.
The optional advance argument leads to a redundant call of read/write routines which can be deduplicated by making use of the optval-function.
The optval function replaces some nested if statemens and allows to remove the reverse_ variable which is a clone of the optional reverse variable.
By using the optval function only one write operation to the si variable is necessary.
The if-else-blocks in the moment_all functions couldn't be replaced with the optval function, because this would lead to poor performance. If the center variable is present, the calculation of the mean can be skipped, but the optval function prevents this optimisation.
Each routine using the optional 'back' argument could be shortend to a single line of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Carltoffel, this all looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for these improvements. Regarding stdlib_stats_moment
, I wonder if using optval()
will not affect its efficiency?
I'm cautious about this pull request, because I don't understand the performance implications of generally replacing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on all discussions in this PR, and in #524, I approve this PR as it is currently. Thank you @Carltoffel .
Let's merge this tomorrow (Sep 21) if there are no objections. |
Thank you all for the respectful discussion about the performance. I've learned a lot! |
With this PR I tried to reduce redundancy, primary by using the
optval
function.Some discoveries I made while working on this:
stdlib_io
theopen
statement is used with an optionaliostat
argument. Sinceopen
is an intrinsic function and not a routine, the optional argument cannot be directly passed, which leads to theopen
statement being defined twice. If it doesn't cause too much overhead I suggest to declare an extra variable which will always be passed to theiostat
argument of theopen
statement. If the optionaliostat
argument of theopen
function is present, the result will then be written to the optional argument.simps_weights
function (stdlib_quadrature_simps.fypp
) contains an nestedif
statement which could be rewritten asselect case ( optval(even, 0) ) ...
. I'm not sure if this would improve the code, but it would reduce theif
-nesting by oneif
.stdlib_stat_moment
has some functions which could be rewritten using theoptval
function, but as I mentioned in the commit message this is inefficient. Instead I introduced a new variable which de-duplicates the formular.min(max(...))
construct which could be replaced withclip
... I guess there are more of these, so maybe we should search and replace them where possible just like I did withpresent()
andoptval()
in this PR.closes #524