Skip to content

Commit

Permalink
Improved perfoermance by not copying read() buffer into Data object
Browse files Browse the repository at this point in the history
  • Loading branch information
amosavian committed Jul 17, 2018
1 parent 8ee3118 commit db87cfb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
17 changes: 4 additions & 13 deletions AMSMB2/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,25 @@ final class SMB2FileHandle {
precondition(length <= UInt32.max, "Length bigger than UInt32.max can't be handled by libsmb2.")

let bufSize = length > 0 ? length : optimizedReadSize
var buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufSize)
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufSize)
buffer.initialize(repeating: 0, count: bufSize)
defer {
buffer.deinitialize(count: bufSize)
buffer.deallocate()
}

let (result, _) = try context.async_await(defaultError: .EIO) { (context, cbPtr) -> Int32 in
smb2_read_async(context, handle, buffer, UInt32(bufSize), SMB2Context.async_handler, cbPtr)
}
return Data(bytes: buffer, count: Int(result))
return Data(bytesNoCopy: buffer, count: Int(result), deallocator: .free)
}

func pread(offset: UInt64, length: Int = 0) throws -> Data {
precondition(length <= UInt32.max, "Length bigger than UInt32.max can't be handled by libsmb2.")

let bufSize = length > 0 ? length : optimizedReadSize
var buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufSize)
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufSize)
buffer.initialize(repeating: 0, count: bufSize)
defer {
buffer.deinitialize(count: bufSize)
buffer.deallocate()
}

let (result, _) = try context.async_await(defaultError: .EIO) { (context, cbPtr) -> Int32 in
smb2_pread_async(context, handle, buffer, UInt32(bufSize), offset, SMB2Context.async_handler, cbPtr)
}
return Data(bytes: buffer, count: Int(result))
return Data(bytesNoCopy: buffer, count: Int(result), deallocator: .free)
}

var maxWriteSize: Int {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Or add this to Cartfile:
github "amosavian/AMSMB2"
```

###Manually
### Manually

To have latest updates with ease, use this command on terminal to get a clone:

Expand Down

0 comments on commit db87cfb

Please sign in to comment.