mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 02:50:32 +00:00
feature: VarInt size prediction
This commit is contained in:
parent
cdf5edae0d
commit
54f6abb6ce
@ -293,6 +293,38 @@ public static Quaternion DecompressQuaternion(uint data)
|
||||
}
|
||||
|
||||
// varint compression //////////////////////////////////////////////////
|
||||
// helper function to predict varint size for a given number.
|
||||
// useful when checking if a message + size header will fit, etc.
|
||||
public static int VarUIntSize(ulong value)
|
||||
{
|
||||
if (value <= 240)
|
||||
return 1;
|
||||
if (value <= 2287)
|
||||
return 2;
|
||||
if (value <= 67823)
|
||||
return 3;
|
||||
if (value <= 16777215)
|
||||
return 4;
|
||||
if (value <= 4294967295)
|
||||
return 5;
|
||||
if (value <= 1099511627775)
|
||||
return 6;
|
||||
if (value <= 281474976710655)
|
||||
return 7;
|
||||
if (value <= 72057594037927935)
|
||||
return 8;
|
||||
return 9;
|
||||
}
|
||||
|
||||
// helper function to predict varint size for a given number.
|
||||
// useful when checking if a message + size header will fit, etc.
|
||||
public static int VarIntSize(long value)
|
||||
{
|
||||
// CompressVarInt zigzags it first
|
||||
ulong zigzagged = (ulong)((value >> 63) ^ (value << 1));
|
||||
return VarUIntSize(zigzagged);
|
||||
}
|
||||
|
||||
// compress ulong varint.
|
||||
// same result for ulong, uint, ushort and byte. only need one function.
|
||||
// NOT an extension. otherwise weaver might accidentally use it.
|
||||
|
@ -386,5 +386,53 @@ public void VarUInt()
|
||||
Assert.That(Compression.DecompressVarUInt(reader), Is.EqualTo(72057594037927935));
|
||||
Assert.That(Compression.DecompressVarUInt(reader), Is.EqualTo(ulong.MaxValue));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCase(0ul)]
|
||||
[TestCase(234ul)]
|
||||
[TestCase(2284ul)]
|
||||
[TestCase(67821ul)]
|
||||
[TestCase(16777210ul)]
|
||||
[TestCase(16777219ul)]
|
||||
[TestCase(4294967295ul)]
|
||||
[TestCase(1099511627775ul)]
|
||||
[TestCase(281474976710655ul)]
|
||||
[TestCase(72057594037927935ul)]
|
||||
[TestCase(ulong.MaxValue)]
|
||||
public void VarUIntSize(ulong number)
|
||||
{
|
||||
NetworkWriter writer = new NetworkWriter();
|
||||
Compression.CompressVarUInt(writer, number);
|
||||
Assert.That(writer.Position, Is.EqualTo(Compression.VarUIntSize(number)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCase(long.MinValue)]
|
||||
[TestCase(-72057594037927935)]
|
||||
[TestCase(-281474976710655)]
|
||||
[TestCase(-1099511627775)]
|
||||
[TestCase(-4294967295)]
|
||||
[TestCase(-16777219)]
|
||||
[TestCase(-16777210)]
|
||||
[TestCase(-67821)]
|
||||
[TestCase(-2284)]
|
||||
[TestCase(-234)]
|
||||
[TestCase(0)]
|
||||
[TestCase(234)]
|
||||
[TestCase(2284)]
|
||||
[TestCase(67821)]
|
||||
[TestCase(16777210)]
|
||||
[TestCase(16777219)]
|
||||
[TestCase(4294967295)]
|
||||
[TestCase(1099511627775)]
|
||||
[TestCase(281474976710655)]
|
||||
[TestCase(72057594037927935)]
|
||||
[TestCase(long.MaxValue)]
|
||||
public void VarIntSize(long number)
|
||||
{
|
||||
NetworkWriter writer = new NetworkWriter();
|
||||
Compression.CompressVarInt(writer, number);
|
||||
Assert.That(writer.Position, Is.EqualTo(Compression.VarIntSize(number)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user