Bit-stream and memory allocation


I have a library, that receives a bit-stream. In this stream I want to detect sync-words. If the sync word is at the right place, I read out the data from the stream.
For the data it's important to know how long it took to receive the data, so I also read out the sync word in the beginning of the stream and start a counter when the sync-word is there.
The sync word is 16bits and the data is 64bits

The library works fine so far, but I think I could do a lot of optimization for memory-usage (the library is no_std, therefore I want to squeeze out what I can)

My question is, what is the most memory efficiant way to do this.


  • Reading out the actual data is not an issue in that case. I read out Timecode data, that is encoded in the bits in a 10base. So I have to read out the single bits in that case anyway and calculate with them. This happens only between 24 and 30 times per second. I'm more focused on the operations that happen between 1920 and 2400 times per second.

Current solution: I have a u64 and u16. Then I shift the bits on both values when I receive another bit from the stream. When the sync-word is in the storage for u16 I can calculate the data.

  • Reading the sync-word from u16 is fast and handy, because I just have to compare two u16.
  • I have to bitshift two values and pass a bit from one to another. That needs an overhead of memory allocation.
  • I allocate too much memory to detect when the sync-word is received in the beginning to start the count, because I read the sync-word from a u64, then I have to typecast it to u16, when u16 would be enough.

Possible solution: I split up the stream into u16-u64-u16 or even u16-u32-u16-u16.

  • Detecting the sync word on start or end gets really fast
  • More complex operation and more memory allocation on bit-shifting

Possible solution: I use a u128

  • Fast and memory efficient bit shifting
  • Slow and memory heavy Sync-Word detections

Using [bool;80]
Upsides/downsides: I don't know

Using bitvec:

Something I didn't consider yet.

My current code is here:

Can someone push me in the right direction on this issue?