Skip to content

Commit 861ce64

Browse files
committed
Cache current byte in bit readers
This seems to improve performance of bit() function and also, probably, int(fromBits:).
1 parent 76d06c7 commit 861ce64

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

Sources/LsbBitReader.swift

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import Foundation
1212
public final class LsbBitReader: ByteReader, BitReader {
1313

1414
private var bitMask: UInt8 = 1
15+
private var currentByte: UInt8
16+
17+
/// Creates an instance for reading bits (and bytes) from `data`.
18+
public override init(data: Data) {
19+
if data.count > 0 {
20+
self.currentByte = data[0]
21+
} else {
22+
self.currentByte = 0
23+
}
24+
super.init(data: data)
25+
}
1526

1627
/// True, if reader's BIT pointer is aligned with the BYTE border.
1728
public var isAligned: Bool {
@@ -25,7 +36,7 @@ public final class LsbBitReader: ByteReader, BitReader {
2536
to check if the end is reached.
2637
*/
2738
public func bit() -> UInt8 {
28-
let bit: UInt8 = self.data[self.offset] & self.bitMask > 0 ? 1 : 0
39+
let bit: UInt8 = self.currentByte & self.bitMask > 0 ? 1 : 0
2940

3041
if self.bitMask == 128 {
3142
self.offset += 1
@@ -49,10 +60,8 @@ public final class LsbBitReader: ByteReader, BitReader {
4960

5061
var result = 0
5162
for i in 0..<count {
52-
let power = i
53-
54-
let bit = self.data[self.offset] & self.bitMask > 0 ? 1 : 0
55-
result += (1 << power) * bit
63+
let bit = self.currentByte & self.bitMask > 0 ? 1 : 0
64+
result += (1 << i) * bit
5665

5766
if self.bitMask == 128 {
5867
self.offset += 1
@@ -82,6 +91,15 @@ public final class LsbBitReader: ByteReader, BitReader {
8291

8392
// MARK: ByteReader's methods.
8493

94+
/// Offset to the byte in `data` which will be read next.
95+
public override var offset: Int {
96+
didSet {
97+
if !self.isFinished {
98+
self.currentByte = self.data[self.offset]
99+
}
100+
}
101+
}
102+
85103
/**
86104
Reads byte and returns it, advancing by one BYTE position.
87105

Sources/MsbBitReader.swift

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import Foundation
1212
public final class MsbBitReader: ByteReader, BitReader {
1313

1414
private var bitMask: UInt8 = 128
15+
private var currentByte: UInt8
16+
17+
/// Creates an instance for reading bits (and bytes) from `data`.
18+
public override init(data: Data) {
19+
if data.count > 0 {
20+
self.currentByte = data[0]
21+
} else {
22+
self.currentByte = 0
23+
}
24+
super.init(data: data)
25+
}
1526

1627
/// True, if reader's BIT pointer is aligned with the BYTE border.
1728
public var isAligned: Bool {
@@ -25,7 +36,7 @@ public final class MsbBitReader: ByteReader, BitReader {
2536
to check if the end is reached.
2637
*/
2738
public func bit() -> UInt8 {
28-
let bit: UInt8 = self.data[self.offset] & self.bitMask > 0 ? 1 : 0
39+
let bit: UInt8 = self.currentByte & self.bitMask > 0 ? 1 : 0
2940

3041
if self.bitMask == 1 {
3142
self.offset += 1
@@ -49,10 +60,8 @@ public final class MsbBitReader: ByteReader, BitReader {
4960

5061
var result = 0
5162
for i in 0..<count {
52-
let power = count - i - 1
53-
54-
let bit = self.data[self.offset] & self.bitMask > 0 ? 1 : 0
55-
result += (1 << power) * bit
63+
let bit = self.currentByte & self.bitMask > 0 ? 1 : 0
64+
result += (1 << (count - i - 1)) * bit
5665

5766
if self.bitMask == 1 {
5867
self.offset += 1
@@ -82,6 +91,15 @@ public final class MsbBitReader: ByteReader, BitReader {
8291

8392
// MARK: ByteReader's methods.
8493

94+
/// Offset to the byte in `data` which will be read next.
95+
public override var offset: Int {
96+
didSet {
97+
if !self.isFinished {
98+
self.currentByte = self.data[self.offset]
99+
}
100+
}
101+
}
102+
85103
/**
86104
Reads byte and returns it, advancing by one BYTE position.
87105

0 commit comments

Comments
 (0)