Skip to content

Nullable<'T>.ValueNullable<'T>.GetValueOrDefault - #17504

Merged
T-Gro merged 3 commits into
dotnet:mainfrom
brianrourkeboll:getvalueordefault
Aug 12, 2024
Merged

Nullable<'T>.ValueNullable<'T>.GetValueOrDefault#17504
T-Gro merged 3 commits into
dotnet:mainfrom
brianrourkeboll:getvalueordefault

Conversation

@brianrourkeboll

@brianrourkebollbrianrourkeboll commented Aug 7, 2024

Copy link
Copy Markdown
Contributor

Description

  • We are already checking Nullable<'T>.HasValue before accessing Nullable<'T>.Value, so it is safe to call Nullable<'T>.GetValueOrDefault. Nullable<'T>.Valuedoes a redundant check of HasValue, while Nullable<'T>.GetValueOrDefaultdoes not. Using GetValueOrDefault is significantly faster when no value is present.

Note: these functions are marked inline — does this change have any binary compatibility implications?

Benchmarks

MethodMeanErrorStdDevRatioRatioSD
Value_NoValue0.2210 ns0.0140 ns0.0117 ns1.000.07
GetValueOrDefault_NoValue0.0368 ns0.0106 ns0.0089 ns0.170.04
Value_Value3.1128 ns0.0731 ns0.0610 ns1.000.03
GetValueOrDefault_Value3.0675 ns0.0107 ns0.0084 ns0.990.02
Source
moduleOfNullable.ProgramopenSystemopenBenchmarkDotNet.AttributesopenBenchmarkDotNet.ConfigsopenBenchmarkDotNet.Running[<GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)>]typeBenchmarks()=letnoValue= Nullable<int>()letvalue= Nullable 3[<Benchmark(Baseline =true)>][<BenchmarkCategory("NoValue")>]member_.Value_NoValue()= ValueOption.ofNullable noValue
[<Benchmark>][<BenchmarkCategory("NoValue")>]member_.GetValueOrDefault_NoValue()=if noValue.HasValue then ValueSome (noValue.GetValueOrDefault ())else ValueNone
[<Benchmark(Baseline =true)>][<BenchmarkCategory("Value")>]member_.Value_Value()= ValueOption.ofNullable value
[<Benchmark>][<BenchmarkCategory("Value")>]member_.GetValueOrDefault_Value()=if value.HasValue then ValueSome (value.GetValueOrDefault ())else ValueNone
ignore (BenchmarkRunner.Run<Benchmarks>())

Checklist

  • Test cases added.
  • Performance benchmarks added in case of performance changes.
  • Release notes entry updated.

* We are already checking `Nullable.HasValue` before accessing
`Nullable.Value`, so it is safe to call `Nullable.GetValueOrDefault`,
which does not do an additional check, unlike `Nullable.Value`.
@github-actions

github-actionsBot commented Aug 7, 2024

Copy link
Copy Markdown
Contributor

⚠️ Release notes required, but author opted out

Warning

Author opted out of release notes, check is disabled for this pull request.
cc @dotnet/fsharp-team-msft

@vzarytovskiivzarytovskii added the NO_RELEASE_NOTES Label for pull requests which signals, that user opted-out of providing release notes label Aug 7, 2024
@vzarytovskii

Copy link
Copy Markdown
Member

That's nice, thanks. I didn't even know about this API existence.

@brianrourkeboll
brianrourkeboll marked this pull request as ready for review August 7, 2024 17:49
@brianrourkeboll
brianrourkeboll requested a review from a team as a code ownerAugust 7, 2024 17:49

@psfinakipsfinaki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment threadsrc/FSharp.Core/option.fs
@KevinRansom
KevinRansom enabled auto-merge (squash) August 9, 2024 16:53
auto-merge was automatically disabled August 12, 2024 08:12

Pull request was closed

@T-Gro
T-Gro enabled auto-merge (squash) August 12, 2024 10:29
@psfinaki

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@psfinaki

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

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

Labels

NO_RELEASE_NOTESLabel for pull requests which signals, that user opted-out of providing release notes

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants

@brianrourkeboll@vzarytovskii@psfinaki@KevinRansom@edgarfgp@T-Gro