diff options
| author | Dan Engelbrecht <[email protected]> | 2023-10-25 22:03:14 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-10-25 22:03:14 +0200 |
| commit | 28aaf25e651d274f1c52befdedf66b9768f415e8 (patch) | |
| tree | c117a285018344de06b323dca9e1fbc6f90084fa /src | |
| parent | 0.2.29-pre0 (diff) | |
| download | zen-28aaf25e651d274f1c52befdedf66b9768f415e8.tar.xz zen-28aaf25e651d274f1c52befdedf66b9768f415e8.zip | |
only measure a variable integer once where we can (#500)
Diffstat (limited to 'src')
| -rw-r--r-- | src/zencore/compactbinary.cpp | 4 | ||||
| -rw-r--r-- | src/zencore/include/zencore/varint.h | 22 |
2 files changed, 19 insertions, 7 deletions
diff --git a/src/zencore/compactbinary.cpp b/src/zencore/compactbinary.cpp index 5b599dda3..416b49f62 100644 --- a/src/zencore/compactbinary.cpp +++ b/src/zencore/compactbinary.cpp @@ -1319,7 +1319,7 @@ TryMeasureCompactBinary(MemoryView View, CbFieldType& OutType, uint64_t& OutSize return false; } - const uint64_t NameLen = ReadVarUInt(View.GetData(), NameLenByteCount); + const uint64_t NameLen = ReadMeasuredVarUInt(View.GetData(), NameLenByteCount); const uint64_t NameSize = NameLen + NameLenByteCount; if (bDynamicSize && View.GetSize() < NameSize) @@ -1353,7 +1353,7 @@ TryMeasureCompactBinary(MemoryView View, CbFieldType& OutType, uint64_t& OutSize OutSize = Size + PayloadSizeByteCount; return false; } - const uint64_t PayloadSize = ReadVarUInt(View.GetData(), PayloadSizeByteCount); + const uint64_t PayloadSize = ReadMeasuredVarUInt(View.GetData(), PayloadSizeByteCount); OutSize = Size + PayloadSize + PayloadSizeByteCount; } return true; diff --git a/src/zencore/include/zencore/varint.h b/src/zencore/include/zencore/varint.h index e57e1d497..c0d63d814 100644 --- a/src/zencore/include/zencore/varint.h +++ b/src/zencore/include/zencore/varint.h @@ -104,15 +104,12 @@ MeasureVarInt(int64_t InValue) * Read a variable-length unsigned integer. * * @param InData A variable-length encoding of an unsigned integer. - * @param OutByteCount The number of bytes consumed from the input. + * @param ByteCount The number of bytes to be consumed from the input. * @return An unsigned integer. */ inline uint64_t -ReadVarUInt(const void* InData, uint32_t& OutByteCount) +ReadMeasuredVarUInt(const void* InData, uint32_t ByteCount) { - const uint32_t ByteCount = MeasureVarUInt(InData); - OutByteCount = ByteCount; - const uint8_t* InBytes = static_cast<const uint8_t*>(InData); uint64_t Value = *InBytes++ & uint8_t(0xff >> ByteCount); switch (ByteCount - 1) @@ -155,6 +152,21 @@ ReadVarUInt(const void* InData, uint32_t& OutByteCount) } /** + * Read a variable-length unsigned integer. + * + * @param InData A variable-length encoding of an unsigned integer. + * @param OutByteCount The number of bytes consumed from the input. + * @return An unsigned integer. + */ +inline uint64_t +ReadVarUInt(const void* InData, uint32_t& OutByteCount) +{ + const uint32_t ByteCount = MeasureVarUInt(InData); + OutByteCount = ByteCount; + return ReadMeasuredVarUInt(InData, ByteCount); +} + +/** * Read a variable-length signed integer. * * @param InData A variable-length encoding of a signed integer. |