Skip to content

Instantly share code, notes, and snippets.

@halter73
Last active March 29, 2019 06:26
Show Gist options
  • Save halter73/af2b9f78978f83813b19e187c4e5309e to your computer and use it in GitHub Desktop.
Save halter73/af2b9f78978f83813b19e187c4e5309e to your computer and use it in GitHub Desktop.
Fill chunk
using System;
using System.Diagnostics;
using System.Runtime.Intrinsics.X86;
public class Program
{
public static void Main()
{
uint NumPrefixHexDigits(uint dataLength) => (35 - Lzcnt.LeadingZeroCount(dataLength)) / 4;
uint lastPrefixSize = 0;
for (uint sizeAvailable = 6; sizeAvailable <= int.MaxValue; sizeAvailable++)
{
var candidate1PrefixSize = NumPrefixHexDigits(sizeAvailable);
var candidate1DataSize = sizeAvailable - candidate1PrefixSize - 4;
var candidate2PrefixSize = NumPrefixHexDigits(candidate1DataSize);
if (candidate1PrefixSize != candidate2PrefixSize)
{
var candidate2DataSize = sizeAvailable - candidate2PrefixSize - 4;
var candidate3PrefixSize = NumPrefixHexDigits(candidate2DataSize);
if (candidate3PrefixSize != candidate2PrefixSize)
{
Trace.Assert(candidate3PrefixSize == candidate2PrefixSize + 1);
Console.WriteLine($"Given {sizeAvailable} bytes from underlying PipeWriter, slice 1 extra byte off end of actualMemory.");
}
}
if (candidate2PrefixSize != lastPrefixSize)
{
lastPrefixSize = candidate2PrefixSize;
Console.WriteLine($"Starting at {sizeAvailable} bytes from underlying PipeWriter, expect prefix to be {candidate2PrefixSize} hex digits.");
}
}
}
}
> dotnet run -c Release
Starting at 6 bytes from underlying PipeWriter, expect prefix to be 1 hex digits.
Given 21 bytes from underlying PipeWriter, slice 1 extra byte off end of actualMemory.
Starting at 22 bytes from underlying PipeWriter, expect prefix to be 2 hex digits.
Given 262 bytes from underlying PipeWriter, slice 1 extra byte off end of actualMemory.
Starting at 263 bytes from underlying PipeWriter, expect prefix to be 3 hex digits.
Given 4103 bytes from underlying PipeWriter, slice 1 extra byte off end of actualMemory.
Starting at 4104 bytes from underlying PipeWriter, expect prefix to be 4 hex digits.
Given 65544 bytes from underlying PipeWriter, slice 1 extra byte off end of actualMemory.
Starting at 65545 bytes from underlying PipeWriter, expect prefix to be 5 hex digits.
Given 1048585 bytes from underlying PipeWriter, slice 1 extra byte off end of actualMemory.
Starting at 1048586 bytes from underlying PipeWriter, expect prefix to be 6 hex digits.
Given 16777226 bytes from underlying PipeWriter, slice 1 extra byte off end of actualMemory.
Starting at 16777227 bytes from underlying PipeWriter, expect prefix to be 7 hex digits.
Given 268435467 bytes from underlying PipeWriter, slice 1 extra byte off end of actualMemory.
Starting at 268435468 bytes from underlying PipeWriter, expect prefix to be 8 hex digits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment