-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Reactivate "Performance improvements for Blob" #1167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
773ac26
Reactivate #416
jberkel 3b91a00
Fix test
jberkel 0715e95
Fix blob equality
jberkel 0ce78d9
Remove helper
jberkel 932d580
Fix SQLCipher subspec
jberkel b3a21ca
Avoid NSMutableData
jberkel bf021c5
Update changelog
jberkel 06e55e7
Simplify
jberkel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,26 +21,50 @@ | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
import Foundation | ||
|
||
public struct Blob { | ||
public final class Blob { | ||
|
||
public let bytes: [UInt8] | ||
public let data: NSData | ||
|
||
public init(bytes: [UInt8]) { | ||
self.bytes = bytes | ||
public var bytes: UnsafePointer<UInt8> { | ||
data.bytes.assumingMemoryBound(to: UInt8.self) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unfortunately undefined behavior and could lead to crashes. We should revert this. |
||
} | ||
|
||
public init(bytes: UnsafeRawPointer, length: Int) { | ||
let i8bufptr = UnsafeBufferPointer(start: bytes.assumingMemoryBound(to: UInt8.self), count: length) | ||
self.init(bytes: [UInt8](i8bufptr)) | ||
public var length: Int { | ||
data.count | ||
} | ||
|
||
public func toHex() -> String { | ||
bytes.map { | ||
($0 < 16 ? "0" : "") + String($0, radix: 16, uppercase: false) | ||
}.joined(separator: "") | ||
public convenience init(bytes: [UInt8]) { | ||
guard bytes.count > 0 else { | ||
self.init(data: NSData()) | ||
return | ||
} | ||
self.init(data: NSData(bytes: bytes, length: bytes.count)) | ||
} | ||
|
||
public convenience init(bytes: UnsafeRawPointer, length: Int) { | ||
self.init(data: NSData(bytes: bytes, length: length)) | ||
} | ||
|
||
public init(data: NSData) { | ||
precondition(!(data is NSMutableData), "Blob cannot be initialized with mutable data") | ||
self.data = data | ||
} | ||
|
||
public func toHex() -> String { | ||
guard length > 0 else { return "" } | ||
|
||
var hex = "" | ||
for idx in 0..<length { | ||
let byte = bytes.advanced(by: idx).pointee | ||
if byte < 16 { | ||
hex += "0" | ||
} | ||
hex += String(byte, radix: 16, uppercase: false) | ||
} | ||
return hex | ||
} | ||
} | ||
|
||
extension Blob: CustomStringConvertible { | ||
|
@@ -56,5 +80,5 @@ extension Blob: Equatable { | |
} | ||
|
||
public func ==(lhs: Blob, rhs: Blob) -> Bool { | ||
lhs.bytes == rhs.bytes | ||
lhs.data == rhs.data | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these days
NSData
is going to make things slower in Swift. If we want to make fundamental changes like this they should be benchmarked. I have some other repos with examples that useswift-benchmark
if that would help!