Skip to content

Commit

Permalink
Be a bit more frugal with our compression buffer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
saagarjha committed Mar 16, 2022
1 parent 415f5ca commit d7cd61a
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions unxip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ struct File {
compressionStream.addTask {
try Task.checkCancellation()
let position = _position
let data = [UInt8](unsafeUninitializedCapacity: blockSize + blockSize / 16) { buffer, count in
data[position..<min(position + blockSize, data.endIndex)].withUnsafeBufferPointer { data in
let end = min(position + blockSize, data.endIndex)
let data = [UInt8](unsafeUninitializedCapacity: (end - position) + (end - position) / 16) { buffer, count in
data[position..<end].withUnsafeBufferPointer { data in
count = compression_encode_buffer(buffer.baseAddress!, buffer.count, data.baseAddress!, data.count, nil, COMPRESSION_LZFSE)
guard count < buffer.count else {
count = 0
Expand Down

0 comments on commit d7cd61a

Please sign in to comment.