Skip to content

CSHARP-5368: Eliminiate allocations in Bson.Decimal128 to decimal conversion#1515

Merged
papafe merged 4 commits intomongodb:mainfrom
obligaron:decimalconversion
Nov 26, 2024
Merged

CSHARP-5368: Eliminiate allocations in Bson.Decimal128 to decimal conversion#1515
papafe merged 4 commits intomongodb:mainfrom
obligaron:decimalconversion

Conversation

@obligaron
Copy link
Contributor

See CSHARP-5368 for details.

This PR removes allocations that happens for

@obligaron obligaron requested a review from a team as a code owner October 28, 2024 13:04
@obligaron obligaron requested review from adelinowona and removed request for a team October 28, 2024 13:04
@papafe papafe self-requested a review November 22, 2024 11:12
@papafe
Copy link
Contributor

papafe commented Nov 22, 2024

Hi @obligaron thanks a lot for your PR. I'll take a look, but in the meantime could you rebase on the latest main? We did some changes to the CI and most tests will fail if you don't.

@obligaron
Copy link
Contributor Author

I rebased the PR. Thanks @papafe for looking at the code. 🙂

var yType = GetDecimal128Type(y);
var result = xType.CompareTo(yType);
#if NET6_0_OR_GREATER
var result = Comparer<Decimal128Type>.Default.Compare(xType, yType);
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that Comparer.Default is available since .NET Framework 2.0. Is there a specific reason why we're making a cutoff point at .NET 6.0 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

.NET Framework lacks some optimisations for enums (e.g. dotnet/coreclr#5503 or dotnet/coreclr#14125).
This results in boxing and lookup overhead for the default comparer on .NET Framework.
That's why I specially cased it to avoid this overhead.

Alternatives could be

  • Change the .NET Framework implementation to Comparer.Default and accept the boxing/overhead (this is slightly slower than the current implementation in main).
  • Change the .NET 6 implementation to the int-cast logic of the .NET Framework (this is almost identical, but less readable).

I'm open to all three (keep it or change to one of the alternatives). 🙂

Copy link
Contributor

@papafe papafe left a comment

Choose a reason for hiding this comment

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

LGTM!

Thanks a lot for this PR @obligaron

@papafe papafe merged commit 5edf5ba into mongodb:main Nov 26, 2024
@obligaron obligaron deleted the decimalconversion branch November 26, 2024 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants