Follow-up: VB.NET: return a new byte array that does not contain ‘3’ bytes, and number of skipped bytes

image

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 Code Review 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).

This is a follow-up question because the response to my previous question already suggested that I should write unit tests and use a separate class as well. The return tuple must contain a byte[] instead of a List(of Byte), as I will need it later for other functions.I would like to have this function optimized for performance. Thanks for your input!

I created a test project / minimal compilable example for you:

FormMain.vb (if you need)

Class with that method

NUnit tests

A small improvement is to initialize the list of results by specifying a large enough capacity so that adding bytes doesn't require resizing its internal array.

Lists start with a small array size of 4 and then double the array size if the array gets too small. Resizing the array creates a new array and then copies all the elements from the old array to the new one.

Since currentBitPosition is always increased by 8, bitIndexWithinByte which is set to currentBitPosition Mod 8 will never change. It is used to align the current bit position to full bytes. Since this alignment must be done only once, do it before the While-loop.

Why carry along the currentBitPosition? All you need inside the loop is currentByteIndex calculated from it. Do this calculation before the loop as well and directly increase currentByteIndex by 1 instead.

The work within the loop has been reduced and the unit tests are still running.

I would not be astonished if converting this code to C# would improve speed even more. If you need it in a VB project, you can create a C# Library project (a DLL) and add a project reference in the VB app.

The compiler (especially C#), library, JIT and CLR teams are investing an immense effort in performance improvements in every .NET version. Use the newest .NET version available.

Ask AI
#1 #2 #3 #4 #5 #6 #7 #8 #9 #10 #11 #12 #13 #14 #15 #16 #17 #18 #19 #20 #21 #22 #23 #24 #25 #26 #27 #28 #29 #30 #31 #32 #33 #34 #35 #36 #37 #38 #39 #40 #41 #42 #43 #44 #45 #46 #47 #48 #49 #50 #51 #52 #53 #54 #55 #56 #57 #58 #59 #60 #61 #62 #63 #64 #65 #66 #67 #68 #69 #70