Skip to content

Commit d6e1754

Browse files
committed
Add a comment to ByteReader on decision making behind this implementation
1 parent f74aa62 commit d6e1754

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

Sources/ByteReader.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ public class ByteReader {
2525
}
2626

2727
var _offset: Int
28+
29+
/*
30+
Generally speaking, what we are doing here is not really safe: pointer can become invalid if the storage of `data`
31+
is non-contiguous, but (unfortunately, in a way) I was unable to produce an example which would cause any issues.
32+
33+
Despite this, it was still decided to use this implementation strategy because:
34+
1. We cannot use `Data.withUnsafeBytes` because it is ridiculously slow.
35+
2. We cannot use `Data.copyBytes` because, well, it copies bytes.
36+
3. We would like to switch to `UnsafePointer` instead of `Data` to eliminate redundant out of bounds checks
37+
(because we check indices ourselves and can guarantee that they are correct), and, thus, significantly improve
38+
performance.
39+
40+
Finally, it is worth mentioning that `NSData` actually has a convenient property `bytes` which provides access
41+
to the pointer to the storage, but, for some reason, `Data` doesn't have it. We also cannot convert between `Data`
42+
to `NSData` using `as` operator since it is not supposed to work on non-Darwin platforms (but, surprisingly, it
43+
works with Swift 4.2).
44+
*/
2845
final let ptr: UnsafeBufferPointer<UInt8>
2946
private final let dataStartIndex: Int // For efficient (without access to `data`) implementation of `offset`.
3047

0 commit comments

Comments
 (0)