Skip to content

Commit

Permalink
Make ObjectCacheV2 reclaim in more effecient way using C++17
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldwings committed Aug 7, 2024
1 parent 2dd0075 commit 008840e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ include(CMake/build-from-src.cmake)
find_package(PkgConfig REQUIRED)

# Options
set(PHOTON_CXX_STANDARD "14" CACHE STRING "C++ standard")
set(PHOTON_CXX_STANDARD "17" CACHE STRING "C++ standard")
option(PHOTON_BUILD_TESTING "enable build testing" OFF)
option(PHOTON_BUILD_WITH_ASAN "build with asan" OFF)
option(PHOTON_ENABLE_URING "enable io_uring function" OFF)
Expand Down
2 changes: 1 addition & 1 deletion common/enumerable.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct Enumerable
this->obj = nullptr;
}
using R = typename std::result_of<decltype(&T::get)(T)>::type;
R operator*() { return obj ? obj->get() : nullptr; }
R operator*() { return obj ? obj->get() : R{}; }
bool operator==(const iterator& rhs) const { return obj == rhs.obj; }
bool operator!=(const iterator& rhs) const { return !(*this == rhs); }
iterator& operator++()
Expand Down
27 changes: 23 additions & 4 deletions common/objectcachev2.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ObjectCacheV2 {
std::swap(r, ref);
return r;
}
std::shared_ptr<V> clear(uint64_t ts = 0) {
std::shared_ptr<V> reset(uint64_t ts = 0) {
return update({nullptr}, ts);
}
std::shared_ptr<V> reader() { return ref; }
Expand Down Expand Up @@ -116,7 +116,11 @@ class ObjectCacheV2 {
return *box;
}
uint64_t __expire() {
#if __cplusplus < 201703L
std::vector<std::shared_ptr<V>> to_release;
#else
intrusive_list<Box> to_release;
#endif
uint64_t now = photon::now;
uint64_t reclaim_before = photon::sat_sub(now, lifespan);
{
Expand All @@ -132,13 +136,27 @@ class ObjectCacheV2 {
if (x->rc == 0) {
// make vector holds those shared_ptr
// prevent object destroy in critical zone
to_release.push_back(x->clear());
#if __cplusplus < 201703L
to_release.push_back(x->reset());
map.erase(*x);
#else
// Here is a hacked implementation
auto node = map.extract(*x);
to_release.push_back(&node.value());
/// node_handle should be just key&val pointer
// Force make it empty can prevent destroy object
// when node_handle goes to destroy
memset((void*)&node, 0, sizeof(node));
#endif
}
x = lru_list.front();
}
}
#if __cplusplus < 201703L
to_release.clear();
#else
to_release.delete_all();
#endif
return 0;
}

Expand All @@ -162,7 +180,7 @@ class ObjectCacheV2 {

~Borrow() {
if (_recycle) {
_box->clear();
_box->reset();
}
_box->release();
if (_box->rc == 0) {
Expand Down Expand Up @@ -228,7 +246,8 @@ class ObjectCacheV2 {

~ObjectCacheV2() {
_timer.stop();
SCOPED_LOCK(maplock);
// Should be no other access during dtor.
// modify lru_list do not need a lock.
lru_list.node = nullptr;
}
};
4 changes: 2 additions & 2 deletions common/ring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ssize_t RingBuffer::do_read(void *buf, size_t count)
ssize_t RingBuffer::readv(const struct iovec *iov, int iovcnt)
{
ssize_t size = 0;
scoped_lock lock(m_read_lock);
photon::scoped_lock lock(m_read_lock);
for (auto& x: ptr_array(iov, iovcnt))
{
auto ret = do_read(x.iov_base, x.iov_len);
Expand Down Expand Up @@ -110,7 +110,7 @@ ssize_t RingBuffer::do_write(const void* buf, size_t count)
ssize_t RingBuffer::writev(const struct iovec *iov, int iovcnt)
{
ssize_t size = 0;
scoped_lock lock(m_write_lock);
photon::scoped_lock lock(m_write_lock);
for (auto& x: ptr_array(iov, iovcnt))
{
auto ret = do_write(x.iov_base, x.iov_len);
Expand Down

0 comments on commit 008840e

Please sign in to comment.