Skip to main content
Code Review

Return to Revisions

4 of 5
added unit tests and a separate class for the function, based on jmoreno's answer
Daniel
  • 313
  • 1
  • 8

VB.NET: optimize function to return a new byte array that does not contain ‘3’ bytes, and number of skipped bytes

I have written a VB.NET function that takes a byte array, a starting position (in bits), and a number of bits as input. The goal is to remove any occurrences of 3 from the specified range being searched. The function should return the new byte array with the specified length and the information about how many bytes were skipped.

Example: Given the byte array {103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128}, the starting position (in bits) is 106, and the number of bits is 32. The function should then return {64, 0, 0, 0, 64} (note the skipped 3) and a value of 1. There are 40 bits here, instead of the 32 specified, because the last byte has already started or because the starting position in bits was not aligned to bit position 0. The function should also return the number of skipped bytes so that I can update the position in the bitstream accordingly.

Edge case: If the last read byte is a 3, and it is partially read, it should not be skipped.

The original byte array must not be modified.

I quickly wrote the function a while ago, and it works. However, I am posting this on CodeReview to get suggestions to increase the cpu performance.

The use case is parsing specific items in H.264. Occasionally, there are threes between zero-bytes so that the decoder does not confuse the zero-bytes with a new frame (to put it simply).

I created a test project for you:

Edit January 5th, 2025. Added unit tests and separate class

Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Friend NotInheritable Class FormMain
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 Dim byteArray As Byte() = New List(Of Byte)() From {103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128}.ToArray()
 Dim result As (Byte(), Integer) = Decoder.ExtractRelevantBytesWithout3(byteArray, 106, 32)
 Dim result2 As (Byte(), Integer) = Decoder.ExtractRelevantBytesWithout3(Array.Empty(Of Byte)(), 220, 32)
 End Sub
End Class
Option Strict On
Imports System
Imports System.Collections.Generic
Public Class Decoder
 Public Shared Function ExtractRelevantBytesWithout3(byteArray As Byte(), bitStartPosition As Integer, bitCount As Integer) As (Byte(), numberOfSkippedBytes As Integer)
 If bitStartPosition > byteArray.Length * 8 Then
 Throw New ArgumentException("The bit start position exceeds the size of the byte array.", NameOf(bitStartPosition))
 End If
 Dim result As New List(Of Byte)()
 Dim bitsRemaining As Integer = bitCount
 Dim currentBitPosition As Integer = bitStartPosition
 Dim numberOfSkippedBytes As Integer = 0
 Dim increaseOnce As Boolean = True
 While bitsRemaining > 0 AndAlso currentBitPosition \ 8 < byteArray.Length
 Dim currentByteIndex As Integer = currentBitPosition \ 8
 Dim bitIndexWithinByte As Integer = currentBitPosition Mod 8
 If bitIndexWithinByte > 0 AndAlso increaseOnce Then
 bitsRemaining += 8
 increaseOnce = False
 End If
 Dim currentByte As Byte = byteArray(currentByteIndex)
 If currentByte = CByte(3) AndAlso bitsRemaining > 8 Then ' bitsRemaining > 8 means: Do not skip if the 3 is not read completely.
 currentBitPosition += 8 ' Skip to the next byte
 numberOfSkippedBytes += 1
 Continue While
 End If
 result.Add(currentByte)
 currentBitPosition += 8
 bitsRemaining -= Math.Min(bitsRemaining, 8) ' Deduct processed bits
 End While
 Return (result.ToArray(), numberOfSkippedBytes)
 End Function
End Class

Unit tests (in C# due to separate class library)

using NUnit.Framework;
using System;
using System.Collections.Generic;
namespace ExtractRelevantBytesWithout3.Tests
{
 public class Remove3sTest
 {
 [SetUp]
 public void Setup()
 {
 }
 [Test]
 public void ByteArrayDoesNotContain3_ReturnsOriginalArray()
 {
 // Arrange
 byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 0, 64, 0, 0, 15, 198, 12, 101, 128 }.ToArray();
 // Act
 (byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 106, bitCount: 32);
 // Assert
 Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 0, 64 }));
 Assert.That(result.skippedBytes, Is.EqualTo(0));
 }
 [Test]
 public void ByteArrayContains3_Removes3()
 {
 // Arrange
 byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
 // Act
 (byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 106, bitCount: 32);
 // Assert
 Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 0, 64 }));
 Assert.That(result.skippedBytes, Is.EqualTo(1));
 }
 [Test]
 public void ByteArrayContains3_3isPartiallyRead_ReturnsArrayWith3()
 {
 // Arrange
 byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
 // Act
 (byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 146, bitCount: 32);
 // Assert
 Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 15, 3 }));
 Assert.That(result.skippedBytes, Is.EqualTo(0));
 }
 /* Edge Cases */
 [Test]
 public void ByteArrayIsEmpty_ReturnsEmpty()
 {
 // Arrange
 byte[] originalArray = Array.Empty<byte>();
 // Act
 (byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 0, bitCount: 1);
 // Assert
 Assert.That(result.Item1, Is.Empty);
 Assert.That(result.skippedBytes, Is.EqualTo(0));
 }
 [Test]
 public void BitPositionGreaterThanArraySize_ThrowsArgumentException()
 {
 // Arrange
 byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
 // Act & Assert
 Assert.Throws<ArgumentException>(() =>
 Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 217, bitCount: 32));
 }
 [Test]
 public void LargeArrayPerformanceTest()
 {
 // Arrange
 byte[] largeArray = new byte[1000000];
 for (int i = 0; i < largeArray.Length; i++)
 {
 largeArray[i] = (byte)(i % 256);
 }
 // Act
 (byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(largeArray, bitStartPosition: 0, bitCount: largeArray.Length * 8);
 // Assert
 Assert.That(result.Item1.Length > 0);
 Assert.That(result.skippedBytes, Is.GreaterThan(0));
 }
 }
}
Daniel
  • 313
  • 1
  • 8
lang-vb

AltStyle によって変換されたページ (->オリジナル) /