Skip to content

Instantly share code, notes, and snippets.

@vzarytovskii
Created November 12, 2024 21:48
Show Gist options
  • Save vzarytovskii/cf31d28dad940299285b0a9b68fa4201 to your computer and use it in GitHub Desktop.
Save vzarytovskii/cf31d28dad940299285b0a9b68fa4201 to your computer and use it in GitHub Desktop.
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Configs
open BenchmarkDotNet.Environments
open BenchmarkDotNet.Jobs
open BenchmarkDotNet.Running
[<MemoryDiagnoser(displayGenColumns=false)>]
[<HideColumns("Job", "Error", "StdDev", "Median", "RatioSD")>]
type Benchmarks () =
let x = Some 3
[<Benchmark>]
member _.DoSomeStuffWithOptions () =
x
|> Option.map ((+) 1) // Used to allocate on the heap, now it doesn't.
|> Option.map ((+) -1) // Used to allocate on the heap, now it doesn't.
|> Option.defaultValue 0
ignore <| BenchmarkRunner.Run<Benchmarks>
(DefaultConfig.Instance
.AddJob(Job.Default.AsBaseline().WithRuntime CoreRuntime.Core80)
.AddJob(Job.Default.WithRuntime CoreRuntime.Core90))
| Method | Runtime | Mean | Ratio | Allocated | Alloc Ratio |
|----------------------- |--------- |----------:|------:|----------:|------------:|
| DoSomeStuffWithOptions | .NET 8.0 | 3.8792 ns | 1.00 | 48 B | 1.00 |
| DoSomeStuffWithOptions | .NET 9.0 | 0.6274 ns | 0.16 | - | 0.00 |
@vzarytovskii
Copy link
Author

Yeah, "side-effects" of inlining will oftentimes result in allocations being also inlined as opposed to being in their own functions. Like it happened to be the case for the Option module. We might wanna see how things are inlining in certain CEs or maybe Asyncs (I suspect it's not gonna be inlining too much).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment