xinyiZzz commented on code in PR #19472:
URL: https://github.com/apache/doris/pull/19472#discussion_r1193186112
##########
be/src/olap/page_cache.h:
##########
@@ -28,11 +28,62 @@
#include "olap/lru_cache.h"
#include "util/slice.h"
-
+#include "vec/common/allocator.h"
+#include "vec/common/allocator_fwd.h"
namespace doris {
class PageCacheHandle;
+template <typename TAllocator>
+class PageBase : private TAllocator {
+public:
+ PageBase() : size(0), bytes(0) {}
+
+ PageBase(const char* d, size_t n, size_t b) : data(const_cast<char*>(d)),
size(n), bytes(b) {}
+
+ PageBase(const PageBase& other) : data(other.data), size(other.size),
bytes(other.bytes) {}
+
+ PageBase(PageBase&& other) : data(other.data), size(other.size) {
+ std::swap(bytes,
+ other.bytes); // other will be read-only, will not free data
and reset_size.
+ }
+
+ PageBase(size_t b) : size(b), bytes(b) {
+ data = reinterpret_cast<char*>(TAllocator::alloc(b,
ALLOCATOR_ALIGNMENT_16));
+ }
+
+ ~PageBase() {
+ if (bytes > 0) {
+ TAllocator::free(data, size);
+ }
+ }
+
+ void reset_size(size_t n) {
+ DCHECK(n <= bytes);
+ size = n;
+ }
+
+ void swap(PageBase& p) {
+ std::swap(data, p.data);
+ std::swap(size, p.size);
+ std::swap(bytes, p.bytes);
+ }
+
+ void release() {
+ data = nullptr;
+ size = 0;
+ bytes = 0;
+ }
+
+ char* data;
+ // Effective size, smaller than bytes, such as data page remove checksum
suffix.
+ size_t size;
Review Comment:
The `size` here is different from LRUCache's `size`. LRUCache's `size =
charge + handle_size` is not the difference between the effective size and the
actual memory size.
And `charge` seems to be a concept in lru cache, and `bytes` seems to be
easier to understand when describing page actual memory?
##########
be/src/olap/page_cache.cpp:
##########
@@ -64,17 +64,22 @@ bool StoragePageCache::lookup(const CacheKey& key,
PageCacheHandle* handle,
return true;
}
-void StoragePageCache::insert(const CacheKey& key, const Slice& data,
PageCacheHandle* handle,
+void StoragePageCache::insert(const CacheKey& key, DataPage<>& data,
PageCacheHandle* handle,
segment_v2::PageTypePB page_type, bool
in_memory) {
- auto deleter = [](const doris::CacheKey& key, void* value) { delete[]
(uint8_t*)value; };
+ auto deleter = [](const doris::CacheKey& key, void* value) {
+ DataPage<>* cache_value = (DataPage<>*)value;
+ delete cache_value;
+ };
CachePriority priority = CachePriority::NORMAL;
if (in_memory) {
priority = CachePriority::DURABLE;
}
auto cache = _get_page_cache(page_type);
- auto lru_handle = cache->insert(key.encode(), data.data, data.size,
deleter, priority);
+ DataPage<>* cache_value = new DataPage<>(std::move(data));
Review Comment:
done,moved之后`datapage`依然可以正常访问,只是析构时不会delete data[]
不过改成&&确实更优雅
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]