Skip to content
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

Perf(common): optimize cache promotion and demotion #2949

Merged

Conversation

zztaki
Copy link
Contributor

@zztaki zztaki commented Dec 6, 2023

What problem does this PR solve?

Issue Number: #2946

Problem Summary: current cache promotion and demotion need to copy a duplica, erase old elem and reinsert duplica.

What is changed and how it works?

What's Changed: optimize cache promotion and demotion using list.splice and add some tests.

How it Works: list.splice will update cache object priority without object duplica, list reinsertion and hashtable update.

Side effects(Breaking backward compatibility? Performance regression?):

Check List

  • Relevant documentation/comments is changed or added
  • I acknowledge that all my contributions will be made under the project's license

@zztaki
Copy link
Contributor Author

zztaki commented Dec 6, 2023

This is a performance test.

#include <benchmark/benchmark.h>
static void GetWithDuplicate(benchmark::State& state){
    auto cache = std::make_shared<LRUCache<int, int>>(5, nullptr);
    for (int i = 0; i < 5; i++) {
        cache->Put(i, i);
    }
    int v;
    for (auto _ : state) {
        for (int k = 0; k < 5; k++) {
            cache->Get(k, &v);
            assert(k == v);
        }
    }
}
BENCHMARK(GetWithDuplicate);

static void GetWithoutDuplicate(benchmark::State& state){
    auto cache = std::make_shared<LRUCache<int, int>>(5, nullptr);
    for (int i = 0; i < 5; i++) {
        cache->Put(i, i);
    }
    int v;
    for (auto _ : state) {
        for (int k = 0; k < 5; k++) {
            cache->GetWithSplice(k, &v);
            assert(k == v);
        }
    }
}
BENCHMARK(GetWithoutDuplicate);

BENCHMARK_MAIN();

Result is
image

@zztaki zztaki force-pushed the optimize-cache-promotion-demotion branch 2 times, most recently from ccbba07 to 5b20ee4 Compare December 6, 2023 14:50
@zztaki
Copy link
Contributor Author

zztaki commented Dec 6, 2023

cicheck

2 similar comments
@zztaki
Copy link
Contributor Author

zztaki commented Dec 7, 2023

cicheck

@zztaki
Copy link
Contributor Author

zztaki commented Dec 7, 2023

cicheck

@zztaki zztaki force-pushed the optimize-cache-promotion-demotion branch from 5b20ee4 to e7ab404 Compare December 7, 2023 01:34
@zztaki zztaki changed the title Feat(common): optimize cache promotion and demotion [Perf](common): optimize cache promotion and demotion Dec 7, 2023
@zztaki
Copy link
Contributor Author

zztaki commented Dec 7, 2023

cicheck

2 similar comments
@zztaki
Copy link
Contributor Author

zztaki commented Dec 7, 2023

cicheck

@zztaki
Copy link
Contributor Author

zztaki commented Dec 7, 2023

cicheck

@zztaki
Copy link
Contributor Author

zztaki commented Dec 7, 2023

@Ziy1-Tan Hello, this is my first encounter with CI. In fact, I've added some relevant tests that should completely cover my modifications. But CI tells me coverage ratio 72.9 < expectd 73. I don’t know if this is because my modifications are not up to standard or if it’s due to other modules. I hope I can get your help. :)

@zztaki
Copy link
Contributor Author

zztaki commented Dec 8, 2023

Maybe I can add some tests for arc_cache.

@Ziy1-Tan
Copy link
Contributor

Ziy1-Tan commented Dec 8, 2023

Maybe I can add some tests for arc_cache.

Nice, try to add some unit tests. Maybe LRUCache is included by many places, so it trigged code coverage check at these files that had not checked before.

@Ziy1-Tan Ziy1-Tan self-requested a review December 8, 2023 06:02
@zztaki
Copy link
Contributor Author

zztaki commented Dec 8, 2023

Maybe I can add some tests for arc_cache.

Nice, try to add some unit tests. Maybe LRUCache is included by many places, so it trigged code coverage check at these files that had not checked before.

Thanks~

ll_.push_back(key);
cache_[key] = --ll_.end();
size_++;
if (cacheMetrics_ != nullptr) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why delete these lines?

@@ -699,16 +696,8 @@ bool SglLRUCache<K, KeyTraits>::MoveBack(const K &key) {
if (iter == cache_.end()) {
return false;
}
// delete the old value
RemoveElement(iter->second);
Copy link
Contributor

Choose a reason for hiding this comment

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

This function also include some metrics code, maybe you should consider it?

@zztaki
Copy link
Contributor Author

zztaki commented Dec 14, 2023

cicheck

@zztaki zztaki force-pushed the optimize-cache-promotion-demotion branch from e7ab404 to 76d8db6 Compare December 14, 2023 12:19
@zztaki
Copy link
Contributor Author

zztaki commented Dec 14, 2023

cicheck

3 similar comments
@zztaki
Copy link
Contributor Author

zztaki commented Dec 14, 2023

cicheck

@zztaki
Copy link
Contributor Author

zztaki commented Dec 14, 2023

cicheck

@Ziy1-Tan
Copy link
Contributor

cicheck

@zztaki zztaki force-pushed the optimize-cache-promotion-demotion branch from 76d8db6 to b6f6f74 Compare December 20, 2023 03:44
@zztaki
Copy link
Contributor Author

zztaki commented Dec 20, 2023

cicheck

3 similar comments
@zztaki
Copy link
Contributor Author

zztaki commented Dec 21, 2023

cicheck

@zztaki
Copy link
Contributor Author

zztaki commented Dec 21, 2023

cicheck

@zztaki
Copy link
Contributor Author

zztaki commented Dec 26, 2023

cicheck

@zztaki zztaki force-pushed the optimize-cache-promotion-demotion branch from b6f6f74 to f105ea1 Compare December 27, 2023 09:16
@zztaki
Copy link
Contributor Author

zztaki commented Dec 27, 2023

cicheck

1 similar comment
@zztaki
Copy link
Contributor Author

zztaki commented Dec 27, 2023

cicheck

@zztaki zztaki force-pushed the optimize-cache-promotion-demotion branch from f105ea1 to f558e70 Compare December 27, 2023 10:35
@zztaki
Copy link
Contributor Author

zztaki commented Dec 27, 2023

cicheck

1 similar comment
@zztaki
Copy link
Contributor Author

zztaki commented Dec 27, 2023

cicheck

@zztaki zztaki force-pushed the optimize-cache-promotion-demotion branch from f558e70 to 63ef637 Compare December 27, 2023 13:42
@zztaki
Copy link
Contributor Author

zztaki commented Dec 27, 2023

cicheck

@zztaki zztaki force-pushed the optimize-cache-promotion-demotion branch from 63ef637 to e1f772c Compare December 27, 2023 16:09
@zztaki
Copy link
Contributor Author

zztaki commented Dec 27, 2023

cicheck

@zztaki
Copy link
Contributor Author

zztaki commented Dec 28, 2023

Since the line coverage and branch coverage of the existing src/client are not up to standard, an additional commit which adds some tests was submitted.

@zztaki zztaki changed the title [Perf](common): optimize cache promotion and demotion Perf(common): optimize cache promotion and demotion Dec 28, 2023
@zztaki
Copy link
Contributor Author

zztaki commented Dec 28, 2023

close #2982

@wuhongsong wuhongsong merged commit 897eddd into opencurve:master Jan 2, 2024
4 checks passed
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