Skip to content

Commit

Permalink
improve object cache expire effeciency
Browse files Browse the repository at this point in the history
  • Loading branch information
beef9999 committed Jun 27, 2024
1 parent 7c59780 commit 1a27551
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
24 changes: 13 additions & 11 deletions common/expirecontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ExpireContainerBase::ExpireContainerBase(uint64_t lifespan,
uint64_t timer_cycle)
: _lifespan(lifespan),
_timer(std::max(static_cast<uint64_t>(1000), timer_cycle),
{this, &ExpireContainerBase::expire}, true, 8UL * 1024 * 1024) {}
{this, &ExpireContainerBase::expire}, true) {}

auto ExpireContainerBase::insert(Item* item) -> std::pair<iterator, bool> {
return _set.emplace(item);
Expand All @@ -38,24 +38,28 @@ auto ExpireContainerBase::find(const Item& key_item) -> iterator {
}

void ExpireContainerBase::clear() {
for (auto x : ({
SCOPED_LOCK(_lock);
_list.node = nullptr;
Set(std::move(_set));
})) {
delete x;
Set tmp;
{
SCOPED_LOCK(_lock);
_list.node = nullptr;
tmp = std::move(_set);
}
for (auto x : tmp)
delete x;
}

uint64_t ExpireContainerBase::expire() {
({
if (_lifespan > UINT64_MAX / 2)
return 0;
{
SCOPED_LOCK(_lock);
_list.split_by_predicate([&](Item* x) {
bool ret = x->_timeout.expiration() < photon::now;
if (ret) _set.erase(x);
return ret;
});
}).delete_all();
}
_list.delete_all();
return 0;
}

Expand All @@ -79,7 +83,6 @@ auto ObjectCacheBase::ref_acquire(const Item& key_item,
uint64_t failure_cooldown) -> Item* {
Base::iterator holder;
Item* item = nullptr;
expire();
do {
SCOPED_LOCK(_lock);
holder = Base::__find_prelock(key_item);
Expand Down Expand Up @@ -115,7 +118,6 @@ auto ObjectCacheBase::ref_acquire(const Item& key_item,
}

int ObjectCacheBase::ref_release(ItemPtr item, bool recycle) {
DEFER(expire());
photon::semaphore sem;
{
SCOPED_LOCK(_lock);
Expand Down
3 changes: 2 additions & 1 deletion common/expirecontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ class ExpireContainerBase : public Object {
template <typename KeyType, typename... Ts>
class ExpireContainer : public ExpireContainerBase {
public:
static constexpr uint64_t EXPIRE_CHECK_TIMES = 16;
using Base = ExpireContainerBase;
ExpireContainer(uint64_t expiration) : Base(expiration, expiration / 16) {}
ExpireContainer(uint64_t expiration) : Base(expiration, expiration / EXPIRE_CHECK_TIMES) {}
ExpireContainer(uint64_t expiration, uint64_t timer_cycle)
: Base(expiration, timer_cycle) {}

Expand Down
3 changes: 2 additions & 1 deletion thread/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace photon
// 0 for default_timeout (given in the ctor)
using Entry = Delegate<uint64_t>;

// Create a timer object with `default_timedout` in usec, callback function `on_timer`,
// Create a timer object with `default_timeout` in usec, callback function `on_timer`,
// and callback argument `arg`. The timer object is implemented as a special thread, so
// it has a `stack_size`, and the `on_timer` is invoked within the thread's context.
// The timer object is deleted automatically after it is finished.
Expand All @@ -39,6 +39,7 @@ namespace photon
{
_on_timer = on_timer;
_default_timeout = default_timeout;
_reset_timeout = -1UL;
_repeating = repeating;
_th = thread_create(&_stub, this, stack_size);
thread_enable_join(_th);
Expand Down

0 comments on commit 1a27551

Please sign in to comment.