Skip to content

refactor(cache): move rwlock from LruEntry to FileStore, to reduce memory usage#1149

Merged
EricHuangqx merged 3 commits intoalibaba:release/0.9from
xiaoyang-hhh:cache_optimize
Mar 20, 2026
Merged

refactor(cache): move rwlock from LruEntry to FileStore, to reduce memory usage#1149
EricHuangqx merged 3 commits intoalibaba:release/0.9from
xiaoyang-hhh:cache_optimize

Conversation

@xiaoyang-hhh
Copy link
Contributor

@xiaoyang-hhh xiaoyang-hhh commented Mar 17, 2026

Change LruEntry::rw_lock_ from direct member to std::unique_ptr for lazy initialization in Full File Cache:

  • rw_lock_ is now created when the openCount in LruEntry changes from 0 to 1, and destroyed when it changes from 1 to 0
  • Add spinlock to protect rw_lock_ pointer during init/destroy

This reduces memory usage when there are many cache files, but only a small number of them are being read or written at any moment.

@lihuiba
Copy link
Collaborator

lihuiba commented Mar 18, 2026

given that you bind the life-cycle of rwlock to FileStore, is it possible to move the rwlock from LruEntry to FileStore?

@xiaoyang-hhh xiaoyang-hhh force-pushed the cache_optimize branch 4 times, most recently from 87eef3f to b32422d Compare March 18, 2026 11:52
int err;
auto lruEntry = static_cast<QuotaLruEntry*>(fileIter->second.get());

auto cacheStore = static_cast<FileCacheStore*>(open(filePath, O_RDWR, 0644));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEFER(cacheStore->release())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

bool flags_dir_delete = false;

auto cacheStore = static_cast<FileCacheStore*>(open(fileName, O_RDWR, 0644));
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEFER(cacheStore->release())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
int err = 0;
auto cacheStore = static_cast<FileCacheStore*>(open(filePath, O_RDWR, 0644));
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DEFER(cacheStore->release())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@lihuiba lihuiba requested a review from EricHuangqx March 20, 2026 02:33
@lihuiba lihuiba changed the title refactor(cache): lazy initialize rwlock to reduce memory usage refactor(cache): move rwlock from LruEntry to FileStore, to reduce memory usage Mar 20, 2026
@EricHuangqx EricHuangqx merged commit b657470 into alibaba:release/0.9 Mar 20, 2026
12 checks passed
photonlibos pushed a commit that referenced this pull request Mar 20, 2026
…mory usage (#1149)

* refactor(cache): lazy initialize rwlock to reduce memory usage

* fix build

* use defer to release
EricHuangqx pushed a commit that referenced this pull request Mar 20, 2026
…mory usage (#1149) (#1158)

* refactor(cache): lazy initialize rwlock to reduce memory usage

* fix build

* use defer to release

Co-authored-by: xiaoyang-hhh <luxiaoyang.lxy@alibaba-inc.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants