Skip to content

Commit cc91a4a

Browse files
committed
Disables redendent return because of buildkite throwing errors for now
Using overflow operators within swift to improve performance Adds tests for GRPC message creation from a retained _InternalByteBuffer
1 parent a8d7af1 commit cc91a4a

File tree

13 files changed

+186
-155
lines changed

13 files changed

+186
-155
lines changed

swift.swiftformat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
--typeattributes prev-line # wrapAttributes
1818

1919
# rules
20-
--rules wrap,todos,anyObjectProtocol,redundantParens,redundantReturn,redundantSelf,sortImports,strongifiedSelf,trailingCommas,trailingSpace,wrapArguments,wrapMultilineStatementBraces,indent,wrapAttributes,void,fileHeader
20+
--rules wrap,todos,anyObjectProtocol,redundantParens,redundantSelf,sortImports,strongifiedSelf,trailingCommas,trailingSpace,wrapArguments,wrapMultilineStatementBraces,indent,wrapAttributes,void,fileHeader
2121
--disable trailingclosures
2222

2323
--exclude **/*_generated.swift

swift/Sources/FlatBuffers/ByteBuffer.swift

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,10 @@ public struct ByteBuffer {
3636
case byteBuffer(_InternalByteBuffer)
3737
case array([UInt8])
3838
case pointer(UnsafeMutableRawPointer)
39-
case writePointer(UnsafeMutableRawPointer)
40-
41-
var isOwned: Bool {
42-
switch self {
43-
case .writePointer: true
44-
default: false
45-
}
46-
}
4739
}
4840

41+
/// This storage doesn't own the memory, therefore, we won't deallocate on deinit.
42+
private let isOwned: Bool
4943
/// Retained blob of data that requires the storage to retain a pointer to.
5044
@usableFromInline
5145
var retainedBlob: Blob
@@ -58,18 +52,21 @@ public struct ByteBuffer {
5852
byteCount: count,
5953
alignment: MemoryLayout<UInt8>.alignment)
6054
capacity = count
61-
retainedBlob = .writePointer(memory)
55+
retainedBlob = .pointer(memory)
56+
isOwned = true
6257
}
6358

6459
@usableFromInline
6560
init(blob: Blob, capacity count: Int) {
6661
capacity = count
6762
retainedBlob = blob
63+
isOwned = false
6864
}
6965

7066
deinit {
67+
guard isOwned else { return }
7168
switch retainedBlob {
72-
case .writePointer(let unsafeMutableRawPointer):
69+
case .pointer(let unsafeMutableRawPointer):
7370
unsafeMutableRawPointer.deallocate()
7471
default: break
7572
}
@@ -78,7 +75,7 @@ public struct ByteBuffer {
7875
@usableFromInline
7976
func copy(from ptr: UnsafeRawPointer, count: Int) {
8077
assert(
81-
retainedBlob.isOwned,
78+
isOwned,
8279
"copy should NOT be called on a buffer that is built by assumingMemoryBound")
8380
withUnsafeRawPointer {
8481
$0.copyMemory(from: ptr, byteCount: count)
@@ -88,88 +85,91 @@ public struct ByteBuffer {
8885
@usableFromInline
8986
func initialize(for size: Int) {
9087
assert(
91-
retainedBlob.isOwned,
88+
isOwned,
9289
"initalize should NOT be called on a buffer that is built by assumingMemoryBound")
9390
withUnsafeRawPointer {
9491
memset($0, 0, size)
9592
}
9693
}
9794

95+
@discardableResult
9896
@inline(__always)
9997
func withUnsafeBytes<T>(
10098
_ body: (UnsafeRawBufferPointer) throws
10199
-> T) rethrows -> T
102100
{
103101
switch retainedBlob {
104102
case .byteBuffer(let byteBuffer):
105-
try byteBuffer.withUnsafeBytes(body)
103+
return try byteBuffer.withUnsafeBytes(body)
104+
#if !os(WASI)
106105
case .data(let data):
107-
try data.withUnsafeBytes(body)
106+
return try data.withUnsafeBytes(body)
108107
case .bytes(let contiguousBytes):
109-
try contiguousBytes.withUnsafeBytes(body)
108+
return try contiguousBytes.withUnsafeBytes(body)
109+
#endif
110110
case .array(let array):
111-
try array.withUnsafeBytes(body)
111+
return try array.withUnsafeBytes(body)
112112
case .pointer(let ptr):
113-
try body(UnsafeRawBufferPointer(start: ptr, count: capacity))
114-
case .writePointer(let ptr):
115-
try body(UnsafeRawBufferPointer(start: ptr, count: capacity))
113+
return try body(UnsafeRawBufferPointer(start: ptr, count: capacity))
116114
}
117115
}
118116

117+
@discardableResult
119118
@inline(__always)
120119
func withUnsafeRawPointer<T>(
121120
_ body: (UnsafeMutableRawPointer) throws
122121
-> T) rethrows -> T
123122
{
124123
switch retainedBlob {
125124
case .byteBuffer(let byteBuffer):
126-
try byteBuffer.withUnsafeRawPointer(body)
125+
return try byteBuffer.withUnsafeRawPointer(body)
126+
#if !os(WASI)
127127
case .data(let data):
128-
try data
128+
return try data
129129
.withUnsafeBytes {
130130
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
131131
}
132132
case .bytes(let contiguousBytes):
133-
try contiguousBytes
133+
return try contiguousBytes
134134
.withUnsafeBytes {
135135
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
136136
}
137+
#endif
137138
case .array(let array):
138-
try array
139+
return try array
139140
.withUnsafeBytes {
140141
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
141142
}
142143
case .pointer(let ptr):
143-
try body(ptr)
144-
case .writePointer(let ptr):
145-
try body(ptr)
144+
return try body(ptr)
146145
}
147146
}
148147

148+
@discardableResult
149149
@inline(__always)
150150
func readWithUnsafeRawPointer<T>(
151151
position: Int,
152152
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
153153
{
154154
switch retainedBlob {
155155
case .byteBuffer(let byteBuffer):
156-
try byteBuffer.readWithUnsafeRawPointer(position: position, body)
156+
return try byteBuffer.readWithUnsafeRawPointer(position: position, body)
157+
#if !os(WASI)
157158
case .data(let data):
158-
try data.withUnsafeBytes {
159+
return try data.withUnsafeBytes {
159160
try body($0.baseAddress!.advanced(by: position))
160161
}
161162
case .bytes(let contiguousBytes):
162-
try contiguousBytes.withUnsafeBytes {
163+
return try contiguousBytes.withUnsafeBytes {
163164
try body($0.baseAddress!.advanced(by: position))
164165
}
166+
#endif
165167
case .array(let array):
166-
try array.withUnsafeBytes {
168+
return try array.withUnsafeBytes {
167169
try body($0.baseAddress!.advanced(by: position))
168170
}
169171
case .pointer(let ptr):
170-
try body(ptr.advanced(by: position))
171-
case .writePointer(let ptr):
172-
try body(ptr.advanced(by: position))
172+
return try body(ptr.advanced(by: position))
173173
}
174174
}
175175
}
@@ -178,10 +178,8 @@ public struct ByteBuffer {
178178

179179
/// The size of the elements written to the buffer + their paddings
180180
private var _readerIndex: Int = 0
181-
/// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer
182-
var writerIndex: Int { _storage.capacity &- _readerIndex }
183181
/// Reader is the position of the current Writer Index (capacity - size)
184-
public var reader: Int { writerIndex }
182+
public var reader: Int { _storage.capacity &- _readerIndex }
185183
/// Current size of the buffer
186184
public var size: UOffset { UOffset(_readerIndex) }
187185
/// Current capacity for the buffer
@@ -192,7 +190,9 @@ public struct ByteBuffer {
192190
/// - bytes: Array of UInt8
193191
@inline(__always)
194192
init(byteBuffer: _InternalByteBuffer) {
195-
_storage = Storage(blob: .byteBuffer(byteBuffer), capacity: byteBuffer.capacity)
193+
_storage = Storage(
194+
blob: .byteBuffer(byteBuffer),
195+
capacity: byteBuffer.capacity)
196196
_readerIndex = Int(byteBuffer.size)
197197
}
198198

@@ -293,7 +293,7 @@ public struct ByteBuffer {
293293
}
294294
assert(index < _storage.capacity, "Write index is out of writing bound")
295295
assert(index >= 0, "Writer index should be above zero")
296-
withUnsafePointer(to: value) { ptr in
296+
_ = withUnsafePointer(to: value) { ptr in
297297
_storage.withUnsafeRawPointer {
298298
memcpy(
299299
$0.advanced(by: index),
@@ -392,7 +392,7 @@ public struct ByteBuffer {
392392
assert(
393393
index + count <= _storage.capacity,
394394
"Reading out of bounds is illegal")
395-
return _storage.retainedBlob.readWithUnsafeRawPointer(position: index) {
395+
return _storage.readWithUnsafeRawPointer(position: index) {
396396
String(cString: $0.bindMemory(to: UInt8.self, capacity: count))
397397
}
398398
}
@@ -433,14 +433,22 @@ public struct ByteBuffer {
433433
}
434434

435435
@discardableResult
436-
@usableFromInline
437436
@inline(__always)
438437
func withUnsafeMutableRawPointer<T>(
439438
body: (UnsafeMutableRawPointer) throws
440439
-> T) rethrows -> T
441440
{
442441
try _storage.withUnsafeRawPointer(body)
443442
}
443+
444+
@discardableResult
445+
@inline(__always)
446+
func readWithUnsafeRawPointer<T>(
447+
position: Int,
448+
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
449+
{
450+
try _storage.readWithUnsafeRawPointer(position: position, body)
451+
}
444452
}
445453

446454
extension ByteBuffer: CustomDebugStringConvertible {
@@ -449,7 +457,8 @@ extension ByteBuffer: CustomDebugStringConvertible {
449457
"""
450458
buffer located at: \(_storage.retainedBlob),
451459
with capacity of \(_storage.capacity),
452-
{ writerSize: \(_readerIndex), readerSize: \(reader), writerIndex: \(writerIndex) }
460+
{ writtenSize: \(_readerIndex), readerSize: \(reader),
461+
size: \(size) }
453462
"""
454463
}
455464
}

0 commit comments

Comments
 (0)