[Swift] Moves capacity outside of Storage (#8650)

- Cleans up capacity usage within the lib and moves it outside of the Storage

-  Use overflow operators
This commit is contained in:
mustiikhalil
2025-07-29 23:21:20 +02:00
committed by GitHub
parent f32a7dcbd2
commit 575d616e60
13 changed files with 121 additions and 110 deletions

View File

@@ -32,32 +32,18 @@ struct _InternalByteBuffer {
final class Storage {
/// pointer to the start of the buffer object in memory
var memory: UnsafeMutableRawPointer
/// Capacity of UInt8 the buffer can hold
var capacity: Int
@usableFromInline
init(count: Int, alignment: Int) {
memory = UnsafeMutableRawPointer.allocate(
byteCount: count,
alignment: alignment)
capacity = count
}
@usableFromInline
init(memory: UnsafeMutableRawPointer, capacity: Int, unowned: Bool) {
self.memory = memory
self.capacity = capacity
}
deinit {
memory.deallocate()
}
@usableFromInline
func copy(from ptr: UnsafeRawPointer, count: Int) {
memory.copyMemory(from: ptr, byteCount: count)
}
@usableFromInline
func initialize(for size: Int) {
memset(memory, 0, size)
@@ -66,14 +52,11 @@ struct _InternalByteBuffer {
/// Reallocates the buffer incase the object to be written doesnt fit in the current buffer
/// - Parameter size: Size of the current object
@usableFromInline
func reallocate(_ size: Int, writerSize: Int, alignment: Int) {
while capacity <= writerSize &+ size {
capacity = capacity << 1
}
/// solution take from Apple-NIO
capacity = capacity.convertToPowerofTwo
func reallocate(
capacity: Int,
writerSize: Int,
alignment: Int
) {
let newData = UnsafeMutableRawPointer.allocate(
byteCount: capacity,
alignment: alignment)
@@ -97,7 +80,7 @@ struct _InternalByteBuffer {
/// Public Pointer to the buffer object in memory. This should NOT be modified for any reason
public var memory: UnsafeMutableRawPointer { _storage.memory }
/// Current capacity for the buffer
public var capacity: Int { _storage.capacity }
public private(set) var capacity: Int
/// Returns the written bytes into the ``ByteBuffer``
public var underlyingBytes: [UInt8] {
@@ -113,6 +96,7 @@ struct _InternalByteBuffer {
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
init(initialSize size: Int) {
initialSize = size.convertToPowerofTwo
capacity = initialSize
_storage = Storage(count: initialSize, alignment: alignment)
_storage.initialize(for: initialSize)
}
@@ -123,8 +107,9 @@ struct _InternalByteBuffer {
writerIndex = 0
alignment = 1
if keepingCapacity {
_storage.initialize(for: _storage.capacity)
_storage.initialize(for: capacity)
} else {
capacity = initialSize
_storage = Storage(count: initialSize, alignment: alignment)
}
}
@@ -136,10 +121,22 @@ struct _InternalByteBuffer {
/// Makes sure that buffer has enouch space for each of the objects that will be written into it
/// - Parameter size: size of object
@inline(__always)
@usableFromInline
mutating func ensureSpace(size: Int) {
guard size &+ writerIndex > _storage.capacity else { return }
_storage.reallocate(size, writerSize: writerIndex, alignment: alignment)
guard size &+ writerIndex > capacity else { return }
while capacity <= writerIndex &+ size {
capacity = capacity << 1
}
/// solution take from Apple-NIO
capacity = capacity.convertToPowerofTwo
_storage.reallocate(
capacity: capacity,
writerSize: writerIndex,
alignment: alignment
)
}
@inline(__always)
@@ -215,7 +212,7 @@ extension _InternalByteBuffer: CustomDebugStringConvertible {
public var debugDescription: String {
"""
buffer located at: \(_storage.memory), with capacity of \(_storage.capacity)
buffer located at: \(_storage.memory), with capacity of \(capacity)
{ writerIndex: \(writerIndex) }
"""
}