Overhead of Returning Optional Values in Java and Rust

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • SharpLab

    .NET language playground

  • C# does indeed produce a rather tight assembly

    https://sharplab.io/#v2:EYLgtghglgdgNAFxBAzmAPgAgLACgACATAMx...

  • valhalla

    https://openjdk.org/projects/valhalla (by openjdk)

  • I reran the benchmarks with the latest Valhalla branch [1] and added a test that used OptionalLong instead of Optional.

    Without Valhalla

    OptionBenchmark.sumSimple avgt 5 328,110 us/op

    OptionBenchmark.sumNulls avgt 5 570,800 us/op

    OptionBenchmark.sumOptional avgt 5 2223,887 us/op

    OptionBenchmark.sumOptionalLong avgt 5 1201,987 us/op

    With Valhalla

    OptionBenchmark.sumSimpleValhalla avgt 5 327,927 us/op

    OptionBenchmark.sumNullsValhalla avgt 5 584,967 us/op

    OptionBenchmark.sumOptionalLongValhalla avgt 5 326,949 us/op

    OptionBenchmark.sumOptionalValhalla avgt 5 572,833 us/op

    OptionalLong is now as fast as the simple sum. And SumOptional is now identical so SumNulls

    [1] https://github.com/openjdk/valhalla

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • rust

    Empowering everyone to build reliable and efficient software.

  • > The language has no special support for them (beyond the try operator)

    In the interest of full and strict accuracy: see the enum’s full definition at https://github.com/rust-lang/rust/blob/d610b0c514b9ccb0dad5d.... There are a few points of specialness about Option that you can’t use elsewhere:

    • #[rustc_diagnostic_item = "Option"]: the compiler knows about Option for the purpose of improving its error messages. I’m not sure how this is used, and am not looking it up now.

    • #[lang = "None"] and #[lang = "Some"]: lang items allow the compiler to hook things up, like knowing the + operator maps to the core::ops::Add trait. https://github.com/search?q=repo%3Arust-lang%2Frust+%28optio... suggests that these lang items are only being used by Clippy, to provide better diagnostics. So if you use your own Option type, you won’t get Clippy lints related to Option.

    • I guess there are also the #[stable] attributes, which can’t be used outside the standard library. When talking about strict accuracy, I guess that counts!

    Anyway: for practical purposes this is just minor diagnostics and documentation stuff, not actual functionality, about which there is nothing special.

    Since you mentioned the try operator, might as well look at the trait implementations on Option too, https://doc.rust-lang.org/std/option/enum.Option.html#trait-.... There are a few things marked “This is a nightly-only experimental API.”, which you can implement yourself if you’re willing to take that stability risk; they’re all linked to try_trait_v2, which is what backs the try operator, `?`. Once it, or its successor, is stabilised, we’ll effectively be back to there being absolutely nothing special about Option, as you’ll be able to implement those traits for your own Option type as well.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts