@@ -692,46 +692,52 @@ public void CopyFrom(ArraySegment<byte> buffer)
692
692
CopyFrom( buffer . Array , buffer . Offset , buffer . Count ) ;
693
693
}
694
694
695
- public void CopyFrom( byte [ ] data , int offset , int count )
695
+ public unsafe void CopyFrom( byte [ ] data , int offset , int count )
696
696
{
697
697
Debug. Assert ( _block != null ) ;
698
698
Debug. Assert ( _block . Next == null ) ;
699
699
Debug. Assert ( _block . End == _index ) ;
700
-
701
- var pool = _block. Pool ;
700
+
702
701
var block = _block;
703
702
var blockIndex = _index;
703
+ var bytesLeftInBlock = block. BlockEndOffset - blockIndex ;
704
704
705
- var bufferIndex = offset ;
706
- var remaining = count ;
707
- var bytesLeftInBlock = block . Data . Offset + block . Data . Count - blockIndex ;
705
+ if ( bytesLeftInBlock >= count )
706
+ {
707
+ _index = blockIndex + count ;
708
+ Buffer. BlockCopy ( data , offset , block . Array , blockIndex , count ) ;
709
+ block. End = _index ;
710
+ return ;
711
+ }
708
712
709
- while ( remaining > 0 )
713
+ do
710
714
{
711
715
if ( bytesLeftInBlock == 0 )
712
716
{
713
- var nextBlock = pool. Lease( ) ;
714
- block . End = blockIndex ;
717
+ var nextBlock = block. Pool. Lease( ) ;
718
+ blockIndex = nextBlock . Data . Offset ;
719
+ bytesLeftInBlock = nextBlock . Data . Count ;
715
720
block . Next = nextBlock ;
716
721
block = nextBlock ;
717
-
718
- blockIndex = block . Data . Offset ;
719
- bytesLeftInBlock = block . Data . Count ;
720
722
}
721
723
722
- var bytesToCopy = remaining < bytesLeftInBlock ? remaining : bytesLeftInBlock ;
723
-
724
- Buffer . BlockCopy ( data , bufferIndex , block . Array , blockIndex , bytesToCopy ) ;
725
-
726
- blockIndex += bytesToCopy ;
727
- bufferIndex += bytesToCopy ;
728
- remaining -= bytesToCopy ;
729
- bytesLeftInBlock -= bytesToCopy ;
730
- }
731
-
732
- block . End = blockIndex ;
733
- _block = block ;
734
- _index = blockIndex ;
724
+ if ( count > bytesLeftInBlock )
725
+ {
726
+ count -= bytesLeftInBlock ;
727
+ Buffer . BlockCopy ( data , offset , block . Array , blockIndex , bytesLeftInBlock ) ;
728
+ offset += bytesLeftInBlock ;
729
+ block . End = blockIndex + bytesLeftInBlock ;
730
+ bytesLeftInBlock = 0 ;
731
+ }
732
+ else
733
+ {
734
+ _index = blockIndex + count ;
735
+ Buffer . BlockCopy ( data , offset , block . Array , blockIndex , count ) ;
736
+ block . End = _index ;
737
+ _block = block ;
738
+ return ;
739
+ }
740
+ } while ( true ) ;
735
741
}
736
742
737
743
public unsafe void CopyFromAscii ( string data )
@@ -740,64 +746,123 @@ public unsafe void CopyFromAscii(string data)
740
746
Debug . Assert ( _block . Next = = null ) ;
741
747
Debug . Assert ( _block . End = = _index ) ;
742
748
743
- var pool = _block. Pool;
744
749
var block = _block;
745
750
var blockIndex = _index ;
746
- var length = data . Length ;
751
+ var count = data . Length ;
747
752
748
- var bytesLeftInBlock = block . Data . Offset + block . Data . Count - blockIndex ;
749
- var bytesLeftInBlockMinusSpan = bytesLeftInBlock - 3 ;
753
+ var blockRemaining = block . BlockEndOffset - blockIndex ;
750
754
751
- fixed ( char * pData = data)
755
+ fixed ( char * pInput = data)
752
756
{
753
- var input = pData ;
754
- var inputEnd = pData + length ;
755
- var inputEndMinusSpan = inputEnd - 3 ;
757
+ if ( blockRemaining > = count )
758
+ {
759
+ _index = blockIndex + count;
760
+
761
+ fixed ( byte * pOutput = & block. Data. Array[ blockIndex] )
762
+ {
763
+ CopyFromAscii( pInput , pOutput , count ) ;
764
+ }
756
765
757
- while ( input < inputEnd )
766
+ block . End = _index ;
767
+ return ;
768
+ }
769
+
770
+ var input = pInput ;
771
+ do
758
772
{
759
- if ( bytesLeftInBlock == 0 )
773
+ if ( blockRemaining = = 0 )
760
774
{
761
- var nextBlock = pool. Lease( ) ;
762
- block. End = blockIndex;
775
+ var nextBlock = block . Pool . Lease ( ) ;
776
+ blockIndex = nextBlock . Data . Offset ;
777
+ blockRemaining = nextBlock . Data . Count ;
763
778
block . Next = nextBlock ;
764
779
block = nextBlock ;
765
-
766
- blockIndex = block. Data. Offset;
767
- bytesLeftInBlock = block. Data. Count;
768
- bytesLeftInBlockMinusSpan = bytesLeftInBlock - 3 ;
769
780
}
770
781
771
- fixed ( byte * pOutput = & block . Data . Array [ block . End ] )
782
+ if ( count > blockRemaining )
772
783
{
773
- //this line is needed to allow output be an register var
774
- var output = pOutput;
784
+ count -= blockRemaining;
775
785
776
- var copied = 0 ;
777
- for ( ; input < inputEndMinusSpan && copied < bytesLeftInBlockMinusSpan ; copied += 4 )
786
+ fixed ( byte * pOutput = & block. Data. Array[ blockIndex] )
778
787
{
779
- * ( output ) = ( byte ) * ( input) ;
780
- * ( output + 1 ) = ( byte ) * ( input + 1 ) ;
781
- * ( output + 2 ) = ( byte ) * ( input + 2 ) ;
782
- * ( output + 3 ) = ( byte ) * ( input + 3 ) ;
783
- output += 4 ;
784
- input += 4 ;
788
+ CopyFromAscii( input, pOutput , blockRemaining ) ;
785
789
}
786
- for ( ; input < inputEnd && copied < bytesLeftInBlock; copied++ )
790
+
791
+ block . End = blockIndex + blockRemaining ;
792
+ input += blockRemaining ;
793
+ blockRemaining = 0 ;
794
+ }
795
+ else
796
+ {
797
+ _index = blockIndex + count ;
798
+
799
+ fixed ( byte * pOutput = & block . Data . Array [ blockIndex ] )
787
800
{
788
- * ( output ++ ) = ( byte ) * ( input ++ ) ;
801
+ CopyFromAscii ( input , pOutput , count ) ;
789
802
}
790
803
791
- blockIndex += copied ;
792
- bytesLeftInBlockMinusSpan -= copied ;
793
- bytesLeftInBlock -= copied ;
804
+ block . End = _index ;
805
+ _block = block ;
806
+ return ;
794
807
}
795
- }
808
+ } while ( true ) ;
796
809
}
810
+ }
797
811
798
- block. End = blockIndex;
799
- _block = block;
800
- _index = blockIndex;
812
+ private unsafe static void CopyFromAscii ( char * pInput , byte * pOutput , int count )
813
+ {
814
+ var i = 0 ;
815
+ //these line is needed to allow input/output be an register var
816
+ var input = pInput;
817
+ var output = pOutput ;
818
+
819
+ while ( i < count - 11 )
820
+ {
821
+ i += 12 ;
822
+ * ( output ) = ( byte ) * ( input) ;
823
+ * ( output + 1 ) = ( byte ) * ( input + 1 ) ;
824
+ * ( output + 2 ) = ( byte ) * ( input + 2 ) ;
825
+ * ( output + 3 ) = ( byte ) * ( input + 3 ) ;
826
+ * ( output + 4 ) = ( byte ) * ( input + 4 ) ;
827
+ * ( output + 5 ) = ( byte ) * ( input + 5 ) ;
828
+ * ( output + 6 ) = ( byte ) * ( input + 6 ) ;
829
+ * ( output + 7 ) = ( byte ) * ( input + 7 ) ;
830
+ * ( output + 8 ) = ( byte ) * ( input + 8 ) ;
831
+ * ( output + 9 ) = ( byte ) * ( input + 9 ) ;
832
+ * ( output + 10 ) = ( byte ) * ( input + 10 ) ;
833
+ * ( output + 11 ) = ( byte ) * ( input + 11 ) ;
834
+ output += 12 ;
835
+ input += 12 ;
836
+ }
837
+ if ( i < count - 5 )
838
+ {
839
+ i += 6 ;
840
+ * ( output) = ( byte ) * ( input) ;
841
+ * ( output + 1 ) = ( byte ) * ( input + 1 ) ;
842
+ * ( output + 2 ) = ( byte ) * ( input + 2 ) ;
843
+ * ( output + 3 ) = ( byte ) * ( input + 3 ) ;
844
+ * ( output + 4 ) = ( byte ) * ( input + 4 ) ;
845
+ * ( output + 5 ) = ( byte ) * ( input + 5 ) ;
846
+ output += 6 ;
847
+ input += 6 ;
848
+ }
849
+ if ( i < count - 3 )
850
+ {
851
+ i += 4 ;
852
+ * ( output) = ( byte ) * ( input) ;
853
+ * ( output + 1 ) = ( byte ) * ( input + 1 ) ;
854
+ * ( output + 2 ) = ( byte ) * ( input + 2 ) ;
855
+ * ( output + 3 ) = ( byte ) * ( input + 3 ) ;
856
+ output += 4 ;
857
+ input += 4 ;
858
+ }
859
+ while ( i < count)
860
+ {
861
+ i++ ;
862
+ * output = ( byte ) * input;
863
+ output++ ;
864
+ input++ ;
865
+ }
801
866
}
802
867
}
803
868
}
0 commit comments