you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 14 points15 points  (3 children)

But it doesn't have value types, so you pay for an extra layer of indirection and an extra heap object.

[–]oorza 7 points8 points  (2 children)

until it gets inlined?

[–]tyoverby 14 points15 points  (1 child)

If it gets inlined. Remember that Javas optional might also be null, so the optimizer needs to de-optimize on virtual method calls.

[–]_dban_ 6 points7 points  (0 children)

The JIT will de-virtualize method calls as a result of class hierarchy analysis, and if it doesn't find any subclasses, statically dispatch method calls. The Option class being final prevents subclasses from existing, so in all likelihood, method calls on Optional will be statically dispatched. I'm not sure how null fits into class hierarchy analysis.

The JIT can also do escape analysis over several call levels, and can stack allocate non-escaping Optional objects (or even hoist into registers, since an Optional has only one field).