Skip to content

Commit fcc0f11

Browse files
Speed-up DefaultCache::Release (#1347)
Introduce a fast-path for 'mutable-cache-only' scenarios and go to the microseconds' territory: for the applicable cases release time become <1ms (original time depends on the `keys` size, e.g. 200 keys ~ 60ms). This commit switches milliseconds to microseconds for the elapsed timer Relates-To: OAM-1629, OAM-1563 Signed-off-by: Andrey Kashcheev <[email protected]>
1 parent 7293de6 commit fcc0f11

File tree

6 files changed

+60
-57
lines changed

6 files changed

+60
-57
lines changed

olp-cpp-sdk-authentication/tests/AuthenticationClientTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2021 HERE Europe B.V.
2+
* Copyright (C) 2020-2022 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,9 +17,9 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20-
#include <cstdio>
2120
#include <memory>
2221
#include <string>
22+
#include <thread>
2323

2424
#include <gmock/gmock.h>
2525

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 HERE Europe B.V.
2+
* Copyright (C) 2019-2022 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -113,7 +113,7 @@ olp::cache::StorageSettings CreateStorageSettings(
113113
}
114114

115115
int64_t GetElapsedTime(std::chrono::steady_clock::time_point start) {
116-
return std::chrono::duration_cast<std::chrono::milliseconds>(
116+
return std::chrono::duration_cast<std::chrono::microseconds>(
117117
std::chrono::steady_clock::now() - start)
118118
.count();
119119
}
@@ -526,7 +526,7 @@ void DefaultCacheImpl::InitializeLru() {
526526
}
527527

528528
OLP_SDK_LOG_INFO_F(kLogTag,
529-
"LRU cache initialized, items=%zu, time=%" PRId64 " ms",
529+
"LRU cache initialized, items=%zu, time=%" PRId64 "us",
530530
mutable_cache_lru_->Size(), GetElapsedTime(start));
531531
}
532532

@@ -632,7 +632,7 @@ uint64_t DefaultCacheImpl::MaybeEvictData() {
632632

633633
OLP_SDK_LOG_INFO_F(kLogTag,
634634
"Evicted from mutable cache, items=%" PRId32
635-
", time=%" PRId64 "ms, size=%" PRIu64,
635+
", time=%" PRId64 "us, size=%" PRIu64,
636636
count, GetElapsedTime(start), evicted);
637637

638638
return evicted;
@@ -998,7 +998,7 @@ bool DefaultCacheImpl::Protect(const DefaultCache::KeyListType& keys) {
998998
}
999999

10001000
OLP_SDK_LOG_INFO_F(kLogTag,
1001-
"Protect, time=%" PRId64 " ms, added keys size=%" PRIu64
1001+
"Protect, time=%" PRId64 "us, added keys size=%" PRIu64
10021002
", total size=%" PRIu64,
10031003
GetElapsedTime(start),
10041004
static_cast<std::uint64_t>(keys.size()),
@@ -1018,30 +1018,33 @@ bool DefaultCacheImpl::Release(const DefaultCache::KeyListType& keys) {
10181018
return result;
10191019
}
10201020

1021-
for (const auto& key : keys) {
1022-
if (memory_cache_) {
1023-
if (!memory_cache_->Remove(key)) {
1024-
memory_cache_->RemoveKeysWithPrefix(key);
1021+
if (memory_cache_ || (mutable_cache_ && mutable_cache_lru_)) {
1022+
for (const auto& key : keys) {
1023+
if (memory_cache_) {
1024+
if (!memory_cache_->Remove(key)) {
1025+
memory_cache_->RemoveKeysWithPrefix(key);
1026+
}
10251027
}
1026-
}
1027-
if (mutable_cache_) {
1028-
auto it = mutable_cache_->NewIterator(leveldb::ReadOptions());
1029-
it->Seek(key);
1030-
while (it->Valid()) {
1031-
auto cached_key = it->key().ToString();
1032-
if (cached_key.size() >= key.size() &&
1033-
std::equal(key.begin(), key.end(), cached_key.begin())) {
1034-
AddKeyLru(cached_key, it->value());
1035-
} else {
1036-
break;
1028+
1029+
if (mutable_cache_ && mutable_cache_lru_) {
1030+
auto it = mutable_cache_->NewIterator(leveldb::ReadOptions());
1031+
auto key_slice = leveldb::Slice(key);
1032+
it->Seek(key_slice);
1033+
while (it->Valid()) {
1034+
auto cached_key = it->key();
1035+
if (cached_key.starts_with(key_slice)) {
1036+
AddKeyLru(cached_key.ToString(), it->value());
1037+
} else {
1038+
break;
1039+
}
1040+
it->Next();
10371041
}
1038-
it->Next();
10391042
}
10401043
}
10411044
}
10421045

10431046
OLP_SDK_LOG_INFO_F(kLogTag,
1044-
"Release, time=%" PRId64 " ms, released keys size=%" PRIu64
1047+
"Release, time=%" PRId64 "us, released keys size=%" PRIu64
10451048
", total size=%" PRIu64,
10461049
GetElapsedTime(start),
10471050
static_cast<std::uint64_t>(keys.size()),

olp-cpp-sdk-core/tests/thread/ContinuationTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <chrono>
2121
#include <future>
22+
#include <thread>
2223

2324
#include <gtest/gtest.h>
2425

tests/integration/olp-cpp-sdk-authentication/AuthenticationClientTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20+
#include <thread>
21+
2022
#include <gmock/gmock.h>
2123
#include <matchers/NetworkUrlMatchers.h>
2224
#include <mocks/NetworkMock.h>

tests/integration/olp-cpp-sdk-authentication/TokenEndpointTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 HERE Europe B.V.
2+
* Copyright (C) 2022 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919

2020
#include <future>
2121
#include <memory>
22+
#include <thread>
2223

2324
#include <gmock/gmock.h>
2425
#include <matchers/NetworkUrlMatchers.h>

tests/integration/olp-cpp-sdk-authentication/TokenProviderTest.cpp

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2021 HERE Europe B.V.
2+
* Copyright (C) 2019-2022 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20+
#include <thread>
21+
2022
#include <gmock/gmock.h>
2123
#include <matchers/NetworkUrlMatchers.h>
2224
#include <mocks/NetworkMock.h>
@@ -335,20 +337,18 @@ TEST_F(TokenProviderTest, RetrySettings) {
335337
client::CancellationContext context;
336338

337339
EXPECT_CALL(*network_mock_, Send(IsPostRequest(kOAuthTokenUrl), _, _, _, _))
338-
.WillOnce(
339-
testing::WithArg<2>([&](http::Network::Callback callback) {
340-
async_finish_future = std::async(std::launch::async, [=]() {
341-
// Oversleep the timeout period.
342-
std::this_thread::sleep_for(
343-
std::chrono::seconds(kMinTimeout * 2));
340+
.WillOnce(testing::WithArg<2>([&](http::Network::Callback callback) {
341+
async_finish_future = std::async(std::launch::async, [=]() {
342+
// Oversleep the timeout period.
343+
std::this_thread::sleep_for(std::chrono::seconds(kMinTimeout * 2));
344344

345-
callback(http::NetworkResponse()
346-
.WithStatus(http::HttpStatusCode::OK)
347-
.WithRequestId(kRequestId));
348-
});
345+
callback(http::NetworkResponse()
346+
.WithStatus(http::HttpStatusCode::OK)
347+
.WithRequestId(kRequestId));
348+
});
349349

350-
return http::SendOutcome(kRequestId);
351-
}));
350+
return http::SendOutcome(kRequestId);
351+
}));
352352

353353
EXPECT_CALL(*network_mock_, Cancel(kRequestId)).Times(1);
354354

@@ -435,25 +435,21 @@ TEST_F(TokenProviderTest, CancellableProvider) {
435435

436436
client::CancellationContext context;
437437
EXPECT_CALL(*network_mock_, Send(IsPostRequest(kOAuthTokenUrl), _, _, _, _))
438-
.WillOnce(
439-
testing::WithArg<2>([&](http::Network::Callback callback) {
440-
async_finish_future =
441-
std::async(std::launch::async, [&, callback]() {
442-
std::this_thread::sleep_for(
443-
std::chrono::seconds(kMinTimeout));
444-
context.CancelOperation();
445-
446-
EXPECT_EQ(network_wait_promise.get_future().wait_for(
447-
kWaitTimeout),
448-
std::future_status::ready);
449-
450-
callback(http::NetworkResponse()
451-
.WithStatus(http::HttpStatusCode::OK)
452-
.WithRequestId(kRequestId));
453-
});
454-
455-
return http::SendOutcome(kRequestId);
456-
}));
438+
.WillOnce(testing::WithArg<2>([&](http::Network::Callback callback) {
439+
async_finish_future = std::async(std::launch::async, [&, callback]() {
440+
std::this_thread::sleep_for(std::chrono::seconds(kMinTimeout));
441+
context.CancelOperation();
442+
443+
EXPECT_EQ(network_wait_promise.get_future().wait_for(kWaitTimeout),
444+
std::future_status::ready);
445+
446+
callback(http::NetworkResponse()
447+
.WithStatus(http::HttpStatusCode::OK)
448+
.WithRequestId(kRequestId));
449+
});
450+
451+
return http::SendOutcome(kRequestId);
452+
}));
457453

458454
EXPECT_CALL(*network_mock_, Cancel(kRequestId)).Times(1);
459455

0 commit comments

Comments
 (0)