Skip to content

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

Merged

Conversation

Carltoffel
Copy link
Member

With this PR I tried to reduce redundancy, primary by using the optval function.

Some discoveries I made while working on this:

  • In stdlib_io the open statement is used with an optional iostat argument. Since open is an intrinsic function and not a routine, the optional argument cannot be directly passed, which leads to the open 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 the iostat argument of the open statement. If the optional iostat argument of the open function is present, the result will then be written to the optional argument.
  • The simps_weights function (stdlib_quadrature_simps.fypp) contains an nested if statement which could be rewritten as select case ( optval(even, 0) ) .... I'm not sure if this would improve the code, but it would reduce the if-nesting by one if.
  • stdlib_stat_moment has some functions which could be rewritten using the optval function, but as I mentioned in the commit message this is inefficient. Instead I introduced a new variable which de-duplicates the formular.
  • OT: I saw a min(max(...)) construct which could be replaced with clip... I guess there are more of these, so maybe we should search and replace them where possible just like I did with present() and optval() in this PR.

closes #524

Carl Burkert added 6 commits September 17, 2021 18:38
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.
Copy link
Member

@milancurcic milancurcic left a 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.

Copy link
Member

@jvdp1 jvdp1 left a 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?

@gareth-nx
Copy link
Contributor

I'm cautious about this pull request, because I don't understand the performance implications of generally replacing present with a function call. See a few comments here -- just wanting to check whether we understand the implications.

@awvwgk awvwgk added refactoring Internal change for better maintainablility reviewers needed This patch requires extra eyes labels Sep 18, 2021
Copy link
Member

@jvdp1 jvdp1 left a 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 .

@milancurcic
Copy link
Member

Let's merge this tomorrow (Sep 21) if there are no objections.

@milancurcic milancurcic merged commit 82f8c8d into fortran-lang:master Sep 21, 2021
@Carltoffel Carltoffel deleted the replace-ifelse-with-optval branch September 22, 2021 05:41
@Carltoffel
Copy link
Member Author

Thank you all for the respectful discussion about the performance. I've learned a lot!

@awvwgk awvwgk removed the reviewers needed This patch requires extra eyes label Sep 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Internal change for better maintainablility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite parts of stdlib where optval could be used
5 participants