Skip to content

Commit

Permalink
Use spans, its faster
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Oct 5, 2024
1 parent 820d874 commit 8b259e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions Img2Ffu.Library/Writer/FFUFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,16 @@ private static void WriteFFUFile(
DateTime startTime = DateTime.Now;
ulong totalBytes = (ulong)BlockPayloads.LongCount() * BlockSize;

for (ulong CurrentBlockIndex = 0; CurrentBlockIndex < (ulong)BlockPayloads.LongCount(); CurrentBlockIndex++)
{
BlockPayload BlockPayload = BlockPayloads.ElementAt((int)CurrentBlockIndex).Value;
byte[] BlockBuffer = BlockPayload.ReadBlock(BlockSize);
Memory<byte> BlockBuffer = new byte[BlockSize];

FFUFileStream.Write(BlockBuffer, 0, (int)BlockSize);
ulong CurrentBlockIndex = 0;
foreach (KeyValuePair<ByteArrayKey, BlockPayload> BlockPayload in BlockPayloads)
{
BlockPayload.Value.ReadBlock(BlockBuffer.Span);
FFUFileStream.Write(BlockBuffer.Span);

ulong bytesRead = CurrentBlockIndex * BlockSize;
CurrentBlockIndex++;

LoggingHelpers.ShowProgress(totalBytes, bytesRead, bytesRead, startTime, Logging);
}
Expand Down
12 changes: 7 additions & 5 deletions Img2Ffu.Library/Writer/Flashing/BlockPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public class BlockPayload(WriteDescriptor WriteDescriptor, Stream Stream, ulong
public Stream Stream = Stream;
public ulong FlashPartStreamLocation = StreamLocation;

internal byte[] ReadBlock(ulong BlockSize)
internal void ReadBlock(byte[] BlockBuffer)
{
_ = Stream.Seek((long)FlashPartStreamLocation, SeekOrigin.Begin);
_ = Stream.Read(BlockBuffer, 0, (int)BlockBuffer.Length);
}

byte[] BlockBuffer = new byte[BlockSize];
_ = Stream.Read(BlockBuffer, 0, (int)BlockSize);

return BlockBuffer;
internal void ReadBlock(Span<byte> BlockBuffer)
{
_ = Stream.Seek((long)FlashPartStreamLocation, SeekOrigin.Begin);
_ = Stream.Read(BlockBuffer);
}
}
}

0 comments on commit 8b259e7

Please sign in to comment.