Skip to content

Commit a438656

Browse files
committed
optimize BitstreamScanner by avoiding bit list materialization
1 parent fa94c7b commit a438656

2 files changed

Lines changed: 87 additions & 23 deletions

File tree

‎pyarinc/arinc717/bitstream.py‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import logging
4-
from collections.abc import Iterable
54

65
logger = logging.getLogger(__name__)
76

@@ -19,33 +18,33 @@ def __init__(self, sync_pattern: int, sync_length: int = 16) -> None:
1918
self.sync_length = sync_length
2019

2120
def find_sync_positions(self, data: bytes) -> list[int]:
22-
"""Return list of bit offsets where the sync pattern occurs.
23-
24-
This is a deterministic, exact-match scanner. It can be extended to
25-
handle fuzzy matches or bit slips.
26-
"""
27-
bits = list(self._bits_from_bytes(data))
21+
"""Return list of bit offsets where the sync pattern occurs using direct byte windowing."""
2822
positions: list[int] = []
29-
total = len(bits)
30-
if self.sync_length <= 0 or self.sync_length > total:
23+
total_bits = len(data) * 8
24+
if self.sync_length <= 0 or self.sync_length > total_bits:
3125
return positions
32-
# build integer window
26+
3327
mask = (1 << self.sync_length) - 1
3428
window = 0
35-
# preload first sync_length-1 bits
36-
for i in range(self.sync_length - 1):
37-
window = (window << 1) | bits[i]
38-
for i in range(self.sync_length - 1, total):
39-
window = ((window << 1) & mask) | bits[i]
40-
# compute pattern as integer
29+
30+
# Preload the sliding window bit by bit
31+
for bit_idx in range(self.sync_length - 1):
32+
byte_pos = bit_idx >> 3
33+
bit_shift = 7 - (bit_idx & 7)
34+
bit = (data[byte_pos] >> bit_shift) & 1
35+
window = (window << 1) | bit
36+
37+
# Scan through the rest of the stream
38+
for bit_idx in range(self.sync_length - 1, total_bits):
39+
byte_pos = bit_idx >> 3
40+
bit_shift = 7 - (bit_idx & 7)
41+
bit = (data[byte_pos] >> bit_shift) & 1
42+
43+
window = ((window << 1) & mask) | bit
44+
4145
if window == self.sync_pattern:
42-
pos = i - (self.sync_length - 1)
46+
pos = bit_idx - (self.sync_length - 1)
4347
positions.append(pos)
48+
4449
logger.debug("Found %d sync positions", len(positions))
4550
return positions
46-
47-
@staticmethod
48-
def _bits_from_bytes(data: bytes) -> Iterable[int]:
49-
for b in data:
50-
for i in range(7, -1, -1):
51-
yield (b >> i) & 1

‎tests/test_arinc717_bitstream.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from pyarinc.arinc717.aligned import AlignedStream
24
from pyarinc.arinc717.bitstream import BitstreamScanner
35

@@ -66,3 +68,66 @@ def test_missing_frame_insertion():
6668
frames = list(aligned.iter_frames())
6769
# we expect two frames extracted sequentially
6870
assert len(frames) >= 2
71+
72+
73+
@pytest.mark.parametrize(
74+
"sync_pat, sync_len, bitstream, expected_pos",
75+
[
76+
(0b11, 2, "001100", 2), # Simple short sync pattern
77+
(
78+
0x257,
79+
12,
80+
"000000000000" + "001001010111" + "1111",
81+
12,
82+
), # Standard 717 pattern
83+
(0xF, 4, "1111", 0), # Sync at the very beginning
84+
(0xF, 4, "000000001111", 8), # Sync at the very end
85+
],
86+
)
87+
def test_bitstream_scanner_parametrized(sync_pat, sync_len, bitstream, expected_pos):
88+
data = bits_to_bytes(bitstream)
89+
scanner = BitstreamScanner(sync_pat, sync_len)
90+
positions = scanner.find_sync_positions(data)
91+
assert positions
92+
assert expected_pos in positions
93+
94+
95+
def test_bitstream_scanner_edge_cases():
96+
# Sync length longer than data
97+
scanner = BitstreamScanner(0b1010, 16)
98+
assert scanner.find_sync_positions(b"\x0A") == []
99+
100+
# Zero or negative sync length
101+
scanner_zero = BitstreamScanner(0b1010, 0)
102+
assert scanner_zero.find_sync_positions(b"\x0A\x0B") == []
103+
104+
# Empty data
105+
scanner_empty = BitstreamScanner(0b10, 2)
106+
assert scanner_empty.find_sync_positions(b"") == []
107+
108+
109+
def test_aligned_stream_truncated_tail():
110+
# Setup data where a full frame requires 4 bytes total, but we leave only 2 bytes at the end
111+
sync = 0b11110000
112+
sync_len = 8
113+
# Frame size configuration: 2 subframes * 1 word per subframe * 8 bits = 2 bytes per frame (+ sync)
114+
# Let's make frame size 4 bytes by setting words_per_subframe=2, subframes_per_frame=2
115+
# Total frame size = 2 * 2 * 8 bits = 32 bits = 4 bytes.
116+
frame1 = bytes([0xAA, 0xBB, 0xCC, 0xDD]) # Full 4-byte frame
117+
truncated_tail = bytes([0x11, 0x22]) # Only 2 bytes left (incomplete frame)
118+
119+
data = bytes([sync]) + frame1 + truncated_tail
120+
121+
aligned = AlignedStream.from_bitstream(
122+
data,
123+
sync_pattern=sync,
124+
sync_length=sync_len,
125+
word_bits=8,
126+
words_per_subframe=2,
127+
subframes_per_frame=2, # Requires 4 bytes per frame
128+
)
129+
frames = list(aligned.iter_frames())
130+
131+
# Should safely drop the truncated 2-byte tail and only return the 1 valid frame
132+
assert len(frames) == 1
133+
assert frames[0].frame_index == 0

0 commit comments

Comments
 (0)