Skip to content

Commit d7cd61a

Browse files
committed
Be a bit more frugal with our compression buffer
For small files (and the remaining bit of large ones, though this is not as big of a deal), the compression buffer can be sized based on the remaining data rather than the block size. This often allows it to avoid being classified as a "medium sized" allocation, saving an expensive call to madvise when the buffer is freed.
1 parent 415f5ca commit d7cd61a

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

unxip.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ struct File {
127127
compressionStream.addTask {
128128
try Task.checkCancellation()
129129
let position = _position
130-
let data = [UInt8](unsafeUninitializedCapacity: blockSize + blockSize / 16) { buffer, count in
131-
data[position..<min(position + blockSize, data.endIndex)].withUnsafeBufferPointer { data in
130+
let end = min(position + blockSize, data.endIndex)
131+
let data = [UInt8](unsafeUninitializedCapacity: (end - position) + (end - position) / 16) { buffer, count in
132+
data[position..<end].withUnsafeBufferPointer { data in
132133
count = compression_encode_buffer(buffer.baseAddress!, buffer.count, data.baseAddress!, data.count, nil, COMPRESSION_LZFSE)
133134
guard count < buffer.count else {
134135
count = 0

0 commit comments

Comments
 (0)