-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Inline funs that are immediately used #4545
Merged
bjorng
merged 1 commit into
erlang:master
from
bjorng:bjorn/compiler/optimize-fun-calls
Feb 26, 2021
Merged
Inline funs that are immediately used #4545
bjorng
merged 1 commit into
erlang:master
from
bjorng:bjorn/compiler/optimize-fun-calls
Feb 26, 2021
Conversation
This file contains 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
3b6198f
to
4d280f8
Compare
jhogberg
approved these changes
Feb 24, 2021
Neat! Quick question: does that work if the function is invoked once but across multiple clauses? For example: Fun = fun(B) -> A = 21, 2 * B end(),
case Condition of
true -> Fun(1);
false -> Fun(-1)
end. |
No. |
Funs can be used to hide local variables. Example: a() -> A = fun() -> A = 21, 2 * A end(), foo:bar(A). To avoid the slight runtime cost for such use of funs, teach the compiler to inline funs that are used only once immediately after creation. Resolves erlang#4019.
d1c5299
to
2c9fd89
Compare
5bdbb78
to
72675ba
Compare
This was referenced Mar 9, 2021
bjorng
added a commit
to bjorng/otp
that referenced
this pull request
Jan 13, 2022
Consider this module: -module(bug). -export([foo/0]). foo() -> fun(a) -> ok end(b). The call to the fun will always fail, which will be noted by the compiler: 1> c(bug). bug.erl:5:5: Warning: no clause will ever match % 5| fun(a) -> ok end(b). % | ^ {ok,bug} What is unexpected is that the exception that is raised is not a `function_clause` exception: 2> bug:foo(). ** exception error: no case clause matching {b} in function bug:foo/0 (bug.erl, line 5) This confusing `case_clause` exception started to appear in OTP 24 because of 72675ba (erlang#4545) that inlines funs that are immediately used. Before OTP 24, when the fun was not inlined, the exception would be: 2> bug:foo(). ** exception error: no function clause matching bug:'-foo/0-fun-0-'(b) (bug.erl, line 5) The reason that `function_clause` exceptions are rewritten to `case_clause` exceptions in inlined code is to avoid the even more confusing and misleading exception: 2> bug:foo(). ** exception error: no function clause matching bug:foo(b) (bug.erl, line 5) This is confusing because it seems that `foo/0` was called with one argument. To reduce the confusion, this commmit ensures that `function_clause` exceptions in inlined code remains `function_clause` exceptions, but with a generated name that makes it clear that the `function_clause` exception occurred in a fun: 2> bug:foo(). ** exception error: no function clause matching bug:'-foo/0-inlined-0-'(b) (bug.erl, line 5) Fixes erlang#5513
bjorng
added a commit
to bjorng/otp
that referenced
this pull request
Jan 14, 2022
Consider this module: -module(bug). -export([foo/0]). foo() -> fun(a) -> ok end(b). The call to the fun will always fail, which will be noted by the compiler: 1> c(bug). bug.erl:5:5: Warning: no clause will ever match % 5| fun(a) -> ok end(b). % | ^ {ok,bug} What is unexpected is that the exception that is raised is not a `function_clause` exception: 2> bug:foo(). ** exception error: no case clause matching {b} in function bug:foo/0 (bug.erl, line 5) This confusing `case_clause` exception started to appear in OTP 24 because of 72675ba (erlang#4545) that inlines funs that are immediately used. Before OTP 24, when the fun was not inlined, the exception would be: 2> bug:foo(). ** exception error: no function clause matching bug:'-foo/0-fun-0-'(b) (bug.erl, line 5) The reason that `function_clause` exceptions are rewritten to `case_clause` exceptions in inlined code is to avoid the even more confusing and misleading exception: 2> bug:foo(). ** exception error: no function clause matching bug:foo(b) (bug.erl, line 5) This is confusing because it seems that `foo/0` was called with one argument. To reduce the confusion, this commmit ensures that `function_clause` exceptions in inlined code remains `function_clause` exceptions, but with a generated name that makes it clear that the `function_clause` exception occurred in a fun: 2> bug:foo(). ** exception error: no function clause matching bug:'-foo/0-inlined-0-'(b) (bug.erl, line 5) Fixes erlang#5513
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.
Funs can be used to hide local variables. Example:
To avoid the slight runtime cost for such use of funs, teach the
compiler to inline funs that are used only once immediately after
creation.
Resolves #4019