Skip to content

Commit ba45f5b

Browse files
committed
Change ByteReader.offset implementation to a more efficient version by adding a new private dataStartIndex constant property
This change eliminates access to ByteReader.data property which is very time-consuming.
1 parent 5762a64 commit ba45f5b

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

Sources/ByteReader.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ public class ByteReader {
1717
/// Offset to the byte in `data` which will be read next.
1818
public var offset: Int {
1919
get {
20-
return self._offset + self.data.startIndex
20+
return self._offset + self.dataStartIndex
2121
}
2222
set {
23-
self._offset = newValue - self.data.startIndex
23+
self._offset = newValue - self.dataStartIndex
2424
}
2525
}
2626

2727
var _offset: Int
28-
2928
var ptr: UnsafeBufferPointer<UInt8>
29+
private let dataStartIndex: Int // For efficient (without access to `data`) implementation of `offset`.
3030

3131
/**
3232
True, if `offset` points at any position after the last byte in `data`.
@@ -55,6 +55,7 @@ public class ByteReader {
5555
self.ptr = data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) -> UnsafeBufferPointer<UInt8> in
5656
return UnsafeBufferPointer<UInt8>(start: ptr, count: data.count)
5757
}
58+
self.dataStartIndex = data.startIndex
5859
}
5960

6061
/**

0 commit comments

Comments
 (0)