-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Avoid Optional.orElse(expression) #15301
Conversation
058d616
to
d7e8025
Compare
core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/GatherAndMergeWindows.java
Outdated
Show resolved
Hide resolved
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.
Majority of those cases should not be altered. The orElseGet
should be used only when some actual work needs to be executed. Getters and trivial constructors are not expensive at all, creating lambdas is.
If any of places here are actual bottlenecks to the point that this is needed, we should get rid of Optional
class first.
core/trino-main/src/main/java/io/trino/sql/planner/iterative/rule/GatherAndMergeWindows.java
Outdated
Show resolved
Hide resolved
Besides |
d7e8025
to
3083487
Compare
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.
I've put (X) sign next to every change that IMHO should be rollbacked.
This is just my opinion, based on some experience, but opinion nonetheless.
The common misconception that you are following here is that object creation in Java is expensive. It is actually super cheap, especially if the object is short-lived.
Your approach is what I call a "defensive performance programming" which is by many considered the "root of all evil" just because they quote a book by Donald Knuth that they haven't even read. But everyone uses lazy string concatenation in logging statements which is exactly that.
I am actually a fan of this approach, but a bit more orthodox. I would just get rid of most of Optionals in the first place.
To sum up, the answer to the question "where to use eager and lazy orElse
" is very simple - it depends.
client/trino-jdbc/src/main/java/io/trino/jdbc/TrinoDriverUri.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/execution/SetTimeZoneTask.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/metadata/PolymorphicScalarFunction.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/security/AccessControlManager.java
Outdated
Show resolved
Hide resolved
...n/trino-hive/src/main/java/io/trino/plugin/hive/aws/athena/projection/IntegerProjection.java
Show resolved
Hide resolved
plugin/trino-hive/src/main/java/io/trino/plugin/hive/parquet/ParquetPageSourceFactory.java
Outdated
Show resolved
Hide resolved
...trino-hive/src/main/java/io/trino/plugin/hive/security/SqlStandardAccessControlMetadata.java
Outdated
Show resolved
Hide resolved
plugin/trino-hudi/src/main/java/io/trino/plugin/hudi/HudiPageSourceProvider.java
Outdated
Show resolved
Hide resolved
The empty result constructor is often used in context of `Optional.orElse(...)`, so it's better if it is cheap.
client/trino-jdbc/src/main/java/io/trino/jdbc/TrinoDriverUri.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/metadata/PolymorphicScalarFunction.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/operator/join/LookupJoinOperator.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/execution/SetTimeZoneTask.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/sql/planner/SplitSourceFactory.java
Outdated
Show resolved
Hide resolved
1b9a7b8
to
e28cbdb
Compare
9c66cb8
to
06dab22
Compare
Applied most comments. Thanks for your review @skrzypo987 ! |
Evaluate option's alternative with `orElseGet` rather than eagerly with `orElse(expression)`, when expression is expensive to evaluate. Here "expensive" means anything that can run a validation, i.e. is NOT constant NOR assessor, NOR a factory method of a simple type (like `Optional`, `ImmutableList`, and similar).
06dab22
to
dd2b7f4
Compare
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.
lgtm
Evaluate option's alternative with
orElseGet
rather than eagerly withorElse(expression)
, when expression is expensive to evaluate. Here "expensive" means anything that can run a validation, i.e. is NOT constant NOR assessor, NOR a factory method of a simple type (likeOptional
,ImmutableList
, and similar).