7
7
using System . Linq ;
8
8
using System . Runtime . CompilerServices ;
9
9
using System . Runtime . InteropServices ;
10
- using static System . Runtime . InteropServices . JavaScript . JSType ;
11
10
using EditorBrowsableAttribute = System . ComponentModel . EditorBrowsableAttribute ;
12
11
using EditorBrowsableState = System . ComponentModel . EditorBrowsableState ;
13
12
@@ -528,14 +527,14 @@ public void CopyTo(scoped TensorSpan<T> destination)
528
527
if ( Rank > TensorShape . MaxInlineRank )
529
528
{
530
529
curIndexesArray = ArrayPool < nint > . Shared . Rent ( Rank ) ;
531
- curIndexes = curIndexesArray ;
532
- curIndexes = curIndexes . Slice ( 0 , Rank ) ;
530
+ curIndexes = curIndexesArray . AsSpan ( 0 , Rank ) ;
533
531
}
534
532
else
535
533
{
536
534
curIndexesArray = null ;
537
535
curIndexes = stackalloc nint [ Rank ] ;
538
536
}
537
+ curIndexes . Clear ( ) ;
539
538
540
539
nint copiedValues = 0 ;
541
540
TensorSpan < T > slice = destination . Slice ( _shape . Lengths ) ;
@@ -576,14 +575,14 @@ public bool TryCopyTo(scoped TensorSpan<T> destination)
576
575
if ( Rank > TensorShape . MaxInlineRank )
577
576
{
578
577
curIndexesArray = ArrayPool < nint > . Shared . Rent ( Rank ) ;
579
- curIndexes = curIndexesArray ;
580
- curIndexes = curIndexes . Slice ( 0 , Rank ) ;
578
+ curIndexes = curIndexesArray . AsSpan ( 0 , Rank ) ;
581
579
}
582
580
else
583
581
{
584
582
curIndexesArray = null ;
585
583
curIndexes = stackalloc nint [ Rank ] ;
586
584
}
585
+ curIndexes . Clear ( ) ;
587
586
588
587
nint copiedValues = 0 ;
589
588
TensorSpan < T > slice = destination . Slice ( _shape . Lengths ) ;
@@ -656,34 +655,58 @@ public ReadOnlyTensorSpan<T> Slice(params scoped ReadOnlySpan<NRange> ranges)
656
655
if ( ranges . Length != Lengths . Length )
657
656
throw new ArgumentOutOfRangeException ( nameof ( ranges ) , "Number of dimensions to slice does not equal the number of dimensions in the span" ) ;
658
657
658
+ ReadOnlyTensorSpan < T > toReturn ;
659
659
scoped Span < nint > lengths ;
660
660
scoped Span < nint > offsets ;
661
+ nint [ ] ? lengthsArray ;
662
+ nint [ ] ? offsetsArray ;
661
663
if ( Rank > TensorShape . MaxInlineRank )
662
664
{
663
- lengths = stackalloc nint [ Rank ] ;
664
- offsets = stackalloc nint [ Rank ] ;
665
+ lengthsArray = ArrayPool < nint > . Shared . Rent ( Rank ) ;
666
+ lengths = lengthsArray . AsSpan ( 0 , Rank ) ;
667
+
668
+ offsetsArray = ArrayPool < nint > . Shared . Rent ( Rank ) ;
669
+ offsets = offsetsArray . AsSpan ( 0 , Rank ) ;
665
670
}
666
671
else
667
672
{
668
- lengths = new nint [ Rank ] ;
669
- offsets = new nint [ Rank ] ;
673
+ lengths = stackalloc nint [ Rank ] ;
674
+ offsets = stackalloc nint [ Rank ] ;
675
+
676
+ lengthsArray = null ;
677
+ offsetsArray = null ;
670
678
}
679
+ lengths . Clear ( ) ;
680
+ offsets . Clear ( ) ;
671
681
672
682
for ( int i = 0 ; i < ranges . Length ; i ++ )
673
683
{
674
684
( offsets [ i ] , lengths [ i ] ) = ranges [ i ] . GetOffsetAndLength ( Lengths [ i ] ) ;
675
685
}
676
686
687
+ // FlattenedLength is computed everytime so using a local to cache the value.
688
+ nint flattenedLength = FlattenedLength ;
677
689
nint index = 0 ;
678
- for ( int i = 0 ; i < offsets . Length ; i ++ )
690
+
691
+ if ( flattenedLength != 0 )
679
692
{
680
- index += Strides [ i ] * ( offsets [ i ] ) ;
693
+ for ( int i = 0 ; i < offsets . Length ; i ++ )
694
+ {
695
+ index += Strides [ i ] * ( offsets [ i ] ) ;
696
+ }
681
697
}
682
698
683
- if ( index >= _shape . _memoryLength || index < 0 )
699
+ if ( ( index >= _shape . _memoryLength || index < 0 ) && flattenedLength != 0 )
684
700
ThrowHelper . ThrowIndexOutOfRangeException ( ) ;
685
701
686
- return new ReadOnlyTensorSpan < T > ( ref Unsafe . Add ( ref _reference , index ) , lengths , _shape . Strides , _shape . _memoryLength - index ) ;
702
+ toReturn = new ReadOnlyTensorSpan < T > ( ref Unsafe . Add ( ref _reference , index ) , lengths , _shape . Strides , _shape . _memoryLength - index ) ;
703
+
704
+ if ( offsetsArray != null )
705
+ ArrayPool < nint > . Shared . Return ( offsetsArray ) ;
706
+ if ( lengthsArray != null )
707
+ ArrayPool < nint > . Shared . Return ( lengthsArray ) ;
708
+
709
+ return toReturn ;
687
710
}
688
711
689
712
/// <summary>
@@ -700,14 +723,14 @@ public bool TryFlattenTo(scoped Span<T> destination)
700
723
if ( Rank > TensorShape . MaxInlineRank )
701
724
{
702
725
curIndexesArray = ArrayPool < nint > . Shared . Rent ( Rank ) ;
703
- curIndexes = curIndexesArray ;
704
- curIndexes = curIndexes . Slice ( 0 , Rank ) ;
726
+ curIndexes = curIndexesArray . AsSpan ( 0 , Rank ) ;
705
727
}
706
728
else
707
729
{
708
730
curIndexesArray = null ;
709
731
curIndexes = stackalloc nint [ Rank ] ;
710
732
}
733
+ curIndexes . Clear ( ) ;
711
734
712
735
nint copiedValues = 0 ;
713
736
while ( copiedValues < _shape . FlattenedLength )
@@ -741,14 +764,14 @@ public void FlattenTo(scoped Span<T> destination)
741
764
if ( Rank > TensorShape . MaxInlineRank )
742
765
{
743
766
curIndexesArray = ArrayPool < nint > . Shared . Rent ( Rank ) ;
744
- curIndexes = curIndexesArray ;
745
- curIndexes = curIndexes . Slice ( 0 , Rank ) ;
767
+ curIndexes = curIndexesArray . AsSpan ( 0 , Rank ) ;
746
768
}
747
769
else
748
770
{
749
771
curIndexesArray = null ;
750
772
curIndexes = stackalloc nint [ Rank ] ;
751
773
}
774
+ curIndexes . Clear ( ) ;
752
775
753
776
nint copiedValues = 0 ;
754
777
while ( copiedValues < _shape . FlattenedLength )
0 commit comments