Skip to content

Commit d8a3920

Browse files
committed
Merge pull request #2676 from eeckstein/stdlib-performance
Some changes in UnsafeBufferPointer to improve performance
2 parents e05649d + 5bfe35a commit d8a3920

File tree

1 file changed

+80
-4
lines changed

1 file changed

+80
-4
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,83 @@ public struct Unsafe${Mutable}BufferPointer<Element>
5757
return count
5858
}
5959

60+
public func index(after i: Int) -> Int {
61+
// NOTE: this is a manual specialization of index movement for a Strideable
62+
// index that is required for UnsafeBufferPointer performance. The
63+
// optimizer is not capable of creating partial specializations yet.
64+
// NOTE: Range checks are not performed here, because it is done later by
65+
// the subscript function.
66+
return i + 1
67+
}
68+
69+
public func formIndex(after i: inout Int) {
70+
// NOTE: this is a manual specialization of index movement for a Strideable
71+
// index that is required for UnsafeBufferPointer performance. The
72+
// optimizer is not capable of creating partial specializations yet.
73+
// NOTE: Range checks are not performed here, because it is done later by
74+
// the subscript function.
75+
i += 1
76+
}
77+
78+
public func index(before i: Int) -> Int {
79+
// NOTE: this is a manual specialization of index movement for a Strideable
80+
// index that is required for UnsafeBufferPointer performance. The
81+
// optimizer is not capable of creating partial specializations yet.
82+
// NOTE: Range checks are not performed here, because it is done later by
83+
// the subscript function.
84+
return i - 1
85+
}
86+
87+
public func formIndex(before i: inout Int) {
88+
// NOTE: this is a manual specialization of index movement for a Strideable
89+
// index that is required for UnsafeBufferPointer performance. The
90+
// optimizer is not capable of creating partial specializations yet.
91+
// NOTE: Range checks are not performed here, because it is done later by
92+
// the subscript function.
93+
i -= 1
94+
}
95+
96+
public func index(_ i: Int, offsetBy n: Int) -> Int {
97+
// NOTE: this is a manual specialization of index movement for a Strideable
98+
// index that is required for UnsafeBufferPointer performance. The
99+
// optimizer is not capable of creating partial specializations yet.
100+
// NOTE: Range checks are not performed here, because it is done later by
101+
// the subscript function.
102+
return i + n
103+
}
104+
105+
public func index(
106+
_ i: Int, offsetBy n: Int, limitedBy limit: Int
107+
) -> Int? {
108+
// NOTE: this is a manual specialization of index movement for a Strideable
109+
// index that is required for UnsafeBufferPointer performance. The
110+
// optimizer is not capable of creating partial specializations yet.
111+
// NOTE: Range checks are not performed here, because it is done later by
112+
// the subscript function.
113+
let l = limit - i
114+
if n > 0 ? l >= 0 && l < n : l <= 0 && n < l {
115+
return nil
116+
}
117+
return i + n
118+
}
119+
120+
public func distance(from start: Int, to end: Int) -> Int {
121+
// NOTE: this is a manual specialization of index movement for a Strideable
122+
// index that is required for UnsafeBufferPointer performance. The
123+
// optimizer is not capable of creating partial specializations yet.
124+
// NOTE: Range checks are not performed here, because it is done later by
125+
// the subscript function.
126+
return end - start
127+
}
128+
129+
public func _failEarlyRangeCheck(_ index: Int, bounds: Range<Int>) {
130+
// NOTE: This method is a no-op for performance reasons.
131+
}
132+
133+
public func _failEarlyRangeCheck(_ range: Range<Int>, bounds: Range<Int>) {
134+
// NOTE: This method is a no-op for performance reasons.
135+
}
136+
60137
public typealias Indices = CountableRange<Int>
61138

62139
public var indices: Indices {
@@ -126,11 +203,10 @@ public struct Unsafe${Mutable}BufferPointer<Element>
126203

127204
/// The number of elements in the buffer.
128205
public var count: Int {
129-
if _position == nil {
130-
_sanityCheck(_end == nil)
131-
return 0
206+
if let pos = _position {
207+
return _end! - pos
132208
}
133-
return _end! - _position!
209+
return 0
134210
}
135211

136212
let _position, _end: Unsafe${Mutable}Pointer<Element>?

0 commit comments

Comments
 (0)