dbwrapper: use SpanReader for iterator keys

`CDBIterator::GetKey()` only deserializes the current LevelDB key once.
`GetKeyImpl()` already exposes the current key as a contiguous borrowed byte span, and `GetKey()` creates a fresh local reader and only performs immediate forward reads before returning.

Switch this path to `SpanReader` so the key bytes are read in place instead of being copied into a temporary `DataStream`.
This keeps the same exception swallowing and `bool` return semantics while avoiding the extra allocation and copy.

The preceding test locks down the subtle safety property that matters here: a failed decode must not consume the current iterator entry.
Note that the same simplification does not apply to `GetValue()`, because that path deobfuscates the value bytes in place first and still needs an owning mutable buffer.
This commit is contained in:
Lőrinc
2026-04-21 11:56:49 +02:00
parent f0e498af5c
commit 5de2f97a05
2 changed files with 3 additions and 1 deletions

View File

@@ -370,6 +370,8 @@ void CDBIterator::SeekImpl(std::span<const std::byte> key)
std::span<const std::byte> CDBIterator::GetKeyImpl() const
{
// The returned span borrows from the current iterator entry and is only
// valid until the iterator is advanced.
return MakeByteSpan(m_impl_iter->iter->key());
}

View File

@@ -153,7 +153,7 @@ public:
template<typename K> bool GetKey(K& key) {
try {
DataStream ssKey{GetKeyImpl()};
SpanReader ssKey{GetKeyImpl()};
ssKey >> key;
} catch (const std::exception&) {
return false;