-
Notifications
You must be signed in to change notification settings - Fork 39.2k
dbwrapper: reuse scratch DataStream buffers
#35156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,10 +79,10 @@ class CDBBatch | |
| struct WriteBatchImpl; | ||
| const std::unique_ptr<WriteBatchImpl> m_impl_batch; | ||
|
|
||
| DataStream ssKey{}; | ||
| DataStream ssValue{}; | ||
| DataStream m_key_scratch{}; | ||
| DataStream m_value_scratch{}; | ||
|
|
||
| void WriteImpl(std::span<const std::byte> key, DataStream& ssValue); | ||
| void WriteImpl(std::span<const std::byte> key, DataStream& value); | ||
| void EraseImpl(std::span<const std::byte> key); | ||
|
|
||
| public: | ||
|
|
@@ -96,22 +96,18 @@ class CDBBatch | |
| template <typename K, typename V> | ||
| void Write(const K& key, const V& value) | ||
| { | ||
| ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | ||
| ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE); | ||
| ssKey << key; | ||
| ssValue << value; | ||
| WriteImpl(ssKey, ssValue); | ||
| ssKey.clear(); | ||
| ssValue.clear(); | ||
| ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | ||
| m_key_scratch << key; | ||
| m_value_scratch << value; | ||
| WriteImpl(m_key_scratch, m_value_scratch); | ||
| } | ||
|
|
||
| template <typename K> | ||
| void Erase(const K& key) | ||
| { | ||
| ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | ||
| ssKey << key; | ||
| EraseImpl(ssKey); | ||
| ssKey.clear(); | ||
| ScopedDataStreamUsage scoped_key{m_key_scratch}; | ||
| m_key_scratch << key; | ||
| EraseImpl(m_key_scratch); | ||
|
Comment on lines
+108
to
+110
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is fine, but mixing between the scoped and member stream here is a bit jarring to read in my opinion. Might it be better to just access the scratch stream through the scoped stream instead?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, but I guess doing so would require implementing all of |
||
| } | ||
|
|
||
| size_t ApproximateSize() const; | ||
|
|
@@ -125,6 +121,7 @@ class CDBIterator | |
| private: | ||
| const CDBWrapper &parent; | ||
| const std::unique_ptr<IteratorImpl> m_impl_iter; | ||
| DataStream m_scratch{}; | ||
|
|
||
| void SeekImpl(std::span<const std::byte> key); | ||
| std::span<const std::byte> GetKeyImpl() const; | ||
|
|
@@ -144,10 +141,9 @@ class CDBIterator | |
| void SeekToFirst(); | ||
|
|
||
| template<typename K> void Seek(const K& key) { | ||
| DataStream ssKey{}; | ||
| ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | ||
| ssKey << key; | ||
| SeekImpl(ssKey); | ||
| ScopedDataStreamUsage scoped_scratch{m_scratch}; | ||
| m_scratch << key; | ||
| SeekImpl(m_scratch); | ||
| } | ||
|
|
||
| void Next(); | ||
|
|
@@ -164,9 +160,10 @@ class CDBIterator | |
|
|
||
| template<typename V> bool GetValue(V& value) { | ||
| try { | ||
| DataStream ssValue{GetValueImpl()}; | ||
| dbwrapper_private::GetObfuscation(parent)(ssValue); | ||
| ssValue >> value; | ||
| ScopedDataStreamUsage scoped_scratch{m_scratch}; | ||
| m_scratch.write(GetValueImpl()); | ||
| dbwrapper_private::GetObfuscation(parent)(m_scratch); | ||
| m_scratch >> value; | ||
| } catch (const std::exception&) { | ||
| return false; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -265,6 +265,20 @@ class DataStream | |
| size_t GetMemoryUsage() const noexcept; | ||
| }; | ||
|
|
||
| // Require empty scratch streams on entry and reset them on exit. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nit: class description. This description is more about what the class does, not about what its purpose is and how it should be used. I suggest something like this: |
||
| class ScopedDataStreamUsage | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nit: naming. I find the name not very descriptive (it's rather hard to pinpoint the essence).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will adjust if I need to push again |
||
| { | ||
| DataStream& m_stream; | ||
|
|
||
| public: | ||
| explicit ScopedDataStreamUsage(DataStream& stream) : m_stream{stream} { assert(m_stream.empty()); } | ||
|
|
||
| ScopedDataStreamUsage(const ScopedDataStreamUsage&) = delete; | ||
| ScopedDataStreamUsage& operator=(const ScopedDataStreamUsage&) = delete; | ||
|
|
||
| ~ScopedDataStreamUsage() { m_stream.clear(); } | ||
| }; | ||
|
|
||
| template <typename IStream> | ||
| class BitStreamReader | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.