<div dir="auto">Hi,</div><div dir="auto"><br></div><div dir="auto">Polymorphism is a performance killer. Normally this does not matter much, but for thin wrappers such as Optional, this is one of the most important factor.</div><div dir="auto"><br></div><div dir="auto">Behaviour polymorphism requires virtual dispatch and prevents inlining. This is really detrimental, as simple operations such as a getter which is previously only consisted of a memory load, in the presence of polymorphism would have to go through a 10-time more expensive virtual dispatch. Function calls, especially virtual ones, are also opaque and prohibit compiler optimisations.</div><div dir="auto"><br></div><div dir="auto">Layout polymorphism prevents direct accesses and requires indirection. This means that for every instance of Optional created a memory allocation is required. Optional is expected to be a near zero-cost abstraction in the presence of Value classes, so making it polymorphic is unacceptable.</div><div dir="auto"><br></div><div dir="auto">Thanks.</div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, 23 Aug 2023 at 22:43, Oleksii Kucheruk <<a href="mailto:iselo%2Bopenjdk@raccoons.co">iselo+openjdk@raccoons.co</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div dir="ltr">Hi there.<div>I have found that `java.util.Optional` is written procedural style and has `ifnonnull`  checks in each method. I propose to refactor `Optional` in accordance to OOP-style. This will eliminates all unnecessary `if`-statements, removes duplications and reduces bytecode size more then twice.</div><div><br></div><div>I have two solutions:</div><div>1. Completely dynamic that avoids single static `EMPTY` instance and unchecked casting of each `Optional.empty()`</div><div>2. Preserving original single static `EMPTY` per VM.</div><div><br></div><div>Also there are couple methods that throws NPE due to calling methods on null-objects and requires to add `Objects.requireNonNull(...)`.</div><div><br></div><div>OptionalInt, OptionalDouble, OptionalLong could be refactored same way even with remove of additional boolean variable `isPresent`.</div><div><br></div><div>Since I'm new here any guidance will be helpful.</div><div>Thank you in advance.</div></div>
</blockquote></div></div>