runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[API Proposal]: Make Decimal constructor decimal.ToDecimal(ReadOnlySpan<byte> span) public

Open silkfire opened this issue 4 years ago • 10 comments

Background and motivation

The decimal struct comes with several constructors; some I've observed are not available in the public API.

As a developer of a perfomant library, I'm creating a decimal value from a byte[] buffer. The way to do it today is using the following constructor:

new decimal(new[]
{
   BinaryPrimitives.ReadInt32LittleEndian(buffer), // lo
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(4)), // mid
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(8)), // hi
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(12)) // flags
});

Which in turns calls on this ReadOnlySpan<int>-based constructor:

public Decimal(ReadOnlySpan<int> bits)
{
   if (bits.Length == 4)
   {
      int f = bits[3];
      if (IsValid(f))
      {
         _lo64 = (uint)bits[0] + ((ulong)(uint)bits[1] << 32);
        _hi32 = (uint)bits[2];
        _flags = f;
        return;
      }
   }

   throw new ArgumentException(SR.Arg_DecBitCtor);
}

In the System.Runtime assembly though, you have access to this ReadOnlySpan<byte>-based internal method:

internal static decimal ToDecimal(ReadOnlySpan<byte> span)
{
   Debug.Assert(span.Length >= 16, "span.Length >= 16");
   int lo = BinaryPrimitives.ReadInt32LittleEndian(span);
   int mid = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(4));
   int hi = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(8));
   int flags = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(12));

   return new decimal(lo, mid, hi, flags);
}

Which subsequently calls on this method:

private Decimal(int lo, int mid, int hi, int flags)
{
   if (IsValid(flags))
   {
      _lo64 = (uint)lo + ((ulong)(uint)mid << 32);
      _hi32 = (uint)hi;
      _flags = flags;

      return;
   }

   throw new ArgumentException(SR.Arg_DecBitCtor);
}

I've run some benchmarks and done further investigating and it seems like the array access used in the public constructor has a negative impact on performance. The difference is roughly 15% slower.

API Proposal

internal static decimal ToDecimal(ReadOnlySpan<byte> span)
{
   Debug.Assert(span.Length >= 16, "span.Length >= 16");
   int lo = BinaryPrimitives.ReadInt32LittleEndian(span);
   int mid = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(4));
   int hi = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(8));
   int flags = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(12));

   return new decimal(lo, mid, hi, flags);
}

API Usage

byte[] buffer = ...

var d = decimal.ToDecimal(buffer);

Alternative Designs

No response

Risks

No response

silkfire avatar Mar 03 '22 23:03 silkfire

Tagging subscribers to this area: @dotnet/area-system-memory See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

The decimal struct comes with several constructors; some I've observed are not available in the public API.

As a developer of a perfomant library, I'm creating a decimal value from a byte[] buffer. The way to do it today is using the following constructor:

new decimal(new[]
{
   BinaryPrimitives.ReadInt32LittleEndian(buffer), // lo
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(4)), // mid
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(8)), // hi
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(12)) // flags
});

Which in turns calls on this ReadOnlySpan<int>-based constructor:

public Decimal(ReadOnlySpan<int> bits)
{
   if (bits.Length == 4)
   {
      int f = bits[3];
      if (IsValid(f))
      {
         _lo64 = (uint)bits[0] + ((ulong)(uint)bits[1] << 32);
        _hi32 = (uint)bits[2];
        _flags = f;
        return;
      }
   }

   throw new ArgumentException(SR.Arg_DecBitCtor);
}

In the System.Runtime assembly though, you have access to this ReadOnlySpan<byte>-based internal method:

internal static decimal ToDecimal(ReadOnlySpan<byte> span)
{
   Debug.Assert(span.Length >= 16, "span.Length >= 16");
   int lo = BinaryPrimitives.ReadInt32LittleEndian(span);
   int mid = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(4));
   int hi = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(8));
   int flags = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(12));

   return new decimal(lo, mid, hi, flags);
}

Which subsequently calls on this method:

private Decimal(int lo, int mid, int hi, int flags)
{
   if (IsValid(flags))
   {
      _lo64 = (uint)lo + ((ulong)(uint)mid << 32);
      _hi32 = (uint)hi;
      _flags = flags;

      return;
   }

   throw new ArgumentException(SR.Arg_DecBitCtor);
}

I've run some benchmarks and done further investigating and it seems like the array access used in the public constructor has a negative impact on performance. The difference is roughly 15% slower.

API Proposal

internal static decimal ToDecimal(ReadOnlySpan<byte> span)
{
   Debug.Assert(span.Length >= 16, "span.Length >= 16");
   int lo = BinaryPrimitives.ReadInt32LittleEndian(span);
   int mid = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(4));
   int hi = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(8));
   int flags = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(12));

   return new decimal(lo, mid, hi, flags);
}

API Usage

byte[] buffer = ...

var d = decimal.ToDecimal(buffer);

Alternative Designs

No response

Risks

No response

Author: silkfire
Assignees: -
Labels:

api-suggestion, area-System.Memory, untriaged

Milestone: -

ghost avatar Mar 03 '22 23:03 ghost

Tagging subscribers to this area: @dotnet/area-system-numerics See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

The decimal struct comes with several constructors; some I've observed are not available in the public API.

As a developer of a perfomant library, I'm creating a decimal value from a byte[] buffer. The way to do it today is using the following constructor:

new decimal(new[]
{
   BinaryPrimitives.ReadInt32LittleEndian(buffer), // lo
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(4)), // mid
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(8)), // hi
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(12)) // flags
});

Which in turns calls on this ReadOnlySpan<int>-based constructor:

public Decimal(ReadOnlySpan<int> bits)
{
   if (bits.Length == 4)
   {
      int f = bits[3];
      if (IsValid(f))
      {
         _lo64 = (uint)bits[0] + ((ulong)(uint)bits[1] << 32);
        _hi32 = (uint)bits[2];
        _flags = f;
        return;
      }
   }

   throw new ArgumentException(SR.Arg_DecBitCtor);
}

In the System.Runtime assembly though, you have access to this ReadOnlySpan<byte>-based internal method:

internal static decimal ToDecimal(ReadOnlySpan<byte> span)
{
   Debug.Assert(span.Length >= 16, "span.Length >= 16");
   int lo = BinaryPrimitives.ReadInt32LittleEndian(span);
   int mid = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(4));
   int hi = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(8));
   int flags = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(12));

   return new decimal(lo, mid, hi, flags);
}

Which subsequently calls on this method:

private Decimal(int lo, int mid, int hi, int flags)
{
   if (IsValid(flags))
   {
      _lo64 = (uint)lo + ((ulong)(uint)mid << 32);
      _hi32 = (uint)hi;
      _flags = flags;

      return;
   }

   throw new ArgumentException(SR.Arg_DecBitCtor);
}

I've run some benchmarks and done further investigating and it seems like the array access used in the public constructor has a negative impact on performance. The difference is roughly 15% slower.

API Proposal

internal static decimal ToDecimal(ReadOnlySpan<byte> span)
{
   Debug.Assert(span.Length >= 16, "span.Length >= 16");
   int lo = BinaryPrimitives.ReadInt32LittleEndian(span);
   int mid = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(4));
   int hi = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(8));
   int flags = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(12));

   return new decimal(lo, mid, hi, flags);
}

API Usage

byte[] buffer = ...

var d = decimal.ToDecimal(buffer);

Alternative Designs

No response

Risks

No response

Author: silkfire
Assignees: -
Labels:

api-suggestion, area-System.Numerics, untriaged

Milestone: -

ghost avatar Mar 04 '22 08:03 ghost

What's about the endianness of the byte array? Does it follow current platform, or does it follow some fixed endian? API depending on current endianness should be avoided.

As a developer of a perfomant library, I'm creating a decimal value from a byte[] buffer.

BTW you can use stackalloc to save allocation of array:

new decimal(stackalloc[]
{
   BinaryPrimitives.ReadInt32LittleEndian(buffer), // lo
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(4)), // mid
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(8)), // hi
   BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(12)) // flags
});

You can even use MemoryMarshal to bypass the constructor if you are sure with the endianness.

huoyaoyuan avatar Mar 04 '22 08:03 huoyaoyuan

It should follow a fixed endianness. Okay, isn't the main performance problem that the runtime needs to check whether index 0-4 points to a valid location in memory? I'll have to benchmark this of course, but thanks for the suggestion.

silkfire avatar Mar 04 '22 11:03 silkfire

Is it a good idea to perhaps add method like this too (not sure about naming) that would return false if span is too small or the bytes are invalidl public static bool TryToDecimal(ReadOnlySpan<byte> span, out decimal result)

Maybe also a method like this to correspond to the ROS constructor? public static bool TryToDecimal(ReadOnlySpan<int> span, out decimal result)

lilinus avatar Mar 08 '22 14:03 lilinus

@lilinus If I remember correctly there's an internal method used called IsValid that might be what you're looking for.

silkfire avatar Mar 08 '22 14:03 silkfire

There is already a constructor that takes ReadOnlySpan<int> and an inverse API for filling a span (TryGetBits).

I'm not convinced one that always takes bytes is worthwhile.

tannergooding avatar Mar 14 '22 19:03 tannergooding

This issue has been marked needs-author-action since it may be missing important information. Please refer to our contribution guidelines for tips on how to report issues effectively.

ghost avatar Mar 14 '22 19:03 ghost

This issue has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

ghost avatar Mar 28 '22 21:03 ghost

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

ghost avatar Apr 12 '22 00:04 ghost