reef-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Won Wook SONG (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (REEF-1390) Make Optional's toString() and hashCode() computed on demand
Date Mon, 16 May 2016 05:09:12 GMT

    [ https://issues.apache.org/jira/browse/REEF-1390?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15284149#comment-15284149
] 

Won Wook SONG commented on REEF-1390:
-------------------------------------

Maybe following [this reference](http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html)?

> Make Optional's toString() and hashCode() computed on demand
> ------------------------------------------------------------
>
>                 Key: REEF-1390
>                 URL: https://issues.apache.org/jira/browse/REEF-1390
>             Project: REEF
>          Issue Type: Improvement
>          Components: REEF
>            Reporter: WooYeonLee
>            Assignee: Won Wook SONG
>            Priority: Minor
>              Labels: easyfix, performance
>
> {{Optional}}'s constructor preloads {{valueStr}} and {{valueHash}} for its {{toString()}}
and {{hashCode()}} methods respectively, even when those methods are never invoked during
the life of the object.
> {code}
>   private Optional(final T value) {
>     this.value = value;
>     this.valueStr = "Optional:{" + value + "}";
>     this.valueHash = value.hashCode();
>   }
> {code}
> It incurs unnecessary overhead, and the problem becomes worse if the size of value is
huge and T's hashcode() requires heavy computation.
> They should be done in {{toString()}} and {{hashCode()}} methods on demand, respectively
like below:
> {code}
>   private Optional(final T value) {
>     this.value = value;
>   }
>   @Override
>   public String toString() {
>     return "Optional:{" + this.value + "}";
>   }
>   @Override
>   public int hashCode() {
>     return this.value.hashCode();
>   }
> {code}
> +As a related issue, when {{value}} is null, it's better to return a reference of a {{static}}
variable for empty Optional, not making separate objects every time. The following is the
current code.
> {code}
>   public static <T> Optional<T> empty() {
>     return new Optional<>();
>   }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message