@@ -612,36 +612,28 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
612
612
public typealias Index = Int
613
613
public typealias Indices = Range < Int >
614
614
615
+ // A small inline buffer of bytes suitable for stack-allocation of small data.
616
+ // Inlinability strategy: everything here should be inlined for direct operation on the stack wherever possible.
615
617
@usableFromInline
616
618
@_fixed_layout
617
619
internal struct InlineData {
618
620
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
619
- @usableFromInline
620
- typealias Buffer = ( UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 ,
621
- UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 ) //len //enum
622
- @usableFromInline
623
- var bytes : Buffer
621
+ @usableFromInline typealias Buffer = ( UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 ,
622
+ UInt8 , UInt8 , UInt8 , UInt8 , UInt8 , UInt8 ) //len //enum
623
+ @usableFromInline var bytes : Buffer
624
624
#elseif arch(i386) || arch(arm)
625
- @usableFromInline
626
- typealias Buffer = ( UInt8 , UInt8 , UInt8 , UInt8 ,
627
- UInt8 , UInt8 ) //len //enum
628
- @usableFromInline
629
- var bytes : Buffer
625
+ @usableFromInline typealias Buffer = ( UInt8 , UInt8 , UInt8 , UInt8 ,
626
+ UInt8 , UInt8 ) //len //enum
627
+ @usableFromInline var bytes : Buffer
630
628
#endif
631
- @usableFromInline
632
- var length : UInt8
629
+ @usableFromInline var length : UInt8
633
630
634
- @inlinable
631
+ @inlinable // This is @inlinable as trivially computable.
635
632
static func canStore( count: Int ) -> Bool {
636
633
return count <= MemoryLayout< Buffer> . size
637
634
}
638
635
639
- @inlinable
640
- init ( ) {
641
- self . init ( count: 0 )
642
- }
643
-
644
- @inlinable
636
+ @inlinable // This is @inlinable as a convenience initializer.
645
637
init ( _ srcBuffer: UnsafeRawBufferPointer ) {
646
638
self . init ( count: srcBuffer. count)
647
639
if srcBuffer. count > 0 {
@@ -651,19 +643,18 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
651
643
}
652
644
}
653
645
654
- @inlinable
655
- init ( count: Int ) {
646
+ @inlinable // This is @inlinable as a trivial initializer.
647
+ init ( count: Int = 0 ) {
656
648
assert ( count <= MemoryLayout< Buffer> . size)
657
649
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le)
658
650
bytes = ( UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) )
659
651
#elseif arch(i386) || arch(arm)
660
- bytes = ( UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) ,
661
- UInt8 ( 0 ) , UInt8 ( 0 ) )
652
+ bytes = ( UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) , UInt8 ( 0 ) )
662
653
#endif
663
654
length = UInt8 ( count)
664
655
}
665
656
666
- @inlinable
657
+ @inlinable // This is @inlinable as a convenience initializer.
667
658
init ( _ slice: InlineSlice , count: Int ) {
668
659
self . init ( count: count)
669
660
Swift . withUnsafeMutableBytes ( of: & bytes) { dstBuffer in
@@ -673,7 +664,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
673
664
}
674
665
}
675
666
676
- @inlinable
667
+ @inlinable // This is @inlinable as a convenience initializer.
677
668
init ( _ slice: LargeSlice , count: Int ) {
678
669
self . init ( count: count)
679
670
Swift . withUnsafeMutableBytes ( of: & bytes) { dstBuffer in
@@ -683,57 +674,61 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
683
674
}
684
675
}
685
676
686
- @inlinable
677
+ @inlinable // This is @inlinable as trivially computable.
687
678
var capacity : Int {
688
679
return MemoryLayout< Buffer> . size
689
680
}
690
681
691
- @inlinable
682
+ @inlinable // This is @inlinable as trivially computable.
692
683
var count : Int {
693
684
get {
694
- return numericCast ( length)
685
+ return Int ( length)
695
686
}
696
687
set ( newValue) {
697
688
precondition ( newValue <= MemoryLayout< Buffer> . size)
698
689
length = UInt8 ( newValue)
699
690
}
700
691
}
701
692
702
- @inlinable
703
- var startIndex : Int { return 0 }
693
+ @inlinable // This is @inlinable as trivially computable.
694
+ var startIndex : Int {
695
+ return 0
696
+ }
704
697
705
- @inlinable
706
- var endIndex : Int { return count }
698
+ @inlinable // This is @inlinable as trivially computable.
699
+ var endIndex : Int {
700
+ return count
701
+ }
707
702
708
- @inlinable
703
+ @inlinable // This is @inlinable as a generic, trivially forwarding function.
709
704
func withUnsafeBytes< Result> ( _ apply: ( UnsafeRawBufferPointer ) throws -> Result ) rethrows -> Result {
710
- let count : Int = numericCast ( length)
705
+ let count = Int ( length)
711
706
return try Swift . withUnsafeBytes ( of: bytes) { ( rawBuffer) throws -> Result in
712
707
return try apply ( UnsafeRawBufferPointer ( start: rawBuffer. baseAddress, count: count) )
713
708
}
714
709
}
715
710
716
- @inlinable
711
+ @inlinable // This is @inlinable as a generic, trivially forwarding function.
717
712
mutating func withUnsafeMutableBytes< Result> ( _ apply: ( UnsafeMutableRawBufferPointer ) throws -> Result ) rethrows -> Result {
718
- let count : Int = numericCast ( length)
713
+ let count = Int ( length)
719
714
return try Swift . withUnsafeMutableBytes ( of: & bytes) { ( rawBuffer) throws -> Result in
720
715
return try apply ( UnsafeMutableRawBufferPointer ( start: rawBuffer. baseAddress, count: count) )
721
716
}
722
717
}
723
718
724
- @inlinable
719
+ @inlinable // This is @inlinable as trivially computable.
725
720
mutating func append( contentsOf buffer: UnsafeRawBufferPointer ) {
726
721
guard buffer. count > 0 else { return }
727
722
assert ( count + buffer. count <= MemoryLayout< Buffer> . size)
728
723
let cnt = count
729
724
_ = Swift . withUnsafeMutableBytes ( of: & bytes) { rawBuffer in
730
725
rawBuffer. baseAddress? . advanced ( by: cnt) . copyMemory ( from: buffer. baseAddress!, byteCount: buffer. count)
731
726
}
732
- length += UInt8 ( buffer. count)
733
727
728
+ length += UInt8 ( buffer. count)
734
729
}
735
730
736
- @inlinable
731
+ @inlinable // This is @inlinable as trivially computable.
737
732
subscript( index: Index ) -> UInt8 {
738
733
get {
739
734
assert ( index <= MemoryLayout< Buffer> . size)
@@ -751,20 +746,21 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
751
746
}
752
747
}
753
748
754
- @inlinable
749
+ @inlinable // This is @inlinable as trivially computable.
755
750
mutating func resetBytes( in range: Range < Index > ) {
756
751
assert ( range. lowerBound <= MemoryLayout< Buffer> . size)
757
752
assert ( range. upperBound <= MemoryLayout< Buffer> . size)
758
753
precondition ( range. lowerBound <= length, " index \( range. lowerBound) is out of bounds of 0..< \( length) " )
759
754
if count < range. upperBound {
760
755
count = range. upperBound
761
756
}
762
- Swift . withUnsafeMutableBytes ( of: & bytes) { rawBuffer in
763
- bzero ( rawBuffer. baseAddress? . advanced ( by: range. lowerBound) , range. upperBound - range. lowerBound)
757
+
758
+ let _ = Swift . withUnsafeMutableBytes ( of: & bytes) { rawBuffer in
759
+ memset ( rawBuffer. baseAddress? . advanced ( by: range. lowerBound) , 0 , range. upperBound - range. lowerBound)
764
760
}
765
761
}
766
762
767
- @inlinable
763
+ @usableFromInline // This is not @ inlinable as it is a non-trivial, non-generic function.
768
764
mutating func replaceSubrange( _ subrange: Range < Index > , with replacementBytes: UnsafeRawPointer ? , count replacementLength: Int ) {
769
765
assert ( subrange. lowerBound <= MemoryLayout< Buffer> . size)
770
766
assert ( subrange. upperBound <= MemoryLayout< Buffer> . size)
@@ -788,7 +784,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
788
784
count = resultingLength
789
785
}
790
786
791
- @inlinable
787
+ @inlinable // This is @inlinable as trivially computable.
792
788
func copyBytes( to pointer: UnsafeMutableRawPointer , from range: Range < Int > ) {
793
789
precondition ( startIndex <= range. lowerBound, " index \( range. lowerBound) is out of bounds of \( startIndex) ..< \( endIndex) " )
794
790
precondition ( range. lowerBound <= endIndex, " index \( range. lowerBound) is out of bounds of \( startIndex) ..< \( endIndex) " )
0 commit comments